Skip to main content

SliceReader

Struct SliceReader 

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

A zero-copy, forward-only reader over a borrowed byte slice.

SliceReader is Copy, and that is deliberate and load-bearing: copying the reader is a zero-cost checkpoint for speculative parsing / backtracking.

let save = reader;                     // checkpoint: just a pointer + usize
if try_parse(&mut reader).is_err() {
    reader = save;                     // rewind; nothing was consumed
}

offset is the absolute position from the start of the original buffer, so errors and sub-readers report positions that stay meaningful across nested parses.

Implementations§

Source§

impl<'a> SliceReader<'a>

Source

pub fn new(buf: &'a [u8]) -> Self

Source

pub fn offset(&self) -> usize

Source

pub fn remaining(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

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

The remaining, not-yet-read bytes.

Source

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

Source

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

Read a fixed-size array.

Source

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

Source

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

Source

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

Source

pub fn read_u128_be(&mut self) -> Result<u128, ParseError>

Source

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

Source

pub fn read_i64_be(&mut self) -> Result<i64, ParseError>

Source

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

Source

pub fn read_f64_be(&mut self) -> Result<f64, ParseError>

Source

pub fn read_bytes(&mut self, len: usize) -> Result<&'a [u8], ParseError>

Read len bytes as a borrowed slice.

Non-panicking even for an attacker-controlled len: split_at_checked returns None on overrun rather than panicking or overflowing. Callers needing owned, zero-copy data should slice_ref the result off the parent Bytes; callers needing an owned copy can .to_vec().

Source

pub fn take_slice(&mut self, len: usize) -> Result<SliceReader<'a>, ParseError>

Zero-copy sub-reader over the next len bytes, with the correct absolute offset carried into it for nested error reporting.

Source

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

Source

pub fn peek_u16_be(&self) -> Result<u16, ParseError>

Source

pub fn peek_u32_be(&self) -> Result<u32, ParseError>

Source

pub fn peek_array<const N: usize>(&self) -> Result<[u8; N], ParseError>

Peek a fixed-size array without advancing. Takes &self.

Source

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

Read len bytes and left-align them into a zero-padded [u8; N].

Source

pub fn read_unsigned32_be(&mut self, len: usize) -> Result<u32, ParseError>

Read a big-endian unsigned integer carried in len octets, where len may be smaller than the type’s natural width.

This is the reduced-size encoding of IPFIX (RFC 7011 Section 6.2): a value whose abstract type is unsigned32 may travel in 1 to 4 octets, and the octets are the low-order end of the value. Callers wanting a narrower type cast the result down, having already bounded len themselves.

len above 4 cannot be represented and is rejected rather than truncated.

Source

pub fn read_unsigned64_be(&mut self, len: usize) -> Result<u64, ParseError>

Read a big-endian unsigned integer carried in len octets, where len may be smaller than the type’s natural width.

This is the reduced-size encoding of IPFIX (RFC 7011 Section 6.2): a value whose abstract type is unsigned64 may travel in 1 to 8 octets, and the octets are the low-order end of the value. Callers wanting a narrower type cast the result down, having already bounded len themselves.

len above 8 cannot be represented and is rejected rather than truncated.

Source

pub fn read_signed32_be(&mut self, len: usize) -> Result<i32, ParseError>

Read a big-endian two’s-complement signed integer carried in len octets, sign-extending it to the full width.

The 32-bit-capped counterpart of Self::read_signed64_be / signed twin of Self::read_unsigned32_be.

Source

pub fn read_signed64_be(&mut self, len: usize) -> Result<i64, ParseError>

Read a big-endian two’s-complement signed integer carried in len octets, sign-extending it to the full width.

The signed counterpart of Self::read_unsigned64_be. The sign is taken from the top bit of the first octet read, so a value shortened under IPFIX reduced-size encoding keeps its sign.

Trait Implementations§

Source§

impl<'a> Clone for SliceReader<'a>

Source§

fn clone(&self) -> SliceReader<'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> Copy for SliceReader<'a>

Source§

impl<'a> Debug for SliceReader<'a>

Source§

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

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

impl<'a> Eq for SliceReader<'a>

Source§

impl<'a> PartialEq for SliceReader<'a>

Source§

fn eq(&self, other: &SliceReader<'a>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for SliceReader<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for SliceReader<'a>

§

impl<'a> RefUnwindSafe for SliceReader<'a>

§

impl<'a> Send for SliceReader<'a>

§

impl<'a> Sync for SliceReader<'a>

§

impl<'a> Unpin for SliceReader<'a>

§

impl<'a> UnsafeUnpin for SliceReader<'a>

§

impl<'a> UnwindSafe for SliceReader<'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.