Skip to main content

BitReader

Struct BitReader 

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

High-performance bit-level reader over a byte slice.

Uses safe unaligned 64-bit reads via u64::from_le_bytes for fast bit extraction. Every read returns Result<T> with overflow checking.

Implementations§

Source§

impl<'a> BitReader<'a>

Source

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

Create a new reader starting at bit 0 of data.

Source

pub fn bits_remaining(&self) -> usize

Number of bits between the cursor and the end of the buffer.

Source

pub fn position(&self) -> usize

Current bit offset from the start of the buffer.

Source

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

Read up to 64 bits. Returns the value right-aligned in a u64.

Source

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

Peek at up to 64 bits without advancing the position.

Source

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

Read a single bit as a boolean.

Source

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

Read 8 bits as a u8.

Source

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

Read 16 bits as a little-endian u16.

Source

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

Read 32 bits as a little-endian u32.

Source

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

Read 64 bits as a little-endian u64.

Source

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

Read 32 bits and reinterpret as an IEEE 754 f32.

Source

pub fn read_bytes(&mut self, buf: &mut [u8]) -> Result<()>

Read N bytes into the provided buffer.

Source

pub fn read_bits_to_bytes(&mut self, buf: &mut [u8], bits: usize) -> Result<()>

Read a specified number of bits into a byte buffer, filling LSB-first.

Source

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

Read an unsigned varint (up to 32 bits).

Source

pub fn read_uvarint64(&mut self) -> Result<u64>

Read an unsigned varint (up to 64 bits).

Source

pub fn read_varint32(&mut self) -> Result<i32>

Read a signed varint (zigzag encoded, 32-bit).

Source

pub fn read_varint64(&mut self) -> Result<i64>

Read a signed varint (zigzag encoded, 64-bit).

Source

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

Valve’s variable-length unsigned integer encoding.

Reads 6 bits; bits 4-5 select the total width: 00 → 6 bits, 01 → 4+4, 10 → 4+8, 11 → 4+28.

Source

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

Field-path variant of ubitvar — cascading 1-bit selectors.

Used exclusively for encoding field path operation indices: 2, 4, 10, 17, or 31 bits depending on which prefix bit is set.

Source

pub fn read_bitcoord(&mut self) -> Result<f32>

Read a coordinate value.

Source

pub fn read_bitnormal(&mut self) -> Result<f32>

Read a normal component (sign + 11-bit fraction).

Source

pub fn read_bitvec3coord(&mut self) -> Result<[f32; 3]>

Read a 3D coordinate vector.

Source

pub fn read_bitvec3normal(&mut self) -> Result<[f32; 3]>

Read a 3D normal vector (2 components + derived Z).

Source

pub fn read_bitangle(&mut self, n: usize) -> Result<f32>

Read an angle encoded as N bits, returning degrees in [0, 360).

Source

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

Read a null-terminated string.

Source

pub fn read_string_into(&mut self, buf: &mut [u8]) -> Result<usize>

Read a string into the provided buffer, returning bytes written (excluding null).

Source

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

Read a string as raw bytes into a Vec, returning bytes written (excluding null).

Source

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

Skip forward by N bits.

Source

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

Skip a varint without decoding it.

Source

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

Skip a bitcoord value.

Source

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

Skip a bitnormal value.

Source

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

Skip a 3D coordinate vector.

Source

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

Skip a 3D normal vector.

Source

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

Skip a null-terminated string.

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.