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, MSB-first.
§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 byte_order(&self) -> ByteOrder
fn byte_order(&self) -> ByteOrder
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 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.