Struct BitReadStream

Source
pub struct BitReadStream<'a, E>
where E: Endianness,
{ /* private fields */ }
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§

Source§

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

Source

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

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);
Source

pub fn read_bool(&mut self) -> Result<bool>

Read a single bit from the stream as boolean

§Errors
§Examples
assert_eq!(stream.read_bool()?, true);
assert_eq!(stream.read_bool()?, false);
assert_eq!(stream.pos(), 2);
Source

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

Read a sequence of bits from the stream as integer

§Errors
§Examples
assert_eq!(stream.read_int::<u16>(3)?, 0b101);
assert_eq!(stream.read_int::<u16>(3)?, 0b110);
assert_eq!(stream.pos(), 6);
Source

pub fn read_float<T>(&mut self) -> Result<T>

Read a sequence of bits from the stream as float

§Errors
§Examples
let result = stream.read_float::<f32>()?;
assert_eq!(stream.pos(), 32);
Source

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

Read a series of bytes from the stream

§Errors
§Examples
let bytes = vec![
assert_eq!(stream.read_bytes(3)?.to_vec(), &[0b1011_0101, 0b0110_1010, 0b1010_1100]);
assert_eq!(stream.pos(), 24);
Source

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

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
§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
Source

pub fn read_bits(&mut self, count: usize) -> Result<Self>

Read a sequence of bits from the stream as a BitStream

§Errors
§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());
Source

pub fn skip_bits(&mut self, count: usize) -> Result<()>

Skip a number of bits in the stream

§Errors
§Examples
stream.skip_bits(3)?;
assert_eq!(stream.pos(), 3);
assert_eq!(stream.read_int::<u8>(3)?, 0b110);
Source

pub fn align(&mut self) -> Result<usize>

Align the stream on the next byte and returns the amount of bits read

§Errors
§Examples
stream.align()?;
assert_eq!(stream.pos(), 0);

stream.skip_bits(3)?;
assert_eq!(stream.pos(), 3);
stream.align();
assert_eq!(stream.pos(), 8);
assert_eq!(stream.read_int::<u8>(4)?, 0b1010);
Source

pub fn set_pos(&mut self, pos: usize) -> Result<()>

Set the position of the stream

§Errors
§Examples
stream.set_pos(3)?;
assert_eq!(stream.pos(), 3);
assert_eq!(stream.read_int::<u8>(3)?, 0b110);
Source

pub fn bit_len(&self) -> usize

Get the length of the stream in bits

§Examples
assert_eq!(stream.bit_len(), 64);
Source

pub fn pos(&self) -> usize

Get the current position in the stream

§Examples
assert_eq!(stream.pos(), 0);
stream.skip_bits(5)?;
assert_eq!(stream.pos(), 5);
Source

pub fn bits_left(&self) -> usize

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);
Source

pub fn read<T: BitRead<'a, E>>(&mut self) -> Result<T>

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,
});
Source

pub fn read_sized<T: BitReadSized<'a, E>>(&mut self, size: usize) -> Result<T>

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]);
Source

pub fn peek<T: BitRead<'a, E>>(&mut self) -> Result<T>

Read a value based on the provided type without advancing the stream

Source

pub fn peek_sized<T: BitReadSized<'a, E>>(&mut self, size: usize) -> Result<T>

Read a value based on the provided type and size without advancing the stream

Source

pub fn check_read(&self, count: usize) -> Result<bool>

Check if we can read a number of bits from the stream

Source

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

Create an owned copy of this stream

Trait Implementations§

Source§

impl<'a, E: Endianness> BitReadSized<'a, E> for BitReadStream<'a, E>

Source§

fn read(stream: &mut BitReadStream<'a, E>, size: usize) -> Result<Self>

Read the type from stream
Source§

fn bit_size_sized(size: usize) -> Option<usize>

The number of bits that will be read or None if the number of bits will change depending on the bit stream
Source§

fn skip(stream: &mut BitReadStream<'a, E>, size: usize) -> Result<()>

Skip the type Read more
Source§

impl<E: Endianness> BitWrite<E> for BitReadStream<'_, E>

Source§

fn write(&self, stream: &mut BitWriteStream<'_, E>) -> Result<()>

Write the type to stream
Source§

impl<E: Endianness> BitWriteSized<E> for BitReadStream<'_, E>

Source§

fn write_sized( &self, stream: &mut BitWriteStream<'_, E>, len: usize, ) -> Result<()>

Write the type to stream
Source§

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

Source§

fn clone(&self) -> Self

Returns a duplicate 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<'a, E> Debug for BitReadStream<'a, E>
where E: Endianness + Debug,

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 BitReadStream<'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> PartialEq for BitReadStream<'_, 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 BitReadStream<'a, E>

§

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

§

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§

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.