pub struct BitReader<'a> { /* private fields */ }Expand description
A cursor that reads values at arbitrary bit offsets from a byte slice, in a
chosen BitOrder (MSB-first by default — bit 0 is the high bit of byte 0,
the RFC/ETSI ASCII-art convention; LSB-first for serial/PHY layers).
§Examples
use bnb::{BitReader, u4, u12};
let mut r = BitReader::new(&[0xAB, 0xCD]);
assert_eq!(r.read::<u4>().unwrap(), u4::new(0xA)); // 4 bits
assert_eq!(r.read::<u12>().unwrap(), u12::new(0xBCD)); // the next 12, straddling a byte
assert_eq!(r.remaining_bits(), 0);Implementations§
Source§impl<'a> BitReader<'a>
impl<'a> BitReader<'a>
Sourcepub fn with_order(bytes: &'a [u8], order: BitOrder) -> Self
pub fn with_order(bytes: &'a [u8], order: BitOrder) -> Self
Wraps bytes, positioned at bit 0, in the given bit order (big-endian).
Sourcepub fn with_layout(bytes: &'a [u8], layout: Layout) -> Self
pub fn with_layout(bytes: &'a [u8], layout: Layout) -> Self
Wraps bytes, positioned at bit 0, in the given Layout (bit + byte order).
Sourcepub fn remaining_bits(&self) -> usize
pub fn remaining_bits(&self) -> usize
Bits not yet consumed.
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 into the low bits of a u128, in the reader’s
bit order (MSB-first by default).
§Errors
ErrorKind::TooWide if n > 128; ErrorKind::UnexpectedEof if fewer
than n bits remain. Either carries the current bit offset.
Sourcepub fn seek_to_bit(&mut self, pos: usize) -> Result<(), BitError>
pub fn seek_to_bit(&mut self, pos: usize) -> Result<(), BitError>
Moves the cursor to absolute bit pos. This needs no Seek trait — the whole
buffer is in hand, so a seek is just cursor arithmetic. (Enables e.g. DNS
name-compression pointers.)
§Errors
ErrorKind::UnexpectedEof if pos is past the end of the buffer.
Sourcepub fn align_to_byte(&mut self)
pub fn align_to_byte(&mut self)
Advances the cursor to the next byte boundary (a no-op if already aligned).
Trait Implementations§
impl SeekSource for BitReader<'_>
Source§impl Source for BitReader<'_>
impl Source for BitReader<'_>
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 only.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.