pub struct BitReader<'a> { /* private fields */ }Expand description
MSB-first bit reader over a borrowed byte slice.
Copy+Clone: the reader owns no heap state, only a borrowed slice
and a handful of counters, so let saved = br; followed by a
later br = saved; is a valid checkpoint/restore — some codec
parsers (e.g. MPEG-4 Part 2’s VOP resync) use exactly that pattern.
Implementations§
Source§impl<'a> BitReader<'a>
impl<'a> BitReader<'a>
pub fn new(data: &'a [u8]) -> Self
Sourcepub fn with_position(data: &'a [u8], byte_pos: usize) -> Self
pub fn with_position(data: &'a [u8], byte_pos: usize) -> Self
Start reading at a specific byte offset (useful for parsers that need to re-anchor into the middle of a buffer without copying).
Sourcepub fn bit_position(&self) -> u64
pub fn bit_position(&self) -> u64
Bits already consumed from the logical stream.
Sourcepub fn byte_position(&self) -> usize
pub fn byte_position(&self) -> usize
Byte offset of the reader (floor of bit_position / 8).
Sourcepub fn bits_remaining(&self) -> u64
pub fn bits_remaining(&self) -> u64
Total remaining bits (buffered + unread from the slice).
Sourcepub fn is_byte_aligned(&self) -> bool
pub fn is_byte_aligned(&self) -> bool
True if the reader is positioned on a byte boundary.
Sourcepub fn align_to_byte(&mut self)
pub fn align_to_byte(&mut self)
Skip remaining bits in the current byte, leaving the reader byte-aligned.
Sourcepub fn read_i32(&mut self, n: u32) -> Result<i32>
pub fn read_i32(&mut self, n: u32) -> Result<i32>
Read n bits as a signed integer, sign-extended from the high bit.
Sourcepub fn read_u1(&mut self) -> Result<u32>
pub fn read_u1(&mut self) -> Result<u32>
Read a single bit as 0 or 1 (some codec specs phrase flags this way).
Sourcepub fn consume(&mut self, n: u32) -> Result<()>
pub fn consume(&mut self, n: u32) -> Result<()>
Alias for Self::skip — some spec wordings prefer “consume”.
Sourcepub fn read_unary(&mut self) -> Result<u32>
pub fn read_unary(&mut self) -> Result<u32>
Read a unary-coded value: the count of leading zero bits, terminated
by a single 1. Used by FLAC Rice residuals and any other codec
that needs variable-length counts. Uses leading_zeros() on the
64-bit accumulator for the fast path.