pub trait Sink: Sealed {
// Required methods
fn write_bits(&mut self, value: u128, n: u32) -> Result<(), BitError>;
fn bit_pos(&self) -> usize;
// Provided methods
fn byte_order(&self) -> ByteOrder { ... }
fn bit_order(&self) -> BitOrder { ... }
fn scratch(&mut self) -> Option<&mut dyn Any> { ... }
fn write<T: Bits>(&mut self, value: T) -> Result<(), BitError> { ... }
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), BitError> { ... }
fn as_write(&mut self) -> SinkWriter<'_, Self> ⓘ
where Self: Sized { ... }
}Expand description
A bit-level output the codec writes to — the in-memory BitWriter
(and, under the bytes feature, BytesWriter). Encode to any
std::io::Write via a message’s generated encode method.
§Examples
use bnb::{BitWriter, Sink, u4};
// A writer generic over any `Sink`.
fn put_nibble<K: Sink>(k: &mut K, v: u4) { k.write(v).unwrap(); }
let mut w = BitWriter::new();
put_nibble(&mut w, u4::new(0xA));
put_nibble(&mut w, u4::new(0x5));
assert_eq!(w.into_bytes(), [0xA5]);Required Methods§
Provided Methods§
Sourcefn byte_order(&self) -> ByteOrder
fn byte_order(&self) -> ByteOrder
The byte order applied to a byte-multiple value (default big-endian).
Sourcefn bit_order(&self) -> BitOrder
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.
Sourcefn scratch(&mut self) -> Option<&mut dyn Any>
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.
Returns None unless the sink was built carrying one (see
BitWriter::with_scratch); the default sink has none. The scratch is the shared
thread the Sink already provides — since one sink is passed by &mut through
every field’s encode, a value stored here is visible to them all.
Sourcefn write_bytes(&mut self, bytes: &[u8]) -> Result<(), BitError>
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.
§Errors
As write_bits.
Sourcefn as_write(&mut self) -> SinkWriter<'_, Self> ⓘwhere
Self: Sized,
Available on crate feature std only.
fn as_write(&mut self) -> SinkWriter<'_, Self> ⓘwhere
Self: Sized,
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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".