Struct bitbuffer::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,
impl<'a, E> BitReadBuffer<'a, E>where
E: Endianness,
sourcepub fn new(bytes: &'a [u8], _endianness: E) -> Self
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);
sourcepub fn to_owned(&self) -> BitReadBuffer<'static, E>
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,
impl<E> BitReadBuffer<'static, E>where
E: Endianness,
sourcepub fn new_owned(bytes: Vec<u8>, _endianness: E) -> Self
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,
impl<'a, E> BitReadBuffer<'a, E>where
E: Endianness,
sourcepub fn read_bool(&self, position: usize) -> Result<bool>
pub fn read_bool(&self, position: usize) -> Result<bool>
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);
sourcepub fn read_int<T>(&self, position: usize, count: usize) -> Result<T>
pub fn read_int<T>(&self, position: usize, count: usize) -> Result<T>
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);
sourcepub fn read_bytes(
&self,
position: usize,
byte_count: usize
) -> Result<Cow<'a, [u8]>>
pub fn read_bytes( &self, position: usize, byte_count: usize ) -> Result<Cow<'a, [u8]>>
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)?.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
]);
sourcepub fn read_string(
&self,
position: usize,
byte_len: Option<usize>
) -> Result<Cow<'a, str>>
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
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());
sourcepub fn read_float<T>(&self, position: usize) -> Result<T>where
T: Float + UncheckedPrimitiveFloat,
pub fn read_float<T>(&self, position: usize) -> Result<T>where
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§
source§impl<'a, E: Endianness> Clone for BitReadBuffer<'a, E>
impl<'a, E: Endianness> Clone for BitReadBuffer<'a, E>
source§impl<E: Endianness> Debug for BitReadBuffer<'_, E>
impl<E: Endianness> Debug for BitReadBuffer<'_, E>
source§impl<'a, E: Endianness> From<&'a [u8]> for BitReadBuffer<'a, E>
impl<'a, E: Endianness> From<&'a [u8]> for BitReadBuffer<'a, E>
source§impl<'a, E: Endianness> From<BitReadBuffer<'a, E>> for BitReadStream<'a, E>
impl<'a, E: Endianness> From<BitReadBuffer<'a, E>> for BitReadStream<'a, E>
source§fn from(buffer: BitReadBuffer<'a, E>) -> Self
fn from(buffer: BitReadBuffer<'a, E>) -> Self
Converts to this type from the input type.
source§impl<'a, E: Endianness> From<Vec<u8>> for BitReadBuffer<'a, E>
impl<'a, E: Endianness> From<Vec<u8>> for BitReadBuffer<'a, E>
source§impl<'a, E: Endianness> PartialEq for BitReadBuffer<'a, E>
impl<'a, E: Endianness> PartialEq for BitReadBuffer<'a, E>
Auto Trait Implementations§
impl<'a, E> Freeze for BitReadBuffer<'a, E>where
E: Sealed,
impl<'a, E> RefUnwindSafe for BitReadBuffer<'a, E>where
E: Sealed + 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: Sealed + Unpin,
impl<'a, E> UnwindSafe for BitReadBuffer<'a, E>where
E: Sealed + UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more