[−][src]Struct bitbuffer::BitReadBuffer
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)?;
Methods
impl<E> BitReadBuffer<E> where
E: Endianness,
[src]
E: Endianness,
pub fn new(bytes: Vec<u8>, _endianness: E) -> Self
[src]
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(bytes, LittleEndian);
impl<E> BitReadBuffer<E> where
E: Endianness,
[src]
E: Endianness,
pub fn bit_len(&self) -> usize
[src]
The available number of bits in the buffer
pub fn byte_len(&self) -> usize
[src]
The available number of bytes in the buffer
pub fn read_bool(&self, position: usize) -> Result<bool>
[src]
Read a single bit from the buffer as boolean
Errors
ReadError::NotEnoughData
: not enough bits available in the buffer
Examples
let result = buffer.read_bool(5)?; assert_eq!(result, true);
pub fn read_int<T>(&self, position: usize, count: usize) -> Result<T> where
T: PrimInt + BitOrAssign + IsSigned + UncheckedPrimitiveInt + BitXor,
[src]
T: PrimInt + BitOrAssign + IsSigned + UncheckedPrimitiveInt + BitXor,
Read a sequence of bits from the buffer as integer
Errors
ReadError::NotEnoughData
: not enough bits available in the bufferReadError::TooManyBits
: to many bits requested for the chosen integer type
Examples
let result = buffer.read_int::<u16>(10, 9)?; assert_eq!(result, 0b100_0110_10);
pub fn read_bytes(&self, position: usize, byte_count: usize) -> Result<Vec<u8>>
[src]
Read a series of bytes from the buffer
Errors
ReadError::NotEnoughData
: not enough bits available in the buffer
Examples
assert_eq!(buffer.read_bytes(5, 3)?, &[0b0_1010_101, 0b0_1100_011, 0b1_1001_101]); assert_eq!(buffer.read_bytes(0, 8)?, &[ 0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111 ]);
pub fn read_string(
&self,
position: usize,
byte_len: Option<usize>
) -> Result<String>
[src]
&self,
position: usize,
byte_len: Option<usize>
) -> Result<String>
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
ReadError::NotEnoughData
: not enough bits available in the bufferReadError::Utf8Error
: the read bytes are not valid utf8
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());
pub fn read_float<T>(&self, position: usize) -> Result<T> where
T: Float + UncheckedPrimitiveFloat,
[src]
T: Float + UncheckedPrimitiveFloat,
Read a sequence of bits from the buffer as float
Errors
ReadError::NotEnoughData
: not enough bits available in the buffer
Examples
let result = buffer.read_float::<f32>(10)?;
Trait Implementations
impl<E: Endianness> Clone for BitReadBuffer<E>
[src]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<E: Endianness> Debug for BitReadBuffer<E>
[src]
impl<E: Endianness> From<BitReadBuffer<E>> for BitReadStream<E>
[src]
fn from(buffer: BitReadBuffer<E>) -> Self
[src]
impl<E: Endianness> From<Vec<u8>> for BitReadBuffer<E>
[src]
Auto Trait Implementations
impl<E> !RefUnwindSafe for BitReadBuffer<E>
impl<E> !Send for BitReadBuffer<E>
impl<E> !Sync for BitReadBuffer<E>
impl<E> Unpin for BitReadBuffer<E> where
E: Unpin,
E: Unpin,
impl<E> UnwindSafe for BitReadBuffer<E> where
E: UnwindSafe,
E: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,