Skip to main content

BitWriter

Struct BitWriter 

Source
pub struct BitWriter { /* private fields */ }
Expand description

A byte-buffer builder that packs fields LSB-first at the bit level.

Created with BitWriter::new, written to with write_* methods, and finalized with BitWriter::finish which flushes any partial byte and returns the completed buffer.

Sub-byte fields are accumulated in a single byte; once 8 bits are filled the byte is flushed. Multi-byte writes (e.g. write_u16) first align to a byte boundary, then append little-endian bytes directly.

Implementations§

Source§

impl BitWriter

Source

pub fn new() -> Self

Create a new, empty BitWriter.

Source

pub fn write_bits(&mut self, value: u64, count: u8)

Write count bits from value, LSB first.

Source

pub fn write_bool(&mut self, v: bool)

Write a single boolean as 1 bit.

Source

pub fn flush_to_byte_boundary(&mut self)

Flush any partial byte to the buffer.

Special case per spec §4.1: if nothing has been written at all (bit_offset == 0 AND buf is empty), push a zero byte anyway. If bit_offset == 0 and buf is non-empty, this is a no-op.

Source

pub fn write_u8(&mut self, v: u8)

Write a u8, aligning to a byte boundary first.

Source

pub fn write_u16(&mut self, v: u16)

Write a u16 in little-endian byte order, aligning first.

Source

pub fn write_u32(&mut self, v: u32)

Write a u32 in little-endian byte order, aligning first.

Source

pub fn write_u64(&mut self, v: u64)

Write a u64 in little-endian byte order, aligning first.

Source

pub fn write_i8(&mut self, v: i8)

Write an i8, aligning to a byte boundary first.

Source

pub fn write_i16(&mut self, v: i16)

Write an i16 in little-endian byte order, aligning first.

Source

pub fn write_i32(&mut self, v: i32)

Write an i32 in little-endian byte order, aligning first.

Source

pub fn write_i64(&mut self, v: i64)

Write an i64 in little-endian byte order, aligning first.

Source

pub fn write_f32(&mut self, v: f32)

Write an f32, canonicalizing NaN to 0x7FC00000.

Source

pub fn write_f64(&mut self, v: f64)

Write an f64, canonicalizing NaN to 0x7FF8000000000000.

Source

pub fn write_leb128(&mut self, v: u64)

Write a LEB128-encoded unsigned integer.

Source

pub fn write_zigzag(&mut self, v: i64, type_bits: u8)

Write a ZigZag + LEB128 encoded signed integer.

Source

pub fn write_string(&mut self, s: &str)

Write a UTF-8 string with a LEB128 length prefix.

Source

pub fn write_bytes(&mut self, data: &[u8])

Write a byte slice with a LEB128 length prefix.

Source

pub fn write_raw_bytes(&mut self, data: &[u8])

Write raw bytes with no length prefix.

Source

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

Increment recursion depth; return error if limit exceeded.

Source

pub fn leave_recursive(&mut self)

Decrement recursion depth.

Source

pub fn finish(self) -> Vec<u8>

Flush any partial byte and return the finished buffer.

Trait Implementations§

Source§

impl Default for BitWriter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.