Reading and writing data types of arbitrary bit length that might not be byte-aligned
  • Rust 99.8%
  • Nix 0.2%
Find a file
Robin Appelman a07123315c
All checks were successful
Continuous integration / checks (push) Successful in 2m36s
Continuous integration / miri-tests (mips64-unknown-linux-gnuabi64) (push) Successful in 2m58s
Continuous integration / semver (push) Successful in 2m9s
Continuous integration / miri-tests (x86_64-unknown-linux-musl) (push) Successful in 2m56s
drop read_*_into for now as it doesn't seem to help much for the complexity
2025-09-16 23:50:45 +02:00
.forgejo/workflows update repo url 2025-05-23 23:20:14 +02:00
benches drop read_*_into for now as it doesn't seem to help much for the complexity 2025-09-16 23:50:45 +02:00
bitbuffer_derive move benchmarks to iai-callgrind 2025-07-13 22:23:18 +02:00
src drop read_*_into for now as it doesn't seem to help much for the complexity 2025-09-16 23:50:45 +02:00
tests fix some subbuffer end handling 2025-09-16 23:39:09 +02:00
.envrc flake 2022-08-06 15:27:15 +02:00
.gitignore update gitignore 2024-11-03 18:44:46 +01:00
Cargo.lock remove memchr 2025-09-16 23:39:09 +02:00
Cargo.toml remove memchr 2025-09-16 23:39:09 +02:00
flake.lock move benchmarks to iai-callgrind 2025-07-13 22:23:18 +02:00
flake.nix move benchmarks to iai-callgrind 2025-07-13 22:23:18 +02:00
LICENSE-APACHE switch licence to apache/mit 2019-02-22 22:47:56 +01:00
LICENSE-MIT switch licence to apache/mit 2019-02-22 22:47:56 +01:00
README.md add main to readme example 2025-05-23 23:27:02 +02:00
README.tpl optimize push_bits 2020-02-15 14:08:54 +01:00

Crates.io Documentation Dependency status

bitbuffer

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

  • read primitives, Strings and byte arrays, using read_bool, read_int, read_float, read_bytes and read_string
  • read any type implementing the BitRead or BitReadSized traits using read and read_sized
    • BitRead is for types that can be read without requiring any size info (e.g. null-terminal strings, floats, whole integers, etc)
    • BitReadSized is for types that require external sizing information to be read (fixed length strings, arbitrary length integers

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

  • write primitives, Strings and byte arrays, using write_bool, write_int, write_float, write_bytes and write_string
  • write any type implementing the BitWrite or BitWriteSized traits using write and write_sized
    • BitWrite is for types that can be written without requiring any size info (e.g. null-terminal strings, floats, whole integers, etc)
    • BitWriteSized is for types that require external sizing information to be written (fixed length strings, arbitrary length integers

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,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    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
    })?;
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.