rename buffer
to volume
to better reflect its purpose
This commit is contained in:
parent
ab069b60e6
commit
ce162aae90
50
src/fs.rs
50
src/fs.rs
|
@ -5,7 +5,7 @@ use alloc::Vec;
|
|||
|
||||
use error::Error;
|
||||
use sector::{Address, Size};
|
||||
use buffer::{Buffer, BufferSlice};
|
||||
use volume::{Volume, VolumeSlice};
|
||||
use sys::superblock::Superblock;
|
||||
use sys::block_group::BlockGroupDescriptor;
|
||||
use sys::inode::Inode;
|
||||
|
@ -23,18 +23,18 @@ impl<T, S: Size> From<(T, Address<S>)> for Struct<T, S> {
|
|||
}
|
||||
|
||||
/// Safe wrapper for raw sys structs
|
||||
pub struct Ext2<S: Size, B: Buffer<u8, Address<S>>> {
|
||||
buffer: B,
|
||||
pub struct Ext2<S: Size, V: Volume<u8, Address<S>>> {
|
||||
volume: V,
|
||||
superblock: Struct<Superblock, S>,
|
||||
block_groups: Struct<Vec<BlockGroupDescriptor>, S>,
|
||||
}
|
||||
|
||||
impl<S: Size + Copy, B: Buffer<u8, Address<S>>> Ext2<S, B>
|
||||
impl<S: Size + Copy, V: Volume<u8, Address<S>>> Ext2<S, V>
|
||||
where
|
||||
Error: From<B::Error>,
|
||||
Error: From<V::Error>,
|
||||
{
|
||||
pub fn new(buffer: B) -> Result<Ext2<S, B>, Error> {
|
||||
let superblock = unsafe { Struct::from(Superblock::find(&buffer)?) };
|
||||
pub fn new(volume: V) -> Result<Ext2<S, V>, Error> {
|
||||
let superblock = unsafe { Struct::from(Superblock::find(&volume)?) };
|
||||
let block_groups_offset = Address::with_block_size(
|
||||
superblock.inner.first_data_block as usize + 1,
|
||||
0,
|
||||
|
@ -47,14 +47,14 @@ where
|
|||
.map_err(|(a, b)| Error::BadBlockGroupCount(a, b))?;
|
||||
let block_groups = unsafe {
|
||||
BlockGroupDescriptor::find_descriptor_table(
|
||||
&buffer,
|
||||
&volume,
|
||||
block_groups_offset,
|
||||
block_groups_count,
|
||||
)?
|
||||
};
|
||||
let block_groups = Struct::from(block_groups);
|
||||
Ok(Ext2 {
|
||||
buffer,
|
||||
volume,
|
||||
superblock,
|
||||
block_groups,
|
||||
})
|
||||
|
@ -64,20 +64,20 @@ where
|
|||
fn update_global(&mut self) -> Result<(), Error> {
|
||||
// superblock
|
||||
{
|
||||
let slice = BufferSlice::from_cast(
|
||||
let slice = VolumeSlice::from_cast(
|
||||
&self.superblock.inner,
|
||||
self.superblock.offset,
|
||||
);
|
||||
let commit = slice.commit();
|
||||
self.buffer.commit(commit).map_err(|err| Error::from(err))?;
|
||||
self.volume.commit(commit).map_err(|err| Error::from(err))?;
|
||||
}
|
||||
|
||||
// block group descriptors
|
||||
let mut offset = self.block_groups.offset;
|
||||
for descr in &self.block_groups.inner {
|
||||
let slice = BufferSlice::from_cast(descr, offset);
|
||||
let slice = VolumeSlice::from_cast(descr, offset);
|
||||
let commit = slice.commit();
|
||||
self.buffer.commit(commit).map_err(|err| Error::from(err))?;
|
||||
self.volume.commit(commit).map_err(|err| Error::from(err))?;
|
||||
offset =
|
||||
offset + Address::from(mem::size_of::<BlockGroupDescriptor>());
|
||||
}
|
||||
|
@ -90,9 +90,9 @@ where
|
|||
&mut self,
|
||||
&(ref inode, offset): &(Inode, Address<S>),
|
||||
) -> Result<(), Error> {
|
||||
let slice = BufferSlice::from_cast(inode, offset);
|
||||
let slice = VolumeSlice::from_cast(inode, offset);
|
||||
let commit = slice.commit();
|
||||
self.buffer.commit(commit).map_err(|err| Error::from(err))
|
||||
self.volume.commit(commit).map_err(|err| Error::from(err))
|
||||
}
|
||||
|
||||
pub fn root_inode(&self) -> (Inode, Address<S>) {
|
||||
|
@ -103,14 +103,14 @@ where
|
|||
self.inodes_nth(index).next()
|
||||
}
|
||||
|
||||
pub fn inodes<'a>(&'a self) -> Inodes<'a, S, B> {
|
||||
pub fn inodes<'a>(&'a self) -> Inodes<'a, S, V> {
|
||||
self.inodes_nth(1)
|
||||
}
|
||||
|
||||
pub fn inodes_nth<'a>(&'a self, index: usize) -> Inodes<'a, S, B> {
|
||||
pub fn inodes_nth<'a>(&'a self, index: usize) -> Inodes<'a, S, V> {
|
||||
assert!(index > 0, "inodes are 1-indexed");
|
||||
Inodes {
|
||||
buffer: &self.buffer,
|
||||
volume: &self.volume,
|
||||
block_groups: &self.block_groups.inner,
|
||||
log_block_size: self.log_block_size(),
|
||||
inode_size: self.inode_size(),
|
||||
|
@ -175,8 +175,8 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Inodes<'a, S: Size, B: 'a + Buffer<u8, Address<S>>> {
|
||||
buffer: &'a B,
|
||||
pub struct Inodes<'a, S: Size, V: 'a + Volume<u8, Address<S>>> {
|
||||
volume: &'a V,
|
||||
block_groups: &'a [BlockGroupDescriptor],
|
||||
log_block_size: u32,
|
||||
inode_size: usize,
|
||||
|
@ -186,10 +186,10 @@ pub struct Inodes<'a, S: Size, B: 'a + Buffer<u8, Address<S>>> {
|
|||
_phantom: PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<'a, S: Size + Copy, B: 'a + Buffer<u8, Address<S>>> Iterator
|
||||
for Inodes<'a, S, B>
|
||||
impl<'a, S: Size + Copy, V: 'a + Volume<u8, Address<S>>> Iterator
|
||||
for Inodes<'a, S, V>
|
||||
where
|
||||
Error: From<B::Error>,
|
||||
Error: From<V::Error>,
|
||||
{
|
||||
type Item = (Inode, Address<S>);
|
||||
|
||||
|
@ -208,7 +208,7 @@ where
|
|||
self.log_block_size,
|
||||
);
|
||||
unsafe {
|
||||
Inode::find_inode(self.buffer, offset, self.inode_size).ok()
|
||||
Inode::find_inode(self.volume, offset, self.inode_size).ok()
|
||||
}
|
||||
} else {
|
||||
None
|
||||
|
@ -222,7 +222,7 @@ mod tests {
|
|||
use std::cell::RefCell;
|
||||
|
||||
use sector::{Address, Size512};
|
||||
use buffer::Buffer;
|
||||
use volume::Volume;
|
||||
|
||||
use super::Ext2;
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ extern crate core;
|
|||
pub mod error;
|
||||
pub mod sys;
|
||||
pub mod sector;
|
||||
pub mod buffer;
|
||||
pub mod volume;
|
||||
pub mod fs;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -5,7 +5,7 @@ use alloc::Vec;
|
|||
|
||||
use error::Error;
|
||||
use sector::{Address, Size};
|
||||
use buffer::Buffer;
|
||||
use volume::Volume;
|
||||
|
||||
/// The Block Group Descriptor Table contains a descriptor for each block group
|
||||
/// within the file system. The number of block groups within the file system,
|
||||
|
@ -54,13 +54,13 @@ impl Debug for BlockGroupDescriptor {
|
|||
impl BlockGroupDescriptor {
|
||||
pub unsafe fn find_descriptor<
|
||||
S: Size + Copy + PartialOrd,
|
||||
B: Buffer<u8, Address<S>>,
|
||||
V: Volume<u8, Address<S>>,
|
||||
>(
|
||||
haystack: &B,
|
||||
haystack: &V,
|
||||
offset: Address<S>,
|
||||
) -> Result<(BlockGroupDescriptor, Address<S>), Error>
|
||||
where
|
||||
Error: From<B::Error>,
|
||||
Error: From<V::Error>,
|
||||
{
|
||||
let end =
|
||||
offset + Address::from(mem::size_of::<BlockGroupDescriptor>());
|
||||
|
@ -81,14 +81,14 @@ impl BlockGroupDescriptor {
|
|||
|
||||
pub unsafe fn find_descriptor_table<
|
||||
S: Size + Copy + PartialOrd,
|
||||
B: Buffer<u8, Address<S>>,
|
||||
V: Volume<u8, Address<S>>,
|
||||
>(
|
||||
haystack: &B,
|
||||
haystack: &V,
|
||||
offset: Address<S>,
|
||||
count: usize,
|
||||
) -> Result<(Vec<BlockGroupDescriptor>, Address<S>), Error>
|
||||
where
|
||||
Error: From<B::Error>,
|
||||
Error: From<V::Error>,
|
||||
{
|
||||
let end = offset
|
||||
+ Address::from(count * mem::size_of::<BlockGroupDescriptor>());
|
||||
|
@ -120,10 +120,10 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn find() {
|
||||
let buffer = vec![0_u8; 4096];
|
||||
let volume = vec![0_u8; 4096];
|
||||
let table = unsafe {
|
||||
BlockGroupDescriptor::find_descriptor_table(
|
||||
&buffer,
|
||||
&volume,
|
||||
Address::<Size512>::new(4, 0),
|
||||
8,
|
||||
)
|
||||
|
|
|
@ -3,7 +3,7 @@ use core::fmt::{self, Debug};
|
|||
|
||||
use error::Error;
|
||||
use sector::{Address, Size};
|
||||
use buffer::Buffer;
|
||||
use volume::Volume;
|
||||
|
||||
/// An inode is a structure on the disk that represents a file, directory,
|
||||
/// symbolic link, etc. Inodes do not contain the data of the file / directory /
|
||||
|
@ -99,14 +99,14 @@ impl Debug for Inode {
|
|||
impl Inode {
|
||||
pub unsafe fn find_inode<
|
||||
S: Size + Copy + PartialOrd,
|
||||
B: Buffer<u8, Address<S>>,
|
||||
V: Volume<u8, Address<S>>,
|
||||
>(
|
||||
haystack: &B,
|
||||
haystack: &V,
|
||||
offset: Address<S>,
|
||||
size: usize,
|
||||
) -> Result<(Inode, Address<S>), Error>
|
||||
where
|
||||
Error: From<B::Error>,
|
||||
Error: From<V::Error>,
|
||||
{
|
||||
if size != mem::size_of::<Inode>() {
|
||||
unimplemented!("inodes with a size != 128");
|
||||
|
|
|
@ -3,7 +3,7 @@ use core::fmt::{self, Debug};
|
|||
|
||||
use error::Error;
|
||||
use sector::{Address, Size};
|
||||
use buffer::Buffer;
|
||||
use volume::Volume;
|
||||
|
||||
/// Ext2 signature (0xef53), used to help confirm the presence of Ext2 on a
|
||||
/// volume
|
||||
|
@ -195,11 +195,11 @@ impl Debug for Superblock {
|
|||
}
|
||||
|
||||
impl Superblock {
|
||||
pub unsafe fn find<S: Size + Copy + PartialOrd, B: Buffer<u8, Address<S>>>(
|
||||
haystack: &B,
|
||||
pub unsafe fn find<S: Size + Copy + PartialOrd, V: Volume<u8, Address<S>>>(
|
||||
haystack: &V,
|
||||
) -> Result<(Superblock, Address<S>), Error>
|
||||
where
|
||||
Error: From<B::Error>,
|
||||
Error: From<V::Error>,
|
||||
{
|
||||
let offset = Address::from(1024_usize);
|
||||
let end = offset + Address::from(mem::size_of::<Superblock>());
|
||||
|
@ -301,11 +301,11 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn find() {
|
||||
let mut buffer = vec![0_u8; 4096];
|
||||
let mut volume = vec![0_u8; 4096];
|
||||
// magic
|
||||
buffer[1024 + 56] = EXT2_MAGIC as u8;
|
||||
buffer[1024 + 57] = (EXT2_MAGIC >> 8) as u8;
|
||||
let superblock = unsafe { Superblock::find::<Size512, _>(&buffer) };
|
||||
volume[1024 + 56] = EXT2_MAGIC as u8;
|
||||
volume[1024 + 57] = (EXT2_MAGIC >> 8) as u8;
|
||||
let superblock = unsafe { Superblock::find::<Size512, _>(&volume) };
|
||||
assert!(
|
||||
superblock.is_ok(),
|
||||
"Err({:?})",
|
||||
|
|
|
@ -12,7 +12,7 @@ use sector::{Address, Size};
|
|||
pub mod length;
|
||||
use self::length::Length;
|
||||
|
||||
pub trait Buffer<T, Idx>
|
||||
pub trait Volume<T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
Idx: PartialEq + PartialOrd,
|
||||
|
@ -22,17 +22,17 @@ where
|
|||
fn len(&self) -> Length<Idx>;
|
||||
fn commit(
|
||||
&mut self,
|
||||
slice: Option<BufferCommit<T, Idx>>,
|
||||
slice: Option<VolumeCommit<T, Idx>>,
|
||||
) -> Result<(), Self::Error>;
|
||||
unsafe fn slice_unchecked<'a>(
|
||||
&'a self,
|
||||
range: Range<Idx>,
|
||||
) -> BufferSlice<'a, T, Idx>;
|
||||
) -> VolumeSlice<'a, T, Idx>;
|
||||
|
||||
fn slice<'a>(
|
||||
&'a self,
|
||||
range: Range<Idx>,
|
||||
) -> Option<BufferSlice<'a, T, Idx>> {
|
||||
) -> Option<VolumeSlice<'a, T, Idx>> {
|
||||
if self.len() >= range.end && self.len() > range.start {
|
||||
unsafe { Some(self.slice_unchecked(range)) }
|
||||
} else {
|
||||
|
@ -41,7 +41,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub struct BufferSlice<'a, T: 'a, Idx>
|
||||
pub struct VolumeSlice<'a, T: 'a, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
{
|
||||
|
@ -49,39 +49,39 @@ where
|
|||
index: Idx,
|
||||
}
|
||||
|
||||
impl<T, Idx: Default> BufferSlice<'static, T, Idx>
|
||||
impl<T, Idx: Default> VolumeSlice<'static, T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
{
|
||||
pub fn with_static(inner: &'static [T]) -> BufferSlice<'static, T, Idx> {
|
||||
BufferSlice {
|
||||
pub fn with_static(inner: &'static [T]) -> VolumeSlice<'static, T, Idx> {
|
||||
VolumeSlice {
|
||||
inner: Cow::Borrowed(inner),
|
||||
index: Idx::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Idx> BufferSlice<'static, T, Idx>
|
||||
impl<T, Idx> VolumeSlice<'static, T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
{
|
||||
pub fn new_owned(
|
||||
inner: <[T] as ToOwned>::Owned,
|
||||
index: Idx,
|
||||
) -> BufferSlice<'static, T, Idx> {
|
||||
BufferSlice {
|
||||
) -> VolumeSlice<'static, T, Idx> {
|
||||
VolumeSlice {
|
||||
inner: Cow::Owned(inner),
|
||||
index,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Idx> BufferSlice<'a, T, Idx>
|
||||
impl<'a, T, Idx> VolumeSlice<'a, T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
{
|
||||
pub fn new(inner: &'a [T], index: Idx) -> BufferSlice<'a, T, Idx> {
|
||||
BufferSlice {
|
||||
pub fn new(inner: &'a [T], index: Idx) -> VolumeSlice<'a, T, Idx> {
|
||||
VolumeSlice {
|
||||
inner: Cow::Borrowed(inner),
|
||||
index,
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, Idx: Copy> BufferSlice<'a, u8, Idx> {
|
||||
impl<'a, Idx: Copy> VolumeSlice<'a, u8, Idx> {
|
||||
pub unsafe fn dynamic_cast<T: Copy>(&self) -> (T, Idx) {
|
||||
assert!(self.inner.len() >= mem::size_of::<T>());
|
||||
let index = self.index;
|
||||
|
@ -110,28 +110,28 @@ impl<'a, Idx: Copy> BufferSlice<'a, u8, Idx> {
|
|||
pub fn from_cast<T: Copy>(
|
||||
cast: &'a T,
|
||||
index: Idx,
|
||||
) -> BufferSlice<'a, u8, Idx> {
|
||||
) -> VolumeSlice<'a, u8, Idx> {
|
||||
let len = mem::size_of::<T>();
|
||||
let ptr = cast as *const T as *const u8;
|
||||
let slice = unsafe { slice::from_raw_parts(ptr, len) };
|
||||
BufferSlice::new(slice, index)
|
||||
VolumeSlice::new(slice, index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Idx> BufferSlice<'a, T, Idx>
|
||||
impl<'a, T, Idx> VolumeSlice<'a, T, Idx>
|
||||
where
|
||||
[T]: ToOwned<Owned = Vec<T>>,
|
||||
{
|
||||
pub fn commit(self) -> Option<BufferCommit<T, Idx>> {
|
||||
pub fn commit(self) -> Option<VolumeCommit<T, Idx>> {
|
||||
if self.is_mutated() {
|
||||
Some(BufferCommit::new(self.inner.into_owned(), self.index))
|
||||
Some(VolumeCommit::new(self.inner.into_owned(), self.index))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Idx> AsRef<[T]> for BufferSlice<'a, T, Idx>
|
||||
impl<'a, T, Idx> AsRef<[T]> for VolumeSlice<'a, T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
{
|
||||
|
@ -140,7 +140,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Idx> AsMut<[T]> for BufferSlice<'a, T, Idx>
|
||||
impl<'a, T, Idx> AsMut<[T]> for VolumeSlice<'a, T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
<[T] as ToOwned>::Owned: AsMut<[T]>,
|
||||
|
@ -150,7 +150,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Idx> Deref for BufferSlice<'a, T, Idx>
|
||||
impl<'a, T, Idx> Deref for VolumeSlice<'a, T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
{
|
||||
|
@ -161,7 +161,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Idx> DerefMut for BufferSlice<'a, T, Idx>
|
||||
impl<'a, T, Idx> DerefMut for VolumeSlice<'a, T, Idx>
|
||||
where
|
||||
[T]: ToOwned,
|
||||
<[T] as ToOwned>::Owned: AsMut<[T]>,
|
||||
|
@ -171,23 +171,23 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub struct BufferCommit<T, Idx> {
|
||||
pub struct VolumeCommit<T, Idx> {
|
||||
inner: Vec<T>,
|
||||
index: Idx,
|
||||
}
|
||||
|
||||
impl<T, Idx: Default> BufferCommit<T, Idx> {
|
||||
pub fn with_vec(inner: Vec<T>) -> BufferCommit<T, Idx> {
|
||||
BufferCommit {
|
||||
impl<T, Idx: Default> VolumeCommit<T, Idx> {
|
||||
pub fn with_vec(inner: Vec<T>) -> VolumeCommit<T, Idx> {
|
||||
VolumeCommit {
|
||||
inner,
|
||||
index: Idx::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Idx> BufferCommit<T, Idx> {
|
||||
pub fn new(inner: Vec<T>, index: Idx) -> BufferCommit<T, Idx> {
|
||||
BufferCommit { inner, index }
|
||||
impl<T, Idx> VolumeCommit<T, Idx> {
|
||||
pub fn new(inner: Vec<T>, index: Idx) -> VolumeCommit<T, Idx> {
|
||||
VolumeCommit { inner, index }
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> Vec<T> {
|
||||
|
@ -199,19 +199,19 @@ impl<T, Idx> BufferCommit<T, Idx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T, Idx> AsRef<[T]> for BufferCommit<T, Idx> {
|
||||
impl<T, Idx> AsRef<[T]> for VolumeCommit<T, Idx> {
|
||||
fn as_ref(&self) -> &[T] {
|
||||
self.inner.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Idx> AsMut<[T]> for BufferCommit<T, Idx> {
|
||||
impl<T, Idx> AsMut<[T]> for VolumeCommit<T, Idx> {
|
||||
fn as_mut(&mut self) -> &mut [T] {
|
||||
self.inner.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Idx> Deref for BufferCommit<T, Idx> {
|
||||
impl<T, Idx> Deref for VolumeCommit<T, Idx> {
|
||||
type Target = [T];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
|
@ -219,15 +219,15 @@ impl<T, Idx> Deref for BufferCommit<T, Idx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T, Idx> DerefMut for BufferCommit<T, Idx> {
|
||||
impl<T, Idx> DerefMut for VolumeCommit<T, Idx> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_slice {
|
||||
(@inner $buffer:ty $( , $lt:lifetime )* ) => {
|
||||
impl<$( $lt, )* S: Size + PartialOrd + Copy, T> Buffer<T, Address<S>> for $buffer
|
||||
(@inner $volume:ty $( , $lt:lifetime )* ) => {
|
||||
impl<$( $lt, )* S: Size + PartialOrd + Copy, T> Volume<T, Address<S>> for $volume
|
||||
where
|
||||
T: Clone,
|
||||
[T]: ToOwned,
|
||||
|
@ -238,7 +238,7 @@ macro_rules! impl_slice {
|
|||
Length::Bounded(Address::from(<Self as AsRef<[T]>>::as_ref(self).len()))
|
||||
}
|
||||
|
||||
fn commit(&mut self, slice: Option<BufferCommit<T, Address<S>>>) -> Result<(), Infallible> {
|
||||
fn commit(&mut self, slice: Option<VolumeCommit<T, Address<S>>>) -> Result<(), Infallible> {
|
||||
slice.map(|slice| {
|
||||
let index = slice.at_index().index64() as usize;
|
||||
let end = index + slice.as_ref().len();
|
||||
|
@ -254,21 +254,21 @@ macro_rules! impl_slice {
|
|||
unsafe fn slice_unchecked<'a>(
|
||||
&'a self,
|
||||
range: Range<Address<S>>,
|
||||
) -> BufferSlice<'a, T, Address<S>> {
|
||||
) -> VolumeSlice<'a, T, Address<S>> {
|
||||
let index = range.start;
|
||||
let range = range.start.index64() as usize..range.end.index64() as usize;
|
||||
BufferSlice::new(
|
||||
VolumeSlice::new(
|
||||
<Self as AsRef<[T]>>::as_ref(self).get_unchecked(range),
|
||||
index,
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
($buffer:ty) => {
|
||||
impl_slice!(@inner $buffer);
|
||||
($volume:ty) => {
|
||||
impl_slice!(@inner $volume);
|
||||
};
|
||||
($buffer:ty $( , $lt:lifetime )* ) => {
|
||||
impl_slice!(@inner $buffer $( , $lt )* );
|
||||
($volume:ty $( , $lt:lifetime )* ) => {
|
||||
impl_slice!(@inner $volume $( , $lt )* );
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -285,10 +285,10 @@ mod file {
|
|||
|
||||
use sector::{Address, Size};
|
||||
|
||||
use super::{Buffer, BufferCommit, BufferSlice};
|
||||
use super::{Volume, VolumeCommit, VolumeSlice};
|
||||
use super::length::Length;
|
||||
|
||||
impl<S: Size + PartialOrd + Copy> Buffer<u8, Address<S>> for RefCell<File> {
|
||||
impl<S: Size + PartialOrd + Copy> Volume<u8, Address<S>> for RefCell<File> {
|
||||
type Error = io::Error;
|
||||
|
||||
fn len(&self) -> Length<Address<S>> {
|
||||
|
@ -302,7 +302,7 @@ mod file {
|
|||
|
||||
fn commit(
|
||||
&mut self,
|
||||
slice: Option<BufferCommit<u8, Address<S>>>,
|
||||
slice: Option<VolumeCommit<u8, Address<S>>>,
|
||||
) -> Result<(), Self::Error> {
|
||||
slice
|
||||
.map(|slice| {
|
||||
|
@ -320,7 +320,7 @@ mod file {
|
|||
unsafe fn slice_unchecked<'a>(
|
||||
&'a self,
|
||||
range: Range<Address<S>>,
|
||||
) -> BufferSlice<'a, u8, Address<S>> {
|
||||
) -> VolumeSlice<'a, u8, Address<S>> {
|
||||
let index = range.start;
|
||||
let len = range.end - range.start;
|
||||
let mut vec = Vec::with_capacity(len.index64() as usize);
|
||||
|
@ -330,15 +330,15 @@ mod file {
|
|||
.seek(SeekFrom::Start(index.index64()))
|
||||
.and_then(|_| refmut.read_exact(&mut vec[..]))
|
||||
.unwrap_or_else(|err| {
|
||||
panic!("could't read from File Buffer: {:?}", err)
|
||||
panic!("could't read from File Volume: {:?}", err)
|
||||
});
|
||||
BufferSlice::new_owned(vec, index)
|
||||
VolumeSlice::new_owned(vec, index)
|
||||
}
|
||||
|
||||
fn slice<'a>(
|
||||
&'a self,
|
||||
range: Range<Address<S>>,
|
||||
) -> Option<BufferSlice<'a, u8, Address<S>>> {
|
||||
) -> Option<VolumeSlice<'a, u8, Address<S>>> {
|
||||
let index = range.start;
|
||||
let mut vec = Vec::with_capacity(
|
||||
(range.end - range.start).index64() as usize,
|
||||
|
@ -347,7 +347,7 @@ mod file {
|
|||
refmut
|
||||
.seek(SeekFrom::Start(index.index64()))
|
||||
.and_then(|_| refmut.read_exact(&mut vec[..]))
|
||||
.map(move |_| BufferSlice::new_owned(vec, index))
|
||||
.map(move |_| VolumeSlice::new_owned(vec, index))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
@ -359,10 +359,10 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn buffer() {
|
||||
let mut buffer = vec![0; 1024];
|
||||
fn volume() {
|
||||
let mut volume = vec![0; 1024];
|
||||
let commit = {
|
||||
let mut slice = buffer
|
||||
let mut slice = volume
|
||||
.slice(
|
||||
Address::<Size512>::from(256_usize)
|
||||
..Address::<Size512>::from(512_usize),
|
||||
|
@ -371,9 +371,9 @@ mod tests {
|
|||
slice.iter_mut().for_each(|x| *x = 1);
|
||||
slice.commit()
|
||||
};
|
||||
assert!(buffer.commit(commit).is_ok());
|
||||
assert!(volume.commit(commit).is_ok());
|
||||
|
||||
for (i, &x) in buffer.iter().enumerate() {
|
||||
for (i, &x) in volume.iter().enumerate() {
|
||||
if i < 256 || i >= 512 {
|
||||
assert_eq!(x, 0);
|
||||
} else {
|
Reference in a new issue