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 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
The byte order applied to a byte-multiple value (default big-endian).
Source§fn as_write(&mut self) -> SinkWriter<'_, Self> ⓘwhere
Self: Sized,
fn as_write(&mut self) -> SinkWriter<'_, Self> ⓘwhere
Self: Sized,
Borrows this sink as a
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.Auto Trait Implementations§
impl Freeze for BitWriter
impl RefUnwindSafe for BitWriter
impl Send for BitWriter
impl Sync for BitWriter
impl Unpin for BitWriter
impl UnsafeUnpin for BitWriter
impl UnwindSafe for BitWriter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more