Skip to main content

ByteReader

Struct ByteReader 

Source
pub struct ByteReader<'a> { /* private fields */ }
Expand description

Reader over bytes, tracking a cursor and the payload name used in errors.

Implementations§

Source§

impl<'a> ByteReader<'a>

Source

pub fn new(bytes: &'a [u8], label: &'static str) -> Self

A reader positioned at the start of bytes. label names the payload kind in every error this reader produces.

Source

pub fn open_payload( bytes: &'a [u8], magic: u32, header_bytes: usize, label: &'static str, ) -> Result<Self, String>

Open a tagged payload: prove it is long enough to hold a header_bytes header and that it opens with magic, then position the reader just past the magic. Lets a decoder report a short buffer once rather than once per header field.

Source

pub fn position(&self) -> usize

Current byte offset.

Source

pub fn remaining(&self) -> usize

Bytes left between the cursor and the end of the buffer.

Source

pub fn is_empty(&self) -> bool

Whether no bytes remain.

Source

pub fn len(&self) -> usize

Total buffer length, including bytes already consumed.

Source

pub fn take(&mut self, n: usize) -> Result<&'a [u8], String>

Consume n bytes and return them, or report where the buffer ran out.

Source

pub fn array<const N: usize>(&mut self) -> Result<[u8; N], String>

Consume exactly N bytes as an owned array, ready for from_le_bytes.

Source

pub fn u8(&mut self) -> Result<u8, String>

Read one byte.

Source

pub fn u16(&mut self) -> Result<u16, String>

Read a little-endian u16.

Source

pub fn u32(&mut self) -> Result<u32, String>

Read a little-endian u32.

Source

pub fn u64(&mut self) -> Result<u64, String>

Read a little-endian u64.

Source

pub fn i32(&mut self) -> Result<i32, String>

Read a little-endian i32.

Source

pub fn f32(&mut self) -> Result<f32, String>

Read a little-endian f32.

Source

pub fn skip(&mut self, n: usize) -> Result<(), String>

Advance past n bytes without returning them.

Source

pub fn seek(&mut self, pos: usize) -> Result<(), String>

Move the cursor to an absolute offset, which must lie within the buffer.

Source

pub fn peek(&self, magic: &[u8]) -> bool

Whether the bytes at the cursor equal magic, without consuming them.

Source

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

Everything from the cursor to the end, leaving the cursor in place.

Trait Implementations§

Source§

impl<'a> Clone for ByteReader<'a>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for ByteReader<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for ByteReader<'a>

§

impl<'a> RefUnwindSafe for ByteReader<'a>

§

impl<'a> Send for ByteReader<'a>

§

impl<'a> Sync for ByteReader<'a>

§

impl<'a> Unpin for ByteReader<'a>

§

impl<'a> UnsafeUnpin for ByteReader<'a>

§

impl<'a> UnwindSafe for ByteReader<'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 = !

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.