pub struct Reader<'a> { /* private fields */ }Expand description
An incremental reader from a byte slice.
The main functions of interest are u32_le and the like, which read the relevant
primitives from the slice. In these docs, they are abbreviated to a single function for
simplicity.
Supported primitives are u8..=u128, i8..=i128, f32, f64.
The functions are suffixed with either _le or _be, for endianness. To use unsuffixed
versions, import either the Le or Be trait.
Cloning a Reader is cheap, but it does not implement Copy for similar reasons as
Range.
Implementations§
Source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the total length of the input.
Note that this is the total length, which does not change. See
remaining for the number of bytes left that can be read.
Sourcepub fn data(&self) -> &'a [u8] ⓘ
pub fn data(&self) -> &'a [u8] ⓘ
Returns the data being read from.
This is the full slice; for only the remainder, see remaining.
Sourcepub fn slice(&mut self, len: usize) -> Result<&'a [u8]>
pub fn slice(&mut self, len: usize) -> Result<&'a [u8]>
Reads a slice of data from the input. No copying is done.
Returns an error if there is not enough data left, in which case the read position is unchanged.
Sourcepub fn array<const N: usize>(&mut self) -> Result<[u8; N]>
pub fn array<const N: usize>(&mut self) -> Result<[u8; N]>
Reads a fixed-size slice of data from the input.
Handles errors identically to slice.
Sourcepub fn read_into(&mut self, buf: &mut [u8]) -> Result<()>
pub fn read_into(&mut self, buf: &mut [u8]) -> Result<()>
Reads a slice of data into a preexisting buffer.
Handles errors identically to slice.
Sourcepub fn seek(&mut self, pos: usize) -> Result<()>
pub fn seek(&mut self, pos: usize) -> Result<()>
Sets the read position.
Returns an error if the position is out of bounds.
See at for a version that returns a copy.
Sourcepub fn at(&self, pos: usize) -> Result<Self>
pub fn at(&self, pos: usize) -> Result<Self>
Returns a copy of the reader at the specified position.
See also ptrN for a shorthand for the common pattern of
f.clone().at(f.u32()? as usize)?.
Sourcepub fn check(&mut self, v: &[u8]) -> Result<()>
pub fn check(&mut self, v: &[u8]) -> Result<()>
Reads a number of bytes and returns an error if they are not as expected.
If it does not match, the read position is not affected.
Sourcepub fn check_T(&mut self, v: T) -> Result<()>
pub fn check_T(&mut self, v: T) -> Result<()>
Read a primitive from the input, giving an error if it is not as expected.
If it not match, the read position is not affected.
Sourcepub fn ptrN(&mut self) -> Result<Self>
pub fn ptrN(&mut self) -> Result<Self>
Read a uN primitive from the input, and return a new Reader at that position.
This is a shorthand for the common pattern of f.clone().at(f.uN()? as usize)?.
Note that no checking is made that the value actually fits inside a usize;
the higher bits are simply discarded. This is primarily relevant with ptr128,
and requires humongous amounts of memory to exhibit issues.
Sourcepub fn align(&mut self, size: usize) -> Result<&'a [u8]>
pub fn align(&mut self, size: usize) -> Result<&'a [u8]>
Rounds the read position up to the next multiple of size, returning the skipped data.
Sourcepub fn align_zeroed(&mut self, size: usize) -> Result<()>
pub fn align_zeroed(&mut self, size: usize) -> Result<()>
Rounds the read position up to the next multiple of size, returning an error if the skipped bytes are nonzero.