pub struct StreamBitReader<R> { /* private fields */ }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_from(&mut s).unwrap(), Word { value: 0x1234_5678 });Implementations§
Source§impl<R: Read> StreamBitReader<R>
impl<R: Read> StreamBitReader<R>
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 MSB-first, pulling bytes from the source as needed.
§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>
Available on crate feature std only.
impl<R: Read> Source for StreamBitReader<R>
Available on crate feature
std only.Source§fn byte_order(&self) -> ByteOrder
fn byte_order(&self) -> ByteOrder
The byte order applied to a byte-multiple value (default big-endian).
Source§fn seek_to_bit(&mut self, _pos: usize) -> Result<(), BitError>
fn seek_to_bit(&mut self, _pos: usize) -> Result<(), BitError>
Moves the cursor to absolute bit
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,
Borrows this source as a
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.Auto Trait Implementations§
impl<R> Freeze for StreamBitReader<R>where
R: Freeze,
impl<R> RefUnwindSafe for StreamBitReader<R>where
R: RefUnwindSafe,
impl<R> Send for StreamBitReader<R>where
R: Send,
impl<R> Sync for StreamBitReader<R>where
R: Sync,
impl<R> Unpin for StreamBitReader<R>where
R: Unpin,
impl<R> UnsafeUnpin for StreamBitReader<R>where
R: UnsafeUnpin,
impl<R> UnwindSafe for StreamBitReader<R>where
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more