Crate bitbuffer

source ·
Expand description

Tools for reading and writing data types of arbitrary bit length and might not be byte-aligned in the source data

The main way of reading the binary data is to first create a BitReadBuffer ,wrap it into a BitReadStream and then read from the stream.

Once you have a BitStream, there are 2 different approaches of reading data

The BitRead and BitReadSized traits can be used with #[derive] if all fields implement BitRead or BitReadSized.

For writing the data you wrap the output Vec into a BitWriteStream which can then be used in a manner similar to the BitReadStream

Just like the read counterparts, BitWrite and BitWriteSized traits can be used with #[derive] if all fields implement BitWrite or BitWriteSized.

§Examples

use bitbuffer::{BitReadBuffer, LittleEndian, BitReadStream, BitRead, BitWrite, BitWriteStream};

#[derive(BitRead, BitWrite)]
struct ComplexType {
    first: u8,
    #[size = 15]
    second: u16,
    third: bool,
}

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);
let value: u8 = stream.read_int(7)?;
let complex: ComplexType = stream.read()?;

let mut write_bytes = vec![];
let mut write_stream = BitWriteStream::new(&mut write_bytes, LittleEndian);
write_stream.write_int(12, 7)?;
write_stream.write(&ComplexType {
    first: 55,
    second: 12,
    third: true
})?;

Modules§

  • A number of traits to help being generic over numbers

Structs§

  • Marks the buffer or stream as big endian
  • Buffer that allows reading integers of arbitrary bit length and non byte-aligned integers
  • Stream that provides an easy way to iterate trough a BitBuffer
  • Stream that provides an a way to write non bit aligned adata
  • Struct that lazily reads it’s contents from the stream
  • Struct that lazily reads it’s contents from the stream
  • Marks the buffer or stream as little endian

Enums§

  • Errors that can be returned when trying to read from or write to a buffer

Traits§

  • Trait for types that can be read from a stream without requiring the size to be configured
  • Trait for types that can be read from a stream, requiring the size to be configured
  • Trait for types that can be written to a stream without requiring the size to be configured
  • Trait for types that can be written to a stream, requiring the size to be configured
  • Trait for specifying endianness of bit buffer

Functions§

  • Get the number of bits required to read a type from stream
  • Get the number of bits required to read a type from stream given an input size

Type Aliases§

Derive Macros§