Skip to main content

BitReader

Struct BitReader 

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

Source

pub fn new(bytes: &'a [u8]) -> Self

Wraps bytes, positioned at bit 0, MSB-first, big-endian.

Source

pub fn with_order(bytes: &'a [u8], order: BitOrder) -> Self

Wraps bytes, positioned at bit 0, in the given bit order (big-endian).

Source

pub fn with_layout(bytes: &'a [u8], layout: Layout) -> Self

Wraps bytes, positioned at bit 0, in the given Layout (bit + byte order).

Source

pub fn bit_pos(&self) -> usize

The current absolute bit offset.

Source

pub fn remaining_bits(&self) -> usize

Bits not yet consumed.

Source

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.

Source

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

Reads one Bits value of its declared width, applying the byte order to a byte-multiple value.

§Errors

As read_bits.

Source

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.

Source

pub fn align_to_byte(&mut self)

Advances the cursor to the next byte boundary (a no-op if already aligned).

Trait Implementations§

Source§

impl<'a> Clone for BitReader<'a>

Source§

fn clone(&self) -> BitReader<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for BitReader<'a>

Source§

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

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

impl SeekSource for BitReader<'_>

Source§

impl Source for BitReader<'_>

Source§

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

Reads n (<= 128) bits MSB-first into the low bits of a u128. 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 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 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<'a> Freeze for BitReader<'a>

§

impl<'a> RefUnwindSafe for BitReader<'a>

§

impl<'a> Send for BitReader<'a>

§

impl<'a> Sync for BitReader<'a>

§

impl<'a> Unpin for BitReader<'a>

§

impl<'a> UnsafeUnpin for BitReader<'a>

§

impl<'a> UnwindSafe for BitReader<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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