Struct BitReadBuffer

Source
pub struct BitReadBuffer<'a, E>
where E: Endianness,
{ /* private fields */ }
Expand description

Buffer that allows reading integers of arbitrary bit length and non byte-aligned integers

§Examples

use bitbuffer::{BitReadBuffer, LittleEndian, Result};

let bytes = vec![
    0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
    0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111
];
let buffer = BitReadBuffer::new(&bytes, LittleEndian);
// read 7 bits as u8, starting from bit 3
let result: u8 = buffer.read_int(3, 7)?;

Implementations§

Source§

impl<'a, E> BitReadBuffer<'a, E>
where E: Endianness,

Source

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

Create a new BitBuffer from a byte slice

§Examples
use bitbuffer::{BitReadBuffer, LittleEndian};

let bytes = vec![
    0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
    0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111
];
let buffer = BitReadBuffer::new(&bytes, LittleEndian);
Source

pub fn to_owned(&self) -> BitReadBuffer<'static, E>

Create a static version of this buffer

If the current buffer is borrowed, this will copy the data

Source§

impl<E> BitReadBuffer<'static, E>
where E: Endianness,

Source

pub fn new_owned(bytes: Vec<u8>, _endianness: E) -> Self

Create a new BitBuffer from a byte vector

§Examples
use bitbuffer::{BitReadBuffer, LittleEndian};

let bytes = vec![
    0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
    0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111
];
let buffer = BitReadBuffer::new_owned(bytes, LittleEndian);
Source§

impl<'a, E> BitReadBuffer<'a, E>
where E: Endianness,

Source

pub fn bit_len(&self) -> usize

The available number of bits in the buffer

Source

pub fn byte_len(&self) -> usize

The available number of bytes in the buffer

Source

pub fn read_bool(&self, position: usize) -> Result<bool>

Read a single bit from the buffer as boolean

§Errors
§Examples
let result = buffer.read_bool(5)?;
assert_eq!(result, true);
Source

pub fn read_int<T>(&self, position: usize, count: usize) -> Result<T>

Read a sequence of bits from the buffer as integer

§Errors
§Examples
let result = buffer.read_int::<u16>(10, 9)?;
assert_eq!(result, 0b100_0110_10);
Source

pub fn read_bytes( &self, position: usize, byte_count: usize, ) -> Result<Cow<'a, [u8]>>

Read a series of bytes from the buffer

§Errors
§Examples
assert_eq!(buffer.read_bytes(5, 3)?.to_vec(), &[0b0_1010_101, 0b0_1100_011, 0b1_1001_101]);
assert_eq!(buffer.read_bytes(0, 8)?.to_vec(), &[
    0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
    0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111
]);
Source

pub fn read_string( &self, position: usize, byte_len: Option<usize>, ) -> Result<Cow<'a, str>>

Read a series of bytes from the buffer as string

You can either read a fixed number of bytes, or a dynamic length null-terminated string

§Errors
§Examples
// Fixed length string
assert_eq!(buffer.read_string(0, Some(13))?, "Hello world".to_owned());
// fixed length with null padding
assert_eq!(buffer.read_string(0, Some(16))?, "Hello world".to_owned());
// null terminated
assert_eq!(buffer.read_string(0, None)?, "Hello world".to_owned());
Source

pub fn read_float<T>(&self, position: usize) -> Result<T>

Read a sequence of bits from the buffer as float

§Errors
§Examples
let result = buffer.read_float::<f32>(10)?;
Source

pub fn truncate(&mut self, bit_len: usize) -> Result<()>

Truncate the buffer to a given bit length

Trait Implementations§

Source§

impl<E: Endianness> Clone for BitReadBuffer<'_, E>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<E: Endianness> Debug for BitReadBuffer<'_, E>

Source§

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

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

impl<'a, E: Endianness> From<&'a [u8]> for BitReadBuffer<'a, E>

Source§

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

Converts to this type from the input type.
Source§

impl<'a, E: Endianness> From<BitReadBuffer<'a, E>> for BitReadStream<'a, E>

Source§

fn from(buffer: BitReadBuffer<'a, E>) -> Self

Converts to this type from the input type.
Source§

impl<E: Endianness> From<Vec<u8>> for BitReadBuffer<'_, E>

Source§

fn from(bytes: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl<E: Endianness> PartialEq for BitReadBuffer<'_, E>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'a, E> Freeze for BitReadBuffer<'a, E>

§

impl<'a, E> RefUnwindSafe for BitReadBuffer<'a, E>
where E: RefUnwindSafe,

§

impl<'a, E> !Send for BitReadBuffer<'a, E>

§

impl<'a, E> !Sync for BitReadBuffer<'a, E>

§

impl<'a, E> Unpin for BitReadBuffer<'a, E>
where E: Unpin,

§

impl<'a, E> UnwindSafe for BitReadBuffer<'a, E>
where E: 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> 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, 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.