Skip to main content

BitReader

Struct BitReader 

Source
pub struct BitReader<'a> { /* private fields */ }
Expand description

Reads bit-level fields from a byte slice, never panicking.

All methods bound-check and return crate::Error::Truncated instead of panicking or wrapping, so hostile input surfaces as an error at the read that needed it.

Implementations§

Source§

impl<'a> BitReader<'a>

Source

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

Creates a reader over data, positioned at bit 0.

Source

pub const fn remaining_bits(&self) -> usize

The number of bits still readable.

Source

pub const fn bit_position(&self) -> usize

The current bit position from the start of the stream.

Source

pub const fn is_aligned(&self) -> bool

true when the position is on a byte boundary.

Source

pub fn byte_align(&mut self)

Discards bits up to the next byte boundary. Aligned readers are unchanged.

Source

pub fn bits(&mut self, n: usize) -> Result<u64>

Reads n bits with the first stream bit becoming the most-significant bit of the result, for n in 0..=64.

use pith_digest::BitReader;

let mut r = BitReader::new(&[0b1010_0110]);
assert_eq!(r.bits(3).unwrap(), 0b101);
assert_eq!(r.bits(5).unwrap(), 0b0_0110);
Source

pub fn byte(&mut self) -> Result<u8>

Reads one whole byte at the current (must be aligned) position.

Trait Implementations§

Source§

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

Source§

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

Formats the value using the given formatter. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.