Trait scroll::Gread
[−]
[src]
pub trait Gread<Ctx = Endian, E = Error, I = usize, TryCtx = (I, Ctx), SliceCtx = (I, I, Ctx)>: Pread<Ctx, E, I, TryCtx, SliceCtx> + TryOffsetWith<Ctx, E, I> where
Ctx: Copy + Default + Debug,
I: AddAssign + Copy + Add + Default + Debug,
E: Debug,
TryCtx: Copy + Default + Debug,
SliceCtx: Copy + Default + Debug, { fn gread_unsafe<'a, N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>>(
&'a self,
offset: &mut I,
ctx: Ctx
) -> N { ... } fn gread<'a, N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>>(
&'a self,
offset: &mut I
) -> Result<N, E> { ... } fn gread_with<'a, N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>>(
&'a self,
offset: &mut I,
ctx: Ctx
) -> Result<N, E> { ... } fn gread_slice<N: ?Sized>(&self, offset: &mut I, count: I) -> Result<&N, E>
where
N: TryRefFromCtx<SliceCtx, Error = E>, { ... } fn gread_inout<'a, N>(
&'a self,
offset: &mut I,
inout: &mut [N]
) -> Result<(), E>
where
N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>, { ... } fn gread_inout_with<'a, N>(
&'a self,
offset: &mut I,
inout: &mut [N],
ctx: Ctx
) -> Result<(), E>
where
N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>, { ... } }
The Greater Read (Gread
) reads a value at a mutable offset, and increments the offset by the size of the interpreted value.
Gread
implements an immutable Self
, mutable
reference offset incrementor which uses Pread
as its base.
If you are writing a custom Gread
interface,
you should only need to implement Pread
for a particular
Ctx
, Error
, Index
target, and implement TryOffsetWith
to explain to the trait how it should increment the mutable offset,
and then a simple blanket impl Gread<I, E, Ctx> for YourType
, etc.
Provided Methods
fn gread_unsafe<'a, N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>>(
&'a self,
offset: &mut I,
ctx: Ctx
) -> N
&'a self,
offset: &mut I,
ctx: Ctx
) -> N
Reads and unwraps a value from self
at offset
with the given ctx
. NB: this can panic if the offset is bad, or whatever error this operates on is "thrown".
For the primitive numeric values, this will read at the machine's endianness. Updates the offset
fn gread<'a, N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>>(
&'a self,
offset: &mut I
) -> Result<N, E>
&'a self,
offset: &mut I
) -> Result<N, E>
Reads a value from self
at offset
with a default Ctx
. For the primitive numeric values, this will read at the machine's endianness. Updates the offset
Example
use scroll::Gread; let offset = &mut 0; let bytes = [0x7fu8; 0x01]; let byte = bytes.gread::<u8>(offset).unwrap(); assert_eq!(*offset, 1);
fn gread_with<'a, N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>>(
&'a self,
offset: &mut I,
ctx: Ctx
) -> Result<N, E>
&'a self,
offset: &mut I,
ctx: Ctx
) -> Result<N, E>
Reads a value from self
at offset
with the given ctx
, and updates the offset.
Example
use scroll::Gread; let offset = &mut 0; let bytes: [u8; 2] = [0xde, 0xad]; let dead: u16 = bytes.gread_with(offset, scroll::BE).unwrap(); assert_eq!(dead, 0xdeadu16); assert_eq!(*offset, 2);
fn gread_slice<N: ?Sized>(&self, offset: &mut I, count: I) -> Result<&N, E> where
N: TryRefFromCtx<SliceCtx, Error = E>,
N: TryRefFromCtx<SliceCtx, Error = E>,
Slices an N
from self
at offset
up to count
times, and updates the offset.
Example
use scroll::Gread; let bytes: [u8; 2] = [0x48, 0x49]; let offset = &mut 0; let hi: &str = bytes.gread_slice(offset, 2).unwrap(); assert_eq!(hi, "HI"); assert_eq!(*offset, 2);
fn gread_inout<'a, N>(
&'a self,
offset: &mut I,
inout: &mut [N]
) -> Result<(), E> where
N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>,
&'a self,
offset: &mut I,
inout: &mut [N]
) -> Result<(), E> where
N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>,
Trys to write inout.len()
N
s into inout
from Self
starting at offset
, using the default context for N
, and updates the offset.
Example
use scroll::Gread; let mut bytes: Vec<u8> = vec![0, 0]; let offset = &mut 0; let bytes_from: [u8; 2] = [0x48, 0x49]; bytes_from.gread_inout(offset, &mut bytes).unwrap(); assert_eq!(&bytes, &bytes_from); assert_eq!(*offset, 2);
fn gread_inout_with<'a, N>(
&'a self,
offset: &mut I,
inout: &mut [N],
ctx: Ctx
) -> Result<(), E> where
N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>,
&'a self,
offset: &mut I,
inout: &mut [N],
ctx: Ctx
) -> Result<(), E> where
N: SizeWith<Ctx, Units = I> + TryFromCtx<'a, TryCtx, Error = E>,
Trys to write inout.len()
N
s into inout
from Self
starting at offset
, using the context ctx
Example
use scroll::{ctx, Gread}; let mut bytes: Vec<u8> = vec![0, 0]; let offset = &mut 0; let bytes_from: [u8; 2] = [0x48, 0x49]; bytes_from.gread_inout_with(offset, &mut bytes, ctx::CTX).unwrap(); assert_eq!(&bytes, &bytes_from); assert_eq!(*offset, 2);