Skip to main content

StreamBitReader

Struct StreamBitReader 

Source
pub struct StreamBitReader<R> { /* private fields */ }
Available on crate feature 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>

Source

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.

Source

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.

Source

pub fn bit_pos(&self) -> usize

The total number of bits consumed so far.

Source

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.

Source

pub fn read<T: Bits>(&mut self) -> Result<T, BitError>

Reads one Bits value (width <= 128) of its declared width, applying the source’s byte order to whole values — matching BitReader::read and Source::read, so sr.read::<T>() agrees with T::decode(&mut sr).

§Errors

As read_bits.

Trait Implementations§

Source§

impl<R: Debug> Debug for StreamBitReader<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R: Read> Source for StreamBitReader<R>

Source§

fn read_bits(&mut self, n: u32) -> Result<u128, BitError>

Reads n (<= 128) bits into the low bits of a u128, in the source’s bit order (MSB-first by default). Read more
Source§

fn bit_pos(&self) -> usize

The current absolute bit offset (for position-aware errors).
Source§

fn byte_order(&self) -> ByteOrder

The byte order applied to a byte-multiple value (default big-endian).
Source§

fn bit_order(&self) -> BitOrder

The bit order this source reads in (default MSB-first). Paired with 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>

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 more
Source§

fn read<T: Bits>(&mut self) -> Result<T, BitError>

Reads one Bits value of its declared width, applying the byte order. Read more
Source§

fn read_bytes(&mut self, n: usize) -> Result<Vec<u8>, BitError>

Reads 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 more
Source§

fn read_into(&mut self, buf: &mut [u8]) -> Result<(), BitError>

Fills buf with bytes from the source — the no-alloc dual of read_bytes, for fixed scratch buffers and tight no_std paths. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more