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
impl BitWriter
Sourcepub fn write_bits(&mut self, value: u64, count: u8)
pub fn write_bits(&mut self, value: u64, count: u8)
Write count bits from value, LSB first.
Sourcepub fn write_bool(&mut self, v: bool)
pub fn write_bool(&mut self, v: bool)
Write a single boolean as 1 bit.
Sourcepub fn flush_to_byte_boundary(&mut self)
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.
Sourcepub fn write_leb128(&mut self, v: u64)
pub fn write_leb128(&mut self, v: u64)
Write a LEB128-encoded unsigned integer.
Sourcepub fn write_zigzag(&mut self, v: i64, type_bits: u8)
pub fn write_zigzag(&mut self, v: i64, type_bits: u8)
Write a ZigZag + LEB128 encoded signed integer.
Sourcepub fn write_string(&mut self, s: &str)
pub fn write_string(&mut self, s: &str)
Write a UTF-8 string with a LEB128 length prefix.
Sourcepub fn write_bytes(&mut self, data: &[u8])
pub fn write_bytes(&mut self, data: &[u8])
Write a byte slice with a LEB128 length prefix.
Sourcepub fn write_raw_bytes(&mut self, data: &[u8])
pub fn write_raw_bytes(&mut self, data: &[u8])
Write raw bytes with no length prefix.
Sourcepub fn enter_recursive(&mut self) -> Result<(), EncodeError>
pub fn enter_recursive(&mut self) -> Result<(), EncodeError>
Increment recursion depth; return error if limit exceeded.
Sourcepub fn leave_recursive(&mut self)
pub fn leave_recursive(&mut self)
Decrement recursion depth.