Struct bitbuffer::BitReadBuffer[][src]

pub struct BitReadBuffer<'a, E> where
    E: Endianness
{ /* fields omitted */ }
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

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

Create a static version of this buffer

If the current buffer is borrowed, this will copy the data

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

The available number of bits in the buffer

The available number of bytes in the buffer

Read a single bit from the buffer as boolean

Errors

Examples

let result = buffer.read_bool(5)?;
assert_eq!(result, true);

Read a sequence of bits from the buffer as integer

Errors

Examples

let result = buffer.read_int::<u16>(10, 9)?;
assert_eq!(result, 0b100_0110_10);

Read a series of bytes from the buffer

Errors

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

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

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

Read a sequence of bits from the buffer as float

Errors

Examples

let result = buffer.read_float::<f32>(10)?;

Trait Implementations

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.

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.