ByteReader

Trait ByteReader 

Source
pub trait ByteReader {
Show 17 methods // Required methods fn read_u8(&mut self) -> Result<u8, DeserializationError>; fn peek_u8(&self) -> Result<u8, DeserializationError>; fn read_slice(&mut self, len: usize) -> Result<&[u8], DeserializationError>; fn read_array<const N: usize>( &mut self, ) -> Result<[u8; N], DeserializationError>; fn check_eor(&self, num_bytes: usize) -> Result<(), DeserializationError>; fn has_more_bytes(&self) -> bool; // Provided methods fn max_alloc(&self, _element_size: usize) -> usize { ... } fn read_bool(&mut self) -> Result<bool, DeserializationError> { ... } fn read_u16(&mut self) -> Result<u16, DeserializationError> { ... } fn read_u32(&mut self) -> Result<u32, DeserializationError> { ... } fn read_u64(&mut self) -> Result<u64, DeserializationError> { ... } fn read_u128(&mut self) -> Result<u128, DeserializationError> { ... } fn read_usize(&mut self) -> Result<usize, DeserializationError> { ... } fn read_vec(&mut self, len: usize) -> Result<Vec<u8>, DeserializationError> { ... } fn read_string( &mut self, num_bytes: usize, ) -> Result<String, DeserializationError> { ... } fn read<D>(&mut self) -> Result<D, DeserializationError> where Self: Sized, D: Deserializable { ... } fn read_many_iter<D>( &mut self, num_elements: usize, ) -> Result<ReadManyIter<'_, Self, D>, DeserializationError> where Self: Sized, D: Deserializable { ... }
}
Expand description

Defines how primitive values are to be read from Self.

Whenever data is read from the reader using any of the read_* functions, the reader advances to the next unread byte. If the error occurs, the reader is not rolled back to the state prior to calling any of the function.

Required Methods§

Source

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

Returns a single byte read from self.

§Errors

Returns a DeserializationError error the reader is at EOF.

Source

fn peek_u8(&self) -> Result<u8, DeserializationError>

Returns the next byte to be read from self without advancing the reader to the next byte.

§Errors

Returns a DeserializationError error the reader is at EOF.

Source

fn read_slice(&mut self, len: usize) -> Result<&[u8], DeserializationError>

Returns a slice of bytes of the specified length read from self.

§Errors

Returns a DeserializationError if a slice of the specified length could not be read from self.

Source

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

Returns a byte array of length N read from self.

§Errors

Returns a DeserializationError if an array of the specified length could not be read from self.

Source

fn check_eor(&self, num_bytes: usize) -> Result<(), DeserializationError>

Checks if it is possible to read at least num_bytes bytes from this ByteReader

§Errors

Returns an error if, when reading the requested number of bytes, we go beyond the the data available in the reader.

Source

fn has_more_bytes(&self) -> bool

Returns true if there are more bytes left to be read from self.

Provided Methods§

Source

fn max_alloc(&self, _element_size: usize) -> usize

Returns the maximum number of elements that can be safely allocated, given each element occupies element_size bytes when serialized.

This can be used by callers to pre-validate collection lengths before iterating, preventing denial-of-service attacks from malicious length prefixes that claim billions of elements.

The default implementation returns usize::MAX, meaning no limit is enforced. BudgetedReader overrides this to return remaining_budget / element_size, providing tight, adaptive limits based on the caller’s budget.

§Arguments
  • element_size - The serialized size of one element, from Deserializable::min_serialized_size. Defaults to size_of::<D>() but can be overridden for types where serialized size differs from in-memory size.
Source

fn read_bool(&mut self) -> Result<bool, DeserializationError>

Returns a boolean value read from self consuming 1 byte from the reader.

§Errors

Returns a DeserializationError if a u16 value could not be read from self.

Source

fn read_u16(&mut self) -> Result<u16, DeserializationError>

Returns a u16 value read from self in little-endian byte order.

§Errors

Returns a DeserializationError if a u16 value could not be read from self.

Source

fn read_u32(&mut self) -> Result<u32, DeserializationError>

Returns a u32 value read from self in little-endian byte order.

§Errors

Returns a DeserializationError if a u32 value could not be read from self.

Source

fn read_u64(&mut self) -> Result<u64, DeserializationError>

Returns a u64 value read from self in little-endian byte order.

§Errors

Returns a DeserializationError if a u64 value could not be read from self.

Source

fn read_u128(&mut self) -> Result<u128, DeserializationError>

Returns a u128 value read from self in little-endian byte order.

§Errors

Returns a DeserializationError if a u128 value could not be read from self.

Source

fn read_usize(&mut self) -> Result<usize, DeserializationError>

Returns a usize value read from self in vint64 format.

§Errors

Returns a DeserializationError if:

  • usize value could not be read from self.
  • encoded value is greater than usize maximum value on a given platform.
Source

fn read_vec(&mut self, len: usize) -> Result<Vec<u8>, DeserializationError>

Returns a byte vector of the specified length read from self.

§Errors

Returns a DeserializationError if a vector of the specified length could not be read from self.

Source

fn read_string( &mut self, num_bytes: usize, ) -> Result<String, DeserializationError>

Returns a String of the specified length read from self.

§Errors

Returns a DeserializationError if a String of the specified length could not be read from self.

Source

fn read<D>(&mut self) -> Result<D, DeserializationError>
where Self: Sized, D: Deserializable,

Reads a deserializable value from self.

§Errors

Returns a DeserializationError if the specified value could not be read from self.

Source

fn read_many_iter<D>( &mut self, num_elements: usize, ) -> Result<ReadManyIter<'_, Self, D>, DeserializationError>
where Self: Sized, D: Deserializable,

Returns an iterator that deserializes num_elements instances of D from this reader.

This method validates the requested count against the reader’s capacity before returning the iterator, rejecting implausible lengths early. Each element is then deserialized lazily as the iterator is consumed.

§Errors

Returns an error if num_elements exceeds self.max_alloc(D::min_serialized_size()), indicating the reader cannot allocate that many elements.

§Example
// Collect into a Vec
let items: Vec<u64> = reader
    .read_many_iter::<u64>(count)?
    .collect::<Result<_, _>>()?;

// Collect directly into a BTreeMap (no intermediate Vec)
let map: BTreeMap<K, V> = reader
    .read_many_iter::<(K, V)>(count)?
    .collect::<Result<_, _>>()?;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: AsRef<[u8]>> ByteReader for Cursor<T>

Available on crate feature std only.

Implementors§

Source§

impl ByteReader for ReadAdapter<'_>

Available on crate feature std only.
Source§

impl ByteReader for SliceReader<'_>

Source§

impl<R: ByteReader> ByteReader for BudgetedReader<R>