Skip to main content

BitReader

Struct BitReader 

Source
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>

Source

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

Source

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).

Source

pub fn bit_position(&self) -> u64

Bits already consumed from the logical stream.

Source

pub fn byte_position(&self) -> usize

Byte offset of the reader (floor of bit_position / 8).

Source

pub fn bits_remaining(&self) -> u64

Total remaining bits (buffered + unread from the slice).

Source

pub fn is_byte_aligned(&self) -> bool

True if the reader is positioned on a byte boundary.

Source

pub fn align_to_byte(&mut self)

Skip remaining bits in the current byte, leaving the reader byte-aligned.

Source

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

Read n bits (0..=32) as an unsigned integer.

Source

pub fn read_u64(&mut self, n: u32) -> Result<u64>

Read n bits (0..=64) as an unsigned integer.

Source

pub fn read_i32(&mut self, n: u32) -> Result<i32>

Read n bits as a signed integer, sign-extended from the high bit.

Source

pub fn read_bit(&mut self) -> Result<bool>

Read a single bit as a bool.

Source

pub fn read_u1(&mut self) -> Result<u32>

Read a single bit as 0 or 1 (some codec specs phrase flags this way).

Source

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

Peek n bits (0..=32) without consuming them.

Source

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

Discard n bits.

Source

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

Alias for Self::skip — some spec wordings prefer “consume”.

Source

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.

Source

pub fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>>

Read n bytes. Requires the reader to be byte-aligned.

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> Copy for BitReader<'a>

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> 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.