Struct bitbuffer::BitReadStream [−][src]
pub struct BitReadStream<'a, E> where
E: Endianness, { /* fields omitted */ }
Expand description
Stream that provides an easy way to iterate trough a BitBuffer
Examples
use bitbuffer::{BitReadBuffer, BitReadStream, 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); let mut stream = BitReadStream::new(buffer);
Implementations
Create a new stream from a BitBuffer
Examples
use bitbuffer::{BitReadBuffer, BitReadStream, 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); let mut stream = BitReadStream::new(buffer);
Read a single bit from the stream as boolean
Errors
ReadError::NotEnoughData
: not enough bits available in the stream
Examples
assert_eq!(stream.read_bool()?, true); assert_eq!(stream.read_bool()?, false); assert_eq!(stream.pos(), 2);
Read a sequence of bits from the stream as integer
Errors
ReadError::NotEnoughData
: not enough bits available in the streamReadError::TooManyBits
: to many bits requested for the chosen integer type
Examples
assert_eq!(stream.read_int::<u16>(3)?, 0b101); assert_eq!(stream.read_int::<u16>(3)?, 0b110); assert_eq!(stream.pos(), 6);
Read a sequence of bits from the stream as float
Errors
ReadError::NotEnoughData
: not enough bits available in the stream
Examples
let result = stream.read_float::<f32>()?; assert_eq!(stream.pos(), 32);
Read a series of bytes from the stream
Errors
ReadError::NotEnoughData
: not enough bits available in the stream
Examples
let bytes = vec![ assert_eq!(stream.read_bytes(3)?.to_vec(), &[0b1011_0101, 0b0110_1010, 0b1010_1100]); assert_eq!(stream.pos(), 24);
Read a series of bytes from the stream as utf8 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 streamReadError::Utf8Error
: the read bytes are not valid utf8
Examples
// Fixed length string stream.set_pos(0); assert_eq!(stream.read_string(Some(11))?, "Hello world".to_owned()); assert_eq!(11 * 8, stream.pos()); // fixed length with null padding stream.set_pos(0); assert_eq!(stream.read_string(Some(16))?, "Hello world".to_owned()); assert_eq!(16 * 8, stream.pos()); // null terminated stream.set_pos(0); assert_eq!(stream.read_string(None)?, "Hello world".to_owned()); assert_eq!(12 * 8, stream.pos()); // 1 more for the terminating null byte
Read a sequence of bits from the stream as a BitStream
Errors
ReadError::NotEnoughData
: not enough bits available in the stream
Examples
let mut bits = stream.read_bits(3)?; assert_eq!(stream.pos(), 3); assert_eq!(bits.pos(), 0); assert_eq!(bits.bit_len(), 3); assert_eq!(stream.read_int::<u8>(3)?, 0b110); assert_eq!(bits.read_int::<u8>(3)?, 0b101); assert_eq!(true, bits.read_int::<u8>(1).is_err());
Skip a number of bits in the stream
Errors
ReadError::NotEnoughData
: not enough bits available in the stream to skip
Examples
stream.skip_bits(3)?; assert_eq!(stream.pos(), 3); assert_eq!(stream.read_int::<u8>(3)?, 0b110);
Set the position of the stream
Errors
ReadError::IndexOutOfBounds
: new position is outside the bounds of the stream
Examples
stream.set_pos(3)?; assert_eq!(stream.pos(), 3); assert_eq!(stream.read_int::<u8>(3)?, 0b110);
Get the current position in the stream
Examples
assert_eq!(stream.pos(), 0); stream.skip_bits(5)?; assert_eq!(stream.pos(), 5);
Get the number of bits left in the stream
Examples
assert_eq!(stream.bits_left(), 64); stream.skip_bits(5)?; assert_eq!(stream.bits_left(), 59);
Read a value based on the provided type
Examples
let int: u8 = stream.read()?; assert_eq!(int, 0b1011_0101); let boolean: bool = stream.read()?; assert_eq!(false, boolean);
use bitbuffer::BitRead; #[derive(BitRead, Debug, PartialEq)] struct ComplexType { first: u8, #[size = 15] second: u16, third: bool, } let data: ComplexType = stream.read()?; assert_eq!(data, ComplexType { first: 0b1011_0101, second: 0b010_1100_0110_1010, third: true, });
Read a value based on the provided type and size
The meaning of the size parameter differs depending on the type that is being read
Examples
let int: u8 = stream.read_sized(7)?; assert_eq!(int, 0b011_0101);
let data: Vec<u16> = stream.read_sized(3)?; assert_eq!(data, vec![0b0110_1010_1011_0101, 0b1001_1001_1010_1100, 0b1001_1001_1001_1001]);
Check if we can read a number of bits from the stream
Create an owned copy of this stream
Trait Implementations
Read the type from stream
The number of bits that will be read or None if the number of bits will change depending on the bit stream Read more
Write the type to stream
Write the type to stream
Performs the conversion.
Auto Trait Implementations
impl<'a, E> !RefUnwindSafe for BitReadStream<'a, E>
impl<'a, E> !Send for BitReadStream<'a, E>
impl<'a, E> !Sync for BitReadStream<'a, E>
impl<'a, E> Unpin for BitReadStream<'a, E> where
E: Unpin,
impl<'a, E> UnwindSafe for BitReadStream<'a, E> where
E: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more