Struct gospel::read::Reader

source ·
pub struct Reader<'a> { /* private fields */ }
Expand description

An incremental reader from a byte slice.

Cloning this type 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 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 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 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 align(&mut self, size: usize) -> Result<&'a [u8]>

Rounds the read position up to the next multiple of size.

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§

impl Reader<'_>

Functions for reading primitives from the stream. The underlying functions are hidden from the docs for brevity.

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.

source

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

Read a primitive from the input.

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.

Trait Implementations§

source§

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

source§

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

Returns a copy 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> 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.