pub struct BitWriter { /* private fields */ }Expand description
A sink that appends values at arbitrary bit offsets in a chosen BitOrder
(MSB-first by default), growing a byte buffer (the final partial byte is
zero-padded).
§Examples
use bnb::{BitWriter, u4, u12};
let mut w = BitWriter::new();
w.write(u4::new(0xA)).unwrap();
w.write(u12::new(0xBCD)).unwrap();
assert_eq!(w.bit_len(), 16);
assert_eq!(w.into_bytes(), [0xAB, 0xCD]);Implementations§
Source§impl BitWriter
impl BitWriter
Sourcepub fn with_order(order: BitOrder) -> Self
pub fn with_order(order: BitOrder) -> Self
An empty writer in the given bit order (big-endian).
Sourcepub fn with_layout(layout: Layout) -> Self
pub fn with_layout(layout: Layout) -> Self
An empty writer in the given Layout (bit + byte order).
Sourcepub fn with_scratch(self, scratch: Box<dyn Any>) -> Self
pub fn with_scratch(self, scratch: Box<dyn Any>) -> Self
Attach a type-erased scratch value, reachable from any codec during the encode
via Sink::scratch and recovered by downcast_mut.
The escape hatch for codecs that need mutable state shared across a whole message’s
fields — a back-reference / compression dictionary (e.g. DNS name compression). The
scratch lives for the encode and is dropped by into_bytes; it
is never written to the output and is not carried by Clone.
use bnb::{BitWriter, Sink};
let mut w = BitWriter::new().with_scratch(Box::new(0u32));
if let Some(n) = w.scratch().and_then(|s| s.downcast_mut::<u32>()) {
*n += 1;
}
assert_eq!(w.scratch().and_then(|s| s.downcast_ref::<u32>()), Some(&1));Sourcepub fn write_bits(&mut self, value: u128, n: u32) -> Result<(), BitError>
pub fn write_bits(&mut self, value: u128, n: u32) -> Result<(), BitError>
Appends the low n (<= 128) bits of value, in the writer’s bit order.
§Errors
ErrorKind::TooWide if n > 128.
Sourcepub fn write<T: Bits>(&mut self, value: T) -> Result<(), BitError>
pub fn write<T: Bits>(&mut self, value: T) -> Result<(), BitError>
Appends one Bits value of its declared width, applying the byte order to
a byte-multiple value.
§Errors
As write_bits.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the writer, returning the packed bytes.
Trait Implementations§
Source§impl Sink for BitWriter
impl Sink for BitWriter
Source§fn byte_order(&self) -> ByteOrder
fn byte_order(&self) -> ByteOrder
Source§fn bit_order(&self) -> BitOrder
fn bit_order(&self) -> BitOrder
byte_order exactly as on Source: each bit order has a
natural byte layout (big-endian under MSB, little-endian under LSB), and only the
opposite declaration swaps a byte-multiple value.Source§fn scratch(&mut self) -> Option<&mut dyn Any>
fn scratch(&mut self) -> Option<&mut dyn Any>
downcast_mut. Read moreSource§fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), BitError>
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), BitError>
Source::read_bytes, replacing the
per-byte write(b)? loop in custom codecs. Works at any bit offset. Read moreSource§fn as_write(&mut self) -> SinkWriter<'_, Self> ⓘwhere
Self: Sized,
fn as_write(&mut self) -> SinkWriter<'_, Self> ⓘwhere
Self: Sized,
std only.std::io::Write — the dual of Source::as_read, for
handing the cursor to std::io-based code from a #[bw(write_with = …)]. Writes 8
bits per byte; see SinkWriter. Only with the std feature.