#bit-level #read-write #io-read #io-write #cursor #non-standard #byte

bit-cursor

A cursor that supports bit-level reads and writes

3 releases

0.1.2 Sep 6, 2024
0.1.1 Jul 23, 2024
0.1.0 May 23, 2022

#717 in Algorithms

Download history 167/week @ 2024-06-14 103/week @ 2024-06-21 81/week @ 2024-06-28 54/week @ 2024-07-05 53/week @ 2024-07-12 228/week @ 2024-07-19 71/week @ 2024-07-26 14/week @ 2024-08-02 5/week @ 2024-08-09 13/week @ 2024-08-16 77/week @ 2024-08-23 116/week @ 2024-08-30 224/week @ 2024-09-06 27/week @ 2024-09-13 91/week @ 2024-09-20 143/week @ 2024-09-27

509 downloads per month
Used in 4 crates (2 directly)

Apache-2.0/MIT

43KB
1K SLoC

BitCursor

BitCursor is similar to std::io::Cursor, but allows reading various amounts of bits from a given buffer in addition to byte-sized chunks. It's built on top of the nsw_types crate for non-standard-width types and leverages bitvec to provide a more complete implementation.

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

Design

Traits

BitRead

BitRead is analogus to 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.

BitWrite

BitWrite is analogus to the std::io::Write trait, but its API is defined in terms of writing to "bit slices" instead of u8 slices (&[u8]) like std::io::Write.

Types

BitCursor

BitCursor is analogous to the std::io::Cursor type, but its API is defined in terms of bits instead of bytes.

Dependencies

~1MB
~28K SLoC