Skip to main content

BitReader

Struct BitReader 

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

Forward bitstream reader.

Reads bits from a byte buffer in LSB-first order. This is the simpler forward direction used for testing.

Implementations§

Source§

impl<'a> BitReader<'a>

Source

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

Create a new bitstream reader (forward mode).

Source

pub fn new_reversed(data: &'a [u8]) -> Result<BitReader<'a>>

Create a new bitstream reader for reversed Huffman streams.

Zstd Huffman streams are stored in reverse order with a sentinel ‘1’ bit at the end (MSB position). This constructor finds the sentinel and positions the reader to start just below it.

Bits are read from the sentinel position downward, moving to previous bytes as needed. This is the standard Zstd Huffman stream format.

Source

pub fn init_from_end(&mut self) -> Result<()>

Initialize from the last byte, finding the sentinel bit.

Zstd bitstreams have a sentinel ‘1’ bit at the end to mark the boundary. This sets up reversed mode for reading from the sentinel position downward.

Source

pub fn init_fse(&mut self) -> Result<()>

Initialize for FSE bitstream reading (Zstd sequence bitstream).

FSE bitstreams use a different bit ordering than Huffman:

  • Bytes are loaded into a little-endian container
  • The sentinel bit marks the end
  • Bits are read from the LSB going UP (bitsConsumed starts at 0)

This matches zstd’s BIT_DStream behavior.

Source

pub fn switch_to_lsb_mode(&mut self) -> Result<()>

Switch to LSB-first reading for the remaining bits.

After reading initial states in reversed (MSB-first) mode, call this to switch to LSB-first mode for reading extra bits. This is because zstd bitstreams have initial states at the end (near sentinel) and extra bits at the beginning (read from bit 0 going up).

Source

pub fn read_bits(&mut self, n: usize) -> Result<u32>

Read n bits from the stream.

In forward mode: reads LSB first from low to high byte indices. In reversed mode: reads from high to low bit positions, high to low byte indices. In FSE mode: reads LSB-first from little-endian container.

Source

pub fn is_empty(&self) -> bool

Check if the stream is exhausted.

Source

pub fn bits_remaining(&self) -> usize

Get the number of bits remaining.

Source

pub fn peek_bits(&self, n: usize) -> Result<u32>

Read bits without consuming them (peek).

Source

pub fn peek_bits_padded(&self, n: usize) -> Result<u32>

Peek bits with zero padding if fewer than n bits remain.

This is used for Huffman decoding where implicit zero padding exists at the front of the stream. Returns available bits shifted to MSB position, with zeros in lower positions.

Trait Implementations§

Source§

impl<'a> Clone for BitReader<'a>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

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