pub struct StreamBitReader<R> { /* private fields */ }std only.Expand description
A forward-only bit reader over any std::io::Read — the streaming counterpart
to the in-memory BitReader, for a stream you read once and don’t seek.
It is bounded on Read only, not Seek, so it works over inputs that can’t
seek (a socket, or a &[u8], which is Read but not Seek). A message that needs
to seek (#[br(restore_position)]) won’t decode through it — use a BufSource or
SeekReader for that. Reads up to 128 bits per call (the Source width
ceiling); running out mid-message yields ErrorKind::Incomplete (“read more and
retry”).
§Examples
use bnb::{bin, StreamBitReader};
#[bin(big)]
#[derive(Debug, PartialEq)]
struct Word { value: u32 }
// `&[u8]` is `Read` but not `Seek` — exactly the forward-only case.
let data: &[u8] = &[0x12, 0x34, 0x56, 0x78];
let mut s = StreamBitReader::new(data);
assert_eq!(Word::decode(&mut s).unwrap(), Word { value: 0x1234_5678 });Implementations§
Source§impl<R: Read> StreamBitReader<R>
impl<R: Read> StreamBitReader<R>
Sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Wraps a byte source, decoding in the default MSB-first, big-endian order. For a
#[bin(little)]/bits = lsb message, use with_layout with
that type’s LAYOUT, or the decode reads the wrong order.
Sourcepub fn with_layout(inner: R, layout: Layout) -> Self
pub fn with_layout(inner: R, layout: Layout) -> Self
Wraps a byte source, decoding in the given Layout (bit + byte order) — pass a
message’s LAYOUT so a non-default-order #[bin] type round-trips over a stream.
Sourcepub fn read_bits(&mut self, n: u32) -> Result<u128, BitError>
pub fn read_bits(&mut self, n: u32) -> Result<u128, BitError>
Reads n (<= 128) bits in the source’s bit order, pulling bytes from
the source as needed. This matches every other Source: byte order is applied to
whole values by read / Source::read, not here.
§Errors
ErrorKind::TooWide if n > 128; ErrorKind::Incomplete if the
source runs out mid-field (read more and retry). Either carries the bit
offset.
Trait Implementations§
Source§impl<R: Debug> Debug for StreamBitReader<R>
impl<R: Debug> Debug for StreamBitReader<R>
Source§impl<R: Read> Source for StreamBitReader<R>
impl<R: Read> Source for StreamBitReader<R>
Source§fn read_bits(&mut self, n: u32) -> Result<u128, BitError>
fn read_bits(&mut self, n: u32) -> Result<u128, BitError>
n (<= 128) bits into the low bits of a u128, in the source’s
bit order (MSB-first by default). Read moreSource§fn byte_order(&self) -> ByteOrder
fn byte_order(&self) -> ByteOrder
Source§fn bit_order(&self) -> BitOrder
fn bit_order(&self) -> BitOrder
byte_order to decide whether a byte-multiple value needs its
bytes swapped — each bit order has a natural byte layout (big-endian under MSB,
little-endian under LSB), and only the opposite declaration swaps.Source§fn seek_to_bit(&mut self, _pos: usize) -> Result<(), BitError>
fn seek_to_bit(&mut self, _pos: usize) -> Result<(), BitError>
pos. The default — for a forward-only
source — fails with ErrorKind::NotSeekable; seekable sources (the slice
BitReader) override it. A SeekSource guarantees this works. Read moreSource§fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>, BitError>
fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>, BitError>
n bytes into a fresh Vec (at any bit offset — the bytes need not be
aligned). The bulk form of the per-byte read::<u8>() loop, for blob/payload
reads in custom codecs and container formats. Read moreSource§fn read_into(&mut self, buf: &mut [u8]) -> Result<(), BitError>
fn read_into(&mut self, buf: &mut [u8]) -> Result<(), BitError>
buf with bytes from the source — the no-alloc dual of
read_bytes, for fixed scratch buffers and tight
no_std paths. Read moreSource§fn as_read(&mut self) -> SourceReader<'_, Self> ⓘwhere
Self: Sized,
fn as_read(&mut self) -> SourceReader<'_, Self> ⓘwhere
Self: Sized,
std::io::Read over its bytes — for handing the
cursor to std::io-based code from a #[br(parse_with = …)] (e.g. a decoder, or
a Read-based parser). Reads 8 bits per byte; see SourceReader. Only with
the std feature.