Skip to main content

BitReader

Struct BitReader 

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

A cursor over a byte slice that reads fields LSB-first at the bit level.

Created with BitReader::new, consumed with read_* methods. Tracks a byte position and a sub-byte bit offset, plus a recursion depth counter for safely decoding recursive types.

Sub-byte reads pull individual bits from the current byte. Multi-byte reads (e.g. read_u16) first align to the next byte boundary, then interpret the bytes as little-endian.

Implementations§

Source§

impl<'a> BitReader<'a>

Source

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

Create a new BitReader over the given byte slice.

Source

pub fn read_bits(&mut self, count: u8) -> Result<u64, DecodeError>

Read count bits LSB-first into a u64.

Source

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

Read a single bit as bool.

Source

pub fn flush_to_byte_boundary(&mut self)

Advance to the next byte boundary, discarding any remaining bits in the current byte. Infallible.

Source

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

Read a u8, aligning to a byte boundary first.

Source

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

Read a little-endian u16, aligning to a byte boundary first.

Source

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

Read a little-endian u32, aligning to a byte boundary first.

Source

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

Read a little-endian u64, aligning to a byte boundary first.

Source

pub fn read_i8(&mut self) -> Result<i8, DecodeError>

Read an i8, aligning to a byte boundary first.

Source

pub fn read_i16(&mut self) -> Result<i16, DecodeError>

Read a little-endian i16, aligning to a byte boundary first.

Source

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

Read a little-endian i32, aligning to a byte boundary first.

Source

pub fn read_i64(&mut self) -> Result<i64, DecodeError>

Read a little-endian i64, aligning to a byte boundary first.

Source

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

Read a little-endian f32, aligning to a byte boundary first.

Source

pub fn read_f64(&mut self) -> Result<f64, DecodeError>

Read a little-endian f64, aligning to a byte boundary first.

Source

pub fn read_leb128(&mut self, max_bytes: u8) -> Result<u64, DecodeError>

Read a LEB128-encoded u64, consuming at most max_bytes bytes.

Source

pub fn read_zigzag( &mut self, _type_bits: u8, max_bytes: u8, ) -> Result<i64, DecodeError>

Read a ZigZag + LEB128 encoded signed integer.

Source

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

Read a length-prefixed UTF-8 string.

Source

pub fn read_bytes(&mut self) -> Result<Vec<u8>, DecodeError>

Read a length-prefixed byte vector.

Source

pub fn read_raw_bytes(&mut self, len: usize) -> Result<Vec<u8>, DecodeError>

Read exactly len raw bytes with no length prefix.

Source

pub fn read_remaining(&mut self) -> Vec<u8>

Read all remaining bytes from the current position to the end. Flushes to byte boundary first. Returns an empty Vec if no bytes remain.

Source

pub fn enter_recursive(&mut self) -> Result<(), DecodeError>

Increment recursion depth; return error if limit exceeded.

Source

pub fn leave_recursive(&mut self)

Decrement recursion depth.

Auto Trait Implementations§

§

impl<'a> Freeze for BitReader<'a>

§

impl<'a> RefUnwindSafe for BitReader<'a>

§

impl<'a> Send for BitReader<'a>

§

impl<'a> Sync for BitReader<'a>

§

impl<'a> Unpin for BitReader<'a>

§

impl<'a> UnsafeUnpin for BitReader<'a>

§

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