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