From 7e0af1b8f6bed94d46bf8de2d88f5336e8b3e616 Mon Sep 17 00:00:00 2001 From: Szymon Walter Date: Tue, 20 Mar 2018 11:05:56 +0100 Subject: [PATCH] add generic `Idx` type argument to `Buffer` and `Length` --- src/buffer/length.rs | 74 ++++++++++++++++++++++-------------------- src/buffer/mod.rs | 17 +++++----- src/fs.rs | 9 ++--- src/sys/block_group.rs | 12 +++---- src/sys/inode.rs | 6 ++-- src/sys/superblock.rs | 6 ++-- 6 files changed, 65 insertions(+), 59 deletions(-) diff --git a/src/buffer/length.rs b/src/buffer/length.rs index e198377..a370002 100644 --- a/src/buffer/length.rs +++ b/src/buffer/length.rs @@ -2,28 +2,30 @@ use core::fmt::{self, Debug, Display}; use core::cmp::Ordering; #[derive(Clone, Copy, Debug, Hash)] -pub enum Length { +pub enum Length { Unbounded, - Bounded(usize), + Bounded(Idx), } -impl Length { - pub fn try_len(&self) -> Option { +impl Length { + pub fn try_len(&self) -> Option { match *self { Length::Unbounded => None, Length::Bounded(n) => Some(n), } } - pub unsafe fn len(&self) -> usize { + pub unsafe fn len(&self) -> Idx { match *self { - Length::Unbounded => { - panic!("attempt to convert `Length::Unbounded` to `usize`") - } + Length::Unbounded => panic!( + "attempt to convert `Length::Unbounded` to `Length::Idx`" + ), Length::Bounded(n) => n, } } +} +impl Length { pub fn is_bounded(&self) -> bool { match *self { Length::Unbounded => false, @@ -32,62 +34,64 @@ impl Length { } } -impl Display for Length { +impl Display for Length { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Debug::fmt(self, f) } } -impl PartialEq for Length { - fn eq(&self, rhs: &Length) -> bool { - match (*self, *rhs) { - (Length::Unbounded, _) => false, - (_, Length::Unbounded) => false, - (Length::Bounded(a), Length::Bounded(ref b)) => a.eq(b), +impl PartialEq for Length { + fn eq(&self, rhs: &Self) -> bool { + match (self, rhs) { + (&Length::Unbounded, _) => false, + (_, &Length::Unbounded) => false, + (&Length::Bounded(ref a), &Length::Bounded(ref b)) => a.eq(b), } } - fn ne(&self, rhs: &Length) -> bool { - match (*self, *rhs) { - (Length::Unbounded, _) => false, - (_, Length::Unbounded) => false, - (Length::Bounded(a), Length::Bounded(ref b)) => a.ne(b), + fn ne(&self, rhs: &Self) -> bool { + match (self, rhs) { + (&Length::Unbounded, _) => false, + (_, &Length::Unbounded) => false, + (&Length::Bounded(ref a), &Length::Bounded(ref b)) => a.ne(b), } } } -impl PartialEq for Length { - fn eq(&self, rhs: &usize) -> bool { +impl PartialEq for Length { + fn eq(&self, rhs: &Idx) -> bool { match *self { Length::Unbounded => false, - Length::Bounded(n) => n.eq(rhs), + Length::Bounded(ref n) => n.eq(rhs), } } - fn ne(&self, rhs: &usize) -> bool { + fn ne(&self, rhs: &Idx) -> bool { match *self { Length::Unbounded => false, - Length::Bounded(n) => n.eq(rhs), + Length::Bounded(ref n) => n.eq(rhs), } } } -impl PartialOrd for Length { - fn partial_cmp(&self, rhs: &Length) -> Option { - match (*self, *rhs) { - (Length::Unbounded, Length::Unbounded) => None, - (Length::Unbounded, _) => Some(Ordering::Greater), - (_, Length::Unbounded) => Some(Ordering::Less), - (Length::Bounded(a), Length::Bounded(ref b)) => a.partial_cmp(b), +impl PartialOrd for Length { + fn partial_cmp(&self, rhs: &Self) -> Option { + match (self, rhs) { + (&Length::Unbounded, &Length::Unbounded) => None, + (&Length::Unbounded, _) => Some(Ordering::Greater), + (_, &Length::Unbounded) => Some(Ordering::Less), + (&Length::Bounded(ref a), &Length::Bounded(ref b)) => { + a.partial_cmp(b) + } } } } -impl PartialOrd for Length { - fn partial_cmp(&self, rhs: &usize) -> Option { +impl PartialOrd for Length { + fn partial_cmp(&self, rhs: &Idx) -> Option { match *self { Length::Unbounded => Some(Ordering::Greater), - Length::Bounded(n) => n.partial_cmp(rhs), + Length::Bounded(ref n) => n.partial_cmp(rhs), } } } diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 6a7d137..7b52be0 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -11,23 +11,24 @@ use error::Infallible; pub mod length; use self::length::Length; -pub trait Buffer +pub trait Buffer where [T]: ToOwned, + Idx: PartialEq + PartialOrd, { type Error; - fn len(&self) -> Length; + fn len(&self) -> Length; fn commit( &mut self, slice: Option>, ) -> Result<(), Self::Error>; unsafe fn slice_unchecked<'a>( &'a self, - range: Range, + range: Range, ) -> BufferSlice<'a, T>; - fn slice<'a>(&'a self, range: Range) -> Option> { + fn slice<'a>(&'a self, range: Range) -> Option> { if self.len() >= range.end && self.len() > range.start { unsafe { Some(self.slice_unchecked(range)) } } else { @@ -214,14 +215,14 @@ impl DerefMut for BufferCommit { macro_rules! impl_slice { (@inner $buffer:ty $( , $lt:lifetime )* ) => { - impl<$( $lt, )* T> Buffer for $buffer + impl<$( $lt, )* T> Buffer for $buffer where T: Clone, [T]: ToOwned, { type Error = Infallible; - fn len(&self) -> Length { + fn len(&self) -> Length { Length::Bounded(>::as_ref(self).len()) } @@ -272,10 +273,10 @@ mod file { use super::{Buffer, BufferCommit, BufferSlice}; use super::length::Length; - impl Buffer for RefCell { + impl Buffer for RefCell { type Error = io::Error; - fn len(&self) -> Length { + fn len(&self) -> Length { Length::Bounded( self.borrow() .metadata() diff --git a/src/fs.rs b/src/fs.rs index 837a550..2acc507 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -2,6 +2,7 @@ use core::mem; use alloc::Vec; use error::Error; +use block::Address; // TODO use buffer::{Buffer, BufferSlice}; use sys::superblock::Superblock; use sys::block_group::BlockGroupDescriptor; @@ -20,13 +21,13 @@ impl From<(T, usize)> for Struct { } /// Safe wrapper for raw sys structs -pub struct Ext2> { +pub struct Ext2> { buffer: B, superblock: Struct, block_groups: Struct>, } -impl> Ext2 +impl> Ext2 where Error: From, { @@ -164,7 +165,7 @@ where } } -pub struct Inodes<'a, B: 'a + Buffer> { +pub struct Inodes<'a, B: 'a + Buffer> { buffer: &'a B, block_groups: &'a [BlockGroupDescriptor], block_size: usize, @@ -174,7 +175,7 @@ pub struct Inodes<'a, B: 'a + Buffer> { index: usize, } -impl<'a, B: 'a + Buffer> Iterator for Inodes<'a, B> +impl<'a, B: 'a + Buffer> Iterator for Inodes<'a, B> where Error: From, { diff --git a/src/sys/block_group.rs b/src/sys/block_group.rs index a856fbf..6963bc5 100644 --- a/src/sys/block_group.rs +++ b/src/sys/block_group.rs @@ -51,12 +51,12 @@ impl Debug for BlockGroupDescriptor { } impl BlockGroupDescriptor { - pub unsafe fn find_descriptor<'a, E>( - haystack: &'a Buffer, + pub unsafe fn find_descriptor>( + haystack: &B, offset: usize, ) -> Result<(BlockGroupDescriptor, usize), Error> where - Error: From, + Error: From, { let end = offset + mem::size_of::(); if haystack.len() < end { @@ -70,13 +70,13 @@ impl BlockGroupDescriptor { Ok(descr) } - pub unsafe fn find_descriptor_table<'a, E>( - haystack: &'a Buffer, + pub unsafe fn find_descriptor_table>( + haystack: &B, offset: usize, count: usize, ) -> Result<(Vec, usize), Error> where - Error: From, + Error: From, { let end = offset + count * mem::size_of::(); if haystack.len() < end { diff --git a/src/sys/inode.rs b/src/sys/inode.rs index 3e6241b..52ba803 100644 --- a/src/sys/inode.rs +++ b/src/sys/inode.rs @@ -96,13 +96,13 @@ impl Debug for Inode { } impl Inode { - pub unsafe fn find_inode<'a, E>( - haystack: &'a Buffer, + pub unsafe fn find_inode>( + haystack: &B, offset: usize, size: usize, ) -> Result<(Inode, usize), Error> where - Error: From, + Error: From, { if size != mem::size_of::() { unimplemented!("inodes with a size != 128"); diff --git a/src/sys/superblock.rs b/src/sys/superblock.rs index 77cb82a..d2cb36c 100644 --- a/src/sys/superblock.rs +++ b/src/sys/superblock.rs @@ -194,11 +194,11 @@ impl Debug for Superblock { } impl Superblock { - pub unsafe fn find<'a, E>( - haystack: &'a Buffer, + pub unsafe fn find>( + haystack: &B, ) -> Result<(Superblock, usize), Error> where - Error: From, + Error: From, { let offset = 1024; let end = offset + mem::size_of::();