Reader

Struct Reader 

Source
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>

Source

pub fn new(data: &'a [u8]) -> Reader<'a>

Constructs a new Reader.

Source

pub fn pos(&self) -> usize

Returns the read position of the reader.

To change the position, use seek or at.

Source

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.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no more bytes left to read.

Note that unlike slices, this is based on remaining, not len.

Source

pub fn remaining(&self) -> &'a [u8]

Returns the remaining data in the buffer.

Source

pub fn data(&self) -> &'a [u8]

Returns the data being read from.

This is the full slice; for only the remainder, see remaining.

Source

pub fn T(&mut self) -> Result<T>

Read a primitive from the input.

Source

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.

Source

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.

Source

pub fn read_into(&mut self, buf: &mut [u8]) -> Result<()>

Reads a slice of data into a preexisting buffer.

Handles errors identically to slice.

Source

pub fn cstr(&mut self) -> Result<&'a CStr>

Reads data to and including the next null byte.

Source

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.

Source

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)?.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn dump(&self) -> Dump<'a>

Creates a hexdump of the data at the current position.

Trait Implementations§

Source§

impl<'a> Clone for Reader<'a>

Source§

fn clone(&self) -> Reader<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Reader<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Be for Reader<'a>

Source§

impl<'a> Le for Reader<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.