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>
impl<'a> SliceReader<'a>
pub fn new(buf: &'a [u8]) -> Self
pub fn offset(&self) -> usize
pub fn remaining(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn read_u8(&mut self) -> Result<u8, ParseError>
Sourcepub fn read_array<const N: usize>(&mut self) -> Result<[u8; N], ParseError>
pub fn read_array<const N: usize>(&mut self) -> Result<[u8; N], ParseError>
Read a fixed-size array.
pub fn read_u16_be(&mut self) -> Result<u16, ParseError>
pub fn read_u32_be(&mut self) -> Result<u32, ParseError>
pub fn read_u64_be(&mut self) -> Result<u64, ParseError>
pub fn read_u128_be(&mut self) -> Result<u128, ParseError>
pub fn read_i32_be(&mut self) -> Result<i32, ParseError>
pub fn read_i64_be(&mut self) -> Result<i64, ParseError>
pub fn read_f32_be(&mut self) -> Result<f32, ParseError>
pub fn read_f64_be(&mut self) -> Result<f64, ParseError>
Sourcepub fn read_bytes(&mut self, len: usize) -> Result<&'a [u8], ParseError>
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().
Sourcepub fn take_slice(&mut self, len: usize) -> Result<SliceReader<'a>, ParseError>
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.
pub fn peek_u8(&mut self) -> Result<u8, ParseError>
pub fn peek_u16_be(&self) -> Result<u16, ParseError>
pub fn peek_u32_be(&self) -> Result<u32, ParseError>
Sourcepub fn peek_array<const N: usize>(&self) -> Result<[u8; N], ParseError>
pub fn peek_array<const N: usize>(&self) -> Result<[u8; N], ParseError>
Peek a fixed-size array without advancing. Takes &self.
Sourcepub fn read_padded<const N: usize>(
&mut self,
len: usize,
) -> Result<[u8; N], ParseError>
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].
Sourcepub fn read_unsigned32_be(&mut self, len: usize) -> Result<u32, ParseError>
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.
Sourcepub fn read_unsigned64_be(&mut self, len: usize) -> Result<u64, ParseError>
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.
Sourcepub fn read_signed32_be(&mut self, len: usize) -> Result<i32, ParseError>
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.
Sourcepub fn read_signed64_be(&mut self, len: usize) -> Result<i64, ParseError>
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>
impl<'a> Clone for SliceReader<'a>
Source§fn clone(&self) -> SliceReader<'a>
fn clone(&self) -> SliceReader<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more