diff --git a/README.md b/README.md index 5f0dbee..73a7a8d 100644 --- a/README.md +++ b/README.md @@ -194,10 +194,9 @@ struct Data<'a> { // note the lifetime specified here impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> { type Error = scroll::Error; - type Size = usize; // and the lifetime annotation on `&'a [u8]` here fn try_from_ctx (src: &'a [u8], endian: Endian) - -> Result<(Self, Self::Size), Self::Error> { + -> Result<(Self, usize), Self::Error> { let offset = &mut 0; let name = src.gread::<&str>(offset)?; let id = src.gread_with(offset, endian)?; diff --git a/examples/data_ctx.rs b/examples/data_ctx.rs index 1771dfc..fda5da4 100644 --- a/examples/data_ctx.rs +++ b/examples/data_ctx.rs @@ -10,9 +10,8 @@ struct Data<'a> { impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> { type Error = scroll::Error; - type Size = usize; fn try_from_ctx (src: &'a [u8], endian: Endian) - -> Result<(Self, Self::Size), Self::Error> { + -> Result<(Self, usize), Self::Error> { let name = src.pread::<&'a str>(0)?; let id = src.pread_with(name.len()+1, endian)?; Ok((Data { name: name, id: id }, name.len()+4)) diff --git a/src/ctx.rs b/src/ctx.rs index 7aa05e5..e370c79 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -28,9 +28,8 @@ //! //! impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> { //! type Error = scroll::Error; -//! type Size = usize; //! fn try_from_ctx (src: &'a [u8], ctx: Endian) -//! -> Result<(Self, Self::Size), Self::Error> { +//! -> Result<(Self, usize), Self::Error> { //! let name = src.pread::<&str>(0)?; //! let id = src.pread_with(name.len() + 1, ctx)?; //! Ok((Data { name: name, id: id }, name.len() + 1 + 4)) @@ -58,23 +57,20 @@ use endian::Endian; /// A trait for measuring how large something is; for a byte sequence, it will be its length. pub trait MeasureWith { - type Units; /// How large is `Self`, given the `ctx`? - fn measure_with(&self, ctx: &Ctx) -> Self::Units; + fn measure_with(&self, ctx: &Ctx) -> usize; } impl MeasureWith for [u8] { - type Units = usize; #[inline] - fn measure_with(&self, _ctx: &Ctx) -> Self::Units { + fn measure_with(&self, _ctx: &Ctx) -> usize { self.len() } } impl> MeasureWith for T { - type Units = usize; #[inline] - fn measure_with(&self, _ctx: &Ctx) -> Self::Units { + fn measure_with(&self, _ctx: &Ctx) -> usize { self.as_ref().len() } } @@ -127,8 +123,7 @@ pub trait FromCtx { /// Tries to read `Self` from `This` using the context `Ctx` pub trait TryFromCtx<'a, Ctx: Copy = (), This: ?Sized = [u8]> where Self: 'a + Sized { type Error; - type Size; - fn try_from_ctx(from: &'a This, ctx: Ctx) -> Result<(Self, Self::Size), Self::Error>; + fn try_from_ctx(from: &'a This, ctx: Ctx) -> Result<(Self, usize), Self::Error>; } /// Writes `Self` into `This` using the context `Ctx` @@ -139,8 +134,7 @@ pub trait IntoCtx: Sized { /// Tries to write `Self` into `This` using the context `Ctx` pub trait TryIntoCtx: Sized { type Error; - type Size; - fn try_into_ctx(self, &mut This, ctx: Ctx) -> Result; + fn try_into_ctx(self, &mut This, ctx: Ctx) -> Result; } /// Gets the size of `Self` with a `Ctx`, and in `Self::Units`. Implementors can then call `Gread` related functions @@ -150,8 +144,7 @@ pub trait TryIntoCtx: Sized { /// 1. Prevent `gread` from being used, and the offset being modified based on simply the sizeof the value, which can be a misnomer, e.g., for Leb128, etc. /// 2. Allow a context based size, which is useful for 32/64 bit variants for various containers, etc. pub trait SizeWith { - type Units; - fn size_with(ctx: &Ctx) -> Self::Units; + fn size_with(ctx: &Ctx) -> usize; } macro_rules! signed_to_unsigned { @@ -196,9 +189,8 @@ macro_rules! into_ctx_impl { } impl TryIntoCtx for $typ where $typ: IntoCtx { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { if $size > dst.len () { Err(error::Error::TooBig{size: $size, len: dst.len()}) } else { @@ -209,9 +201,8 @@ macro_rules! into_ctx_impl { } impl<'a> TryIntoCtx for &'a $typ { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { (*self).try_into_ctx(dst, le) } } @@ -236,10 +227,9 @@ macro_rules! from_ctx_impl { } impl<'a> TryFromCtx<'a, Endian> for $typ where $typ: FromCtx { - type Error = error::Error; - type Size = usize; + type Error = error::Error; #[inline] - fn try_from_ctx(src: &'a [u8], le: Endian) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], le: Endian) -> result::Result<(Self, usize), Self::Error> { if $size > src.len () { Err(error::Error::TooBig{size: $size, len: src.len()}) } else { @@ -266,9 +256,8 @@ macro_rules! from_ctx_impl { impl<'a, T> TryFromCtx<'a, Endian, T> for $typ where $typ: FromCtx, T: AsRef<[u8]> { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a T, le: Endian) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a T, le: Endian) -> result::Result<(Self, usize), Self::Error> { let src = src.as_ref(); Self::try_from_ctx(src, le) } @@ -313,9 +302,8 @@ macro_rules! from_ctx_float_impl { } impl<'a> TryFromCtx<'a, Endian> for $typ where $typ: FromCtx { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a [u8], le: Endian) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], le: Endian) -> result::Result<(Self, usize), Self::Error> { if $size > src.len () { Err(error::Error::TooBig{size: $size, len: src.len()}) } else { @@ -359,9 +347,8 @@ macro_rules! into_ctx_float_impl { } impl TryIntoCtx for $typ where $typ: IntoCtx { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { if $size > dst.len () { Err(error::Error::TooBig{size: $size, len: dst.len()}) } else { @@ -372,9 +359,8 @@ macro_rules! into_ctx_float_impl { } impl<'a> TryIntoCtx for &'a $typ { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { (*self).try_into_ctx(dst, le) } } @@ -386,10 +372,9 @@ into_ctx_float_impl!(f64, 8); impl<'a> TryFromCtx<'a, StrCtx> for &'a str { type Error = error::Error; - type Size = usize; #[inline] /// Read a `&str` from `src` using `delimiter` - fn try_from_ctx(src: &'a [u8], ctx: StrCtx) -> Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], ctx: StrCtx) -> Result<(Self, usize), Self::Error> { let len = match ctx { StrCtx::Length(len) => len, StrCtx::Delimiter(delimiter) => src.iter().take_while(|c| **c != delimiter).count(), @@ -418,9 +403,8 @@ impl<'a> TryFromCtx<'a, StrCtx> for &'a str { impl<'a, T> TryFromCtx<'a, StrCtx, T> for &'a str where T: AsRef<[u8]> { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a T, ctx: StrCtx) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a T, ctx: StrCtx) -> result::Result<(Self, usize), Self::Error> { let src = src.as_ref(); TryFromCtx::try_from_ctx(src, ctx) } @@ -428,9 +412,8 @@ impl<'a, T> TryFromCtx<'a, StrCtx, T> for &'a str where T: AsRef<[u8]> { impl<'a> TryIntoCtx for &'a [u8] { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { let src_len = self.len() as isize; let dst_len = dst.len() as isize; // if src_len < 0 || dst_len < 0 || offset < 0 { @@ -448,9 +431,8 @@ impl<'a> TryIntoCtx for &'a [u8] { // TODO: make TryIntoCtx use StrCtx for awesomeness impl<'a> TryIntoCtx for &'a str { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { let bytes = self.as_bytes(); TryIntoCtx::try_into_ctx(bytes, dst, ()) } @@ -460,7 +442,6 @@ impl<'a> TryIntoCtx for &'a str { macro_rules! sizeof_impl { ($ty:ty) => { impl SizeWith for $ty { - type Units = usize; #[inline] fn size_with(_ctx: &Endian) -> usize { size_of::<$ty>() @@ -504,9 +485,8 @@ impl FromCtx for usize { impl<'a> TryFromCtx<'a, Endian> for usize where usize: FromCtx { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a [u8], le: Endian) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], le: Endian) -> result::Result<(Self, usize), Self::Error> { let size = ::core::mem::size_of::(); if size > src.len () { Err(error::Error::TooBig{size, len: src.len()}) @@ -518,9 +498,8 @@ impl<'a> TryFromCtx<'a, Endian> for usize where usize: FromCtx { impl<'a> TryFromCtx<'a, usize> for &'a[u8] { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a [u8], size: usize) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], size: usize) -> result::Result<(Self, usize), Self::Error> { if size > src.len () { Err(error::Error::TooBig{size, len: src.len()}) } else { @@ -544,9 +523,8 @@ impl IntoCtx for usize { impl TryIntoCtx for usize where usize: IntoCtx { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { let size = ::core::mem::size_of::(); if size > dst.len() { Err(error::Error::TooBig{size, len: dst.len()}) @@ -560,9 +538,8 @@ impl TryIntoCtx for usize where usize: IntoCtx { #[cfg(feature = "std")] impl<'a> TryFromCtx<'a> for &'a CStr { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, usize), Self::Error> { let null_byte = match src.iter().position(|b| *b == 0) { Some(ix) => ix, None => return Err(error::Error::BadInput { @@ -579,9 +556,8 @@ impl<'a> TryFromCtx<'a> for &'a CStr { #[cfg(feature = "std")] impl<'a> TryFromCtx<'a> for CString { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, usize), Self::Error> { let (raw, bytes_read) = <&CStr as TryFromCtx>::try_from_ctx(src, _ctx)?; Ok((raw.to_owned(), bytes_read)) } @@ -590,9 +566,8 @@ impl<'a> TryFromCtx<'a> for CString { #[cfg(feature = "std")] impl<'a> TryIntoCtx for &'a CStr { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { let data = self.to_bytes_with_nul(); if dst.len() < data.len() { @@ -613,9 +588,8 @@ impl<'a> TryIntoCtx for &'a CStr { #[cfg(feature = "std")] impl TryIntoCtx for CString { type Error = error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { + fn try_into_ctx(self, dst: &mut [u8], _ctx: ()) -> error::Result { self.as_c_str().try_into_ctx(dst, ()) } } diff --git a/src/error.rs b/src/error.rs index dd49afd..c297153 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,12 +8,12 @@ use std::error; #[derive(Debug)] /// A custom Scroll error -pub enum Error { +pub enum Error { /// The type you tried to read was too big - TooBig { size: T, len: T }, + TooBig { size: usize, len: usize }, /// The requested offset to read/write at is invalid - BadOffset(T), - BadInput{ size: T, msg: &'static str }, + BadOffset(usize), + BadInput{ size: usize, msg: &'static str }, #[cfg(feature = "std")] /// A custom Scroll error for reporting messages to clients Custom(String), diff --git a/src/leb128.rs b/src/leb128.rs index 78e31ce..121be44 100644 --- a/src/leb128.rs +++ b/src/leb128.rs @@ -93,9 +93,8 @@ fn mask_continuation(byte: u8) -> u8 { impl<'a> TryFromCtx<'a> for Uleb128 { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, usize), Self::Error> { use pread::Pread; let mut result = 0; let mut shift = 0; @@ -122,9 +121,8 @@ impl<'a> TryFromCtx<'a> for Uleb128 { impl<'a> TryFromCtx<'a> for Sleb128 { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, usize), Self::Error> { let o = 0; let offset = &mut 0; let mut result = 0; diff --git a/src/lesser.rs b/src/lesser.rs index 60b48f1..f7283d4 100644 --- a/src/lesser.rs +++ b/src/lesser.rs @@ -26,9 +26,8 @@ use ctx::{FromCtx, IntoCtx, SizeWith}; /// } /// /// impl ctx::SizeWith for Foo { -/// type Units = usize; /// // our parsing context doesn't influence our size -/// fn size_with(_: &scroll::Endian) -> Self::Units { +/// fn size_with(_: &scroll::Endian) -> usize { /// ::std::mem::size_of::() /// } /// } @@ -69,7 +68,7 @@ pub trait IOread : Read /// assert_eq!(0xefbe, beef); /// ``` #[inline] - fn ioread + SizeWith>(&mut self) -> Result where Ctx: Default { + fn ioread + SizeWith>(&mut self) -> Result where Ctx: Default { let ctx = Ctx::default(); self.ioread_with(ctx) } @@ -95,7 +94,7 @@ pub trait IOread : Read /// assert_eq!(0xfeeddead, feeddead); /// ``` #[inline] - fn ioread_with + SizeWith>(&mut self, ctx: Ctx) -> Result { + fn ioread_with + SizeWith>(&mut self, ctx: Ctx) -> Result { let mut scratch = [0u8; 256]; let size = N::size_with(&ctx); let mut buf = &mut scratch[0..size]; @@ -133,7 +132,7 @@ pub trait IOwrite: Write /// assert_eq!(bytes.into_inner(), [0xde, 0xad, 0xbe, 0xef,]); /// ``` #[inline] - fn iowrite + IntoCtx>(&mut self, n: N) -> Result<()> where Ctx: Default { + fn iowrite + IntoCtx>(&mut self, n: N) -> Result<()> where Ctx: Default { let ctx = Ctx::default(); self.iowrite_with(n, ctx) } @@ -155,7 +154,7 @@ pub trait IOwrite: Write /// assert_eq!(cursor.into_inner(), [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0x0]); /// ``` #[inline] - fn iowrite_with + IntoCtx>(&mut self, n: N, ctx: Ctx) -> Result<()> { + fn iowrite_with + IntoCtx>(&mut self, n: N, ctx: Ctx) -> Result<()> { let mut buf = [0u8; 256]; let size = N::size_with(&ctx); let buf = &mut buf[0..size]; diff --git a/src/lib.rs b/src/lib.rs index 8441132..d01c222 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,10 +117,9 @@ //! // note the lifetime specified here //! impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> { //! type Error = scroll::Error; -//! type Size = usize; //! // and the lifetime annotation on `&'a [u8]` here //! fn try_from_ctx (src: &'a [u8], endian: Endian) -//! -> Result<(Self, Self::Size), Self::Error> { +//! -> Result<(Self, usize), Self::Error> { //! let offset = &mut 0; //! let name = src.gread::<&str>(offset)?; //! let id = src.gread_with(offset, endian)?; @@ -350,8 +349,7 @@ mod tests { impl super::ctx::TryIntoCtx for Foo { type Error = ExternalError; - type Size = usize; - fn try_into_ctx(self, this: &mut [u8], le: super::Endian) -> Result { + fn try_into_ctx(self, this: &mut [u8], le: super::Endian) -> Result { use super::Pwrite; if this.len() < 2 { return Err((ExternalError {}).into()) } this.pwrite_with(self.0, 0, le)?; @@ -361,8 +359,7 @@ mod tests { impl<'a> super::ctx::TryFromCtx<'a, super::Endian> for Foo { type Error = ExternalError; - type Size = usize; - fn try_from_ctx(this: &'a [u8], le: super::Endian) -> Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(this: &'a [u8], le: super::Endian) -> Result<(Self, usize), Self::Error> { use super::Pread; if this.len() > 2 { return Err((ExternalError {}).into()) } let n = this.pread_with(0, le)?; diff --git a/src/pread.rs b/src/pread.rs index c9d17d0..69459f2 100644 --- a/src/pread.rs +++ b/src/pread.rs @@ -1,5 +1,5 @@ use core::result; -use core::ops::{Index, RangeFrom, Add, AddAssign}; +use core::ops::{Index, RangeFrom}; use ctx::{TryFromCtx, MeasureWith}; use error; @@ -18,8 +18,7 @@ use error; /// /// impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for Foo { /// type Error = scroll::Error; -/// type Size = usize; -/// fn try_from_ctx(this: &'a [u8], le: scroll::Endian) -> Result<(Self, Self::Size), Self::Error> { +/// fn try_from_ctx(this: &'a [u8], le: scroll::Endian) -> Result<(Self, usize), Self::Error> { /// if this.len() < 2 { return Err((scroll::Error::Custom("whatever".to_string())).into()) } /// let n = this.pread_with(0, le)?; /// Ok((Foo(n), 2)) @@ -68,8 +67,7 @@ use error; /// /// impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for Foo { /// type Error = ExternalError; -/// type Size = usize; -/// fn try_from_ctx(this: &'a [u8], le: scroll::Endian) -> Result<(Self, Self::Size), Self::Error> { +/// fn try_from_ctx(this: &'a [u8], le: scroll::Endian) -> Result<(Self, usize), Self::Error> { /// if this.len() <= 2 { return Err((ExternalError {}).into()) } /// let offset = &mut 0; /// let n = this.gread_with(offset, le)?; @@ -80,11 +78,10 @@ use error; /// let bytes: [u8; 4] = [0xde, 0xad, 0, 0]; /// let foo: Result = bytes.pread(0); /// ``` -pub trait Pread : Index + Index> + MeasureWith +pub trait Pread : Index + Index> + MeasureWith where Ctx: Copy, - I: Add + Copy + PartialOrd, - E: From>, + E: From, { #[inline] /// Reads a value from `self` at `offset` with a default `Ctx`. For the primitive numeric values, this will read at the machine's endianness. @@ -93,7 +90,7 @@ pub trait Pread : Index + Index> + MeasureWit /// use scroll::Pread; /// let bytes = [0x7fu8; 0x01]; /// let byte = bytes.pread::(0).unwrap(); - fn pread<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E, Size = I>>(&'a self, offset: I) -> result::Result where >>::Output: 'a, Ctx: Default { + fn pread<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E>>(&'a self, offset: usize) -> result::Result where >>::Output: 'a, Ctx: Default { self.pread_with(offset, Ctx::default()) } #[inline] @@ -104,7 +101,7 @@ pub trait Pread : Index + Index> + MeasureWit /// let bytes: [u8; 2] = [0xde, 0xad]; /// let dead: u16 = bytes.pread_with(0, scroll::BE).unwrap(); /// assert_eq!(dead, 0xdeadu16); - fn pread_with<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E, Size = I>>(&'a self, offset: I, ctx: Ctx) -> result::Result where >>::Output: 'a { + fn pread_with<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E>>(&'a self, offset: usize, ctx: Ctx) -> result::Result where >>::Output: 'a { let len = self.measure_with(&ctx); if offset >= len { return Err(error::Error::BadOffset(offset).into()) @@ -120,7 +117,7 @@ pub trait Pread : Index + Index> + MeasureWit /// let bytes = [0x7fu8; 0x01]; /// let byte = bytes.gread::(offset).unwrap(); /// assert_eq!(*offset, 1); - fn gread<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E, Size = I>>(&'a self, offset: &mut I) -> result::Result where I: AddAssign, Ctx: Default, >>::Output: 'a { + fn gread<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E>>(&'a self, offset: &mut usize) -> result::Result where Ctx: Default, >>::Output: 'a { let ctx = Ctx::default(); self.gread_with(offset, ctx) } @@ -134,10 +131,10 @@ pub trait Pread : Index + Index> + MeasureWit /// assert_eq!(dead, 0xdeadu16); /// assert_eq!(*offset, 2); #[inline] - fn gread_with<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E, Size = I>> - (&'a self, offset: &mut I, ctx: Ctx) -> + fn gread_with<'a, N: TryFromCtx<'a, Ctx, >>::Output, Error = E>> + (&'a self, offset: &mut usize, ctx: Ctx) -> result::Result - where I: AddAssign, >>::Output: 'a + where >>::Output: 'a { let o = *offset; // self.pread_with(o, ctx).and_then(|(n, size)| { @@ -165,12 +162,11 @@ pub trait Pread : Index + Index> + MeasureWit /// assert_eq!(&bytes, &bytes_from); /// assert_eq!(*offset, 2); #[inline] - fn gread_inout<'a, N>(&'a self, offset: &mut I, inout: &mut [N]) -> result::Result<(), E> + fn gread_inout<'a, N>(&'a self, offset: &mut usize, inout: &mut [N]) -> result::Result<(), E> where - I: AddAssign, - N: TryFromCtx<'a, Ctx, >>::Output, Error = E, Size = I>, + N: TryFromCtx<'a, Ctx, >>::Output, Error = E>, Ctx: Default, - >>::Output: 'a + >>::Output: 'a { for i in inout.iter_mut() { *i = self.gread(offset)?; @@ -189,11 +185,10 @@ pub trait Pread : Index + Index> + MeasureWit /// assert_eq!(&bytes, &bytes_from); /// assert_eq!(*offset, 2); #[inline] - fn gread_inout_with<'a, N>(&'a self, offset: &mut I, inout: &mut [N], ctx: Ctx) -> result::Result<(), E> + fn gread_inout_with<'a, N>(&'a self, offset: &mut usize, inout: &mut [N], ctx: Ctx) -> result::Result<(), E> where - I: AddAssign, - N: TryFromCtx<'a, Ctx, >>::Output, Error = E, Size = I>, - >>::Output: 'a + N: TryFromCtx<'a, Ctx, >>::Output, Error = E>, + >>::Output: 'a { for i in inout.iter_mut() { *i = self.gread_with(offset, ctx)?; @@ -203,7 +198,6 @@ pub trait Pread : Index + Index> + MeasureWit } impl>, - R: ?Sized + Index + Index> + MeasureWith> - Pread for R {} + E: From, + R: ?Sized + Index + Index> + MeasureWith> + Pread for R {} diff --git a/src/pwrite.rs b/src/pwrite.rs index 040d197..51e95b0 100644 --- a/src/pwrite.rs +++ b/src/pwrite.rs @@ -1,5 +1,5 @@ use core::result; -use core::ops::{Index, IndexMut, RangeFrom, Add, AddAssign}; +use core::ops::{Index, IndexMut, RangeFrom}; use ctx::{TryIntoCtx, MeasureWith}; use error; @@ -17,9 +17,8 @@ use error; /// impl ctx::TryIntoCtx for Foo { /// // you can use your own error here too, but you will then need to specify it in fn generic parameters /// type Error = scroll::Error; -/// type Size = usize; /// // you can write using your own context too... see `leb128.rs` -/// fn try_into_ctx(self, this: &mut [u8], le: Endian) -> Result { +/// fn try_into_ctx(self, this: &mut [u8], le: Endian) -> Result { /// if this.len() < 2 { return Err((scroll::Error::Custom("whatever".to_string())).into()) } /// this.pwrite_with(self.0, 0, le)?; /// Ok(2) @@ -30,13 +29,12 @@ use error; /// let mut bytes: [u8; 4] = [0, 0, 0, 0]; /// bytes.pwrite_with(Foo(0x7f), 1, LE).unwrap(); /// -pub trait Pwrite : Index + IndexMut> + MeasureWith +pub trait Pwrite : Index + IndexMut> + MeasureWith where Ctx: Copy, - I: Add + Copy + PartialOrd, - E: From>, + E: From, { - fn pwrite>>::Output, Error = E, Size = I>>(&mut self, n: N, offset: I) -> result::Result where Ctx: Default { + fn pwrite>>::Output, Error = E>>(&mut self, n: N, offset: usize) -> result::Result where Ctx: Default { self.pwrite_with(n, offset, Ctx::default()) } /// Write `N` at offset `I` with context `Ctx` @@ -46,7 +44,7 @@ pub trait Pwrite : Index + IndexMut> + Measur /// let mut bytes: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0]; /// bytes.pwrite_with::(0xbeefbeef, 0, LE).unwrap(); /// assert_eq!(bytes.pread_with::(0, LE).unwrap(), 0xbeefbeef); - fn pwrite_with>>::Output, Error = E, Size = I>>(&mut self, n: N, offset: I, ctx: Ctx) -> result::Result { + fn pwrite_with>>::Output, Error = E>>(&mut self, n: N, offset: usize, ctx: Ctx) -> result::Result { let len = self.measure_with(&ctx); if offset >= len { return Err(error::Error::BadOffset(offset).into()) @@ -56,17 +54,14 @@ pub trait Pwrite : Index + IndexMut> + Measur } /// Write `n` into `self` at `offset`, with a default `Ctx`. Updates the offset. #[inline] - fn gwrite>>::Output, Error = E, Size = I>>(&mut self, n: N, offset: &mut I) -> result::Result where - I: AddAssign, + fn gwrite>>::Output, Error = E>>(&mut self, n: N, offset: &mut usize) -> result::Result where Ctx: Default { let ctx = Ctx::default(); self.gwrite_with(n, offset, ctx) } /// Write `n` into `self` at `offset`, with the `ctx`. Updates the offset. #[inline] - fn gwrite_with>>::Output, Error = E, Size = I>>(&mut self, n: N, offset: &mut I, ctx: Ctx) -> result::Result - where I: AddAssign, - { + fn gwrite_with>>::Output, Error = E>>(&mut self, n: N, offset: &mut usize, ctx: Ctx) -> result::Result { let o = *offset; match self.pwrite_with(n, o, ctx) { Ok(size) => { @@ -79,7 +74,6 @@ pub trait Pwrite : Index + IndexMut> + Measur } impl>, - R: ?Sized + Index + IndexMut> + MeasureWith> - Pwrite for R {} + E: From, + R: ?Sized + Index + IndexMut> + MeasureWith> + Pwrite for R {} diff --git a/tests/api.rs b/tests/api.rs index b29a94a..7c79dbc 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -35,7 +35,6 @@ impl<'a> Section<'a> { } impl<'a> ctx::SizeWith for Section<'a> { - type Units = usize; fn size_with(_ctx: &()) -> usize { 4 } @@ -61,8 +60,7 @@ pub struct Section32 { impl<'a> ctx::TryFromCtx<'a, ()> for Section<'a> { type Error = scroll::Error; - type Size = usize; - fn try_from_ctx(_bytes: &'a [u8], _ctx: ()) -> ::std::result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(_bytes: &'a [u8], _ctx: ()) -> ::std::result::Result<(Self, usize), Self::Error> { //let section = Section::from_ctx(bytes, bytes.pread_with::(offset, ctx)?); let section = unsafe { ::std::mem::uninitialized::
()}; Ok((section, ::std::mem::size_of::
())) @@ -106,7 +104,6 @@ impl<'a> Segment<'a> { } impl<'a> ctx::SizeWith for Segment<'a> { - type Units = usize; fn size_with(_ctx: &()) -> usize { 4 } @@ -186,8 +183,7 @@ impl scroll::ctx::FromCtx for Foo { } impl scroll::ctx::SizeWith for Foo { - type Units = usize; - fn size_with(_: &scroll::Endian) -> Self::Units { + fn size_with(_: &scroll::Endian) -> usize { ::std::mem::size_of::() } }