3 releases

new 0.1.7 Apr 12, 2025
0.1.6 Apr 11, 2025
0.1.5 Apr 11, 2025

#42 in #bit

34 downloads per month

Apache-2.0/MIT

36KB
851 lines

bits-io

bits-io provides types which mimic those in std::io except which operate on the bit level instead of the byte level.

BitCursor

Mimics std::io::Cursor but tracks a bit-level position instead of a byte-level position. In addition to the standard Seek implementation which allows seeking by a number of bytes, it also provides BitSeek which allows seeking by a number of bits.

BitRead

BitRead mimics the std::io::Read trait, but its API is defined in terms of reading from "bit slices" instead of u8 slices (&[u8]) like std::io::Read. It leverages the BitSlice type defined in the bitvec crate.

BitWrite

BitWrite mimics the std::io::Write trait, but its API is defined in terms of reading from "bit slices" instead of u8 slices (&[u8]). It leverages the BitSlice type defined in the bitvec crate.

Examples

let data: Vec<u8> = vec![0b11100000, 0b11101111];
let mut cursor = BitCursor::from_vec(data);

// Read any non-standard-width type from the cursor
let u3_val = cursor.read_u3().unwrap();
assert_eq!(u3_val, nsw_types::u3::new(0b111));
// Sizes larger than 8 bits require a byte order argument
let u13_val = cursor
    .read_u13::<crate::byte_order::NetworkOrder>()
    .unwrap();
assert_eq!(u13_val, nsw_types::u13::new(0b0000011101111));

Dependencies

~1MB
~28K SLoC