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

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

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

Examples

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

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

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

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

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

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

Get the length of the stream in bits

Examples

assert_eq!(stream.bit_len(), 64);

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

Skip the type Read more

Write the type to stream

Write the type to stream

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.