Skip to main content

BitWriter

Struct BitWriter 

Source
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

Source

pub fn new() -> Self

An empty MSB-first, big-endian writer.

Source

pub fn with_order(order: BitOrder) -> Self

An empty writer in the given bit order (big-endian).

Source

pub fn with_layout(layout: Layout) -> Self

An empty writer in the given Layout (bit + byte order).

Source

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));
Source

pub fn bit_len(&self) -> usize

Bits written so far.

Source

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.

Source

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.

Source

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

Consumes the writer, returning the packed bytes.

Trait Implementations§

Source§

impl Clone for BitWriter

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BitWriter

Source§

fn default() -> BitWriter

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

impl Sink for BitWriter

Source§

fn write_bits(&mut self, value: u128, n: u32) -> Result<(), BitError>

Appends the low n (<= 128) bits of value, in the sink’s bit order (MSB-first by default). Read more
Source§

fn bit_pos(&self) -> usize

The number of bits written so far.
Source§

fn byte_order(&self) -> ByteOrder

The byte order applied to a byte-multiple value (default big-endian).
Source§

fn bit_order(&self) -> BitOrder

The bit order this sink writes in (default MSB-first). Paired with 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>

A type-erased, encode-scoped scratch value for codecs that need mutable state shared across a whole message’s fields — a back-reference / compression dictionary (e.g. DNS name compression). Recover the concrete type with downcast_mut. Read more
Source§

fn write<T: Bits>(&mut self, value: T) -> Result<(), BitError>

Appends one Bits value of its declared width, applying the byte order. Read more
Source§

fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), BitError>

Appends a run of bytes — the bulk dual of Source::read_bytes, replacing the per-byte write(b)? loop in custom codecs. Works at any bit offset. Read more
Source§

fn as_write(&mut self) -> SinkWriter<'_, Self>
where Self: Sized,

Available on crate feature std only.
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§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more