#cursor #bit #amounts #reading #buffer #io #bit-level

bit-cursor

Buffer cursor that supports bit-level operations

1 unstable release

0.1.0 May 23, 2022

#2338 in Algorithms

Download history 147/week @ 2024-01-08 137/week @ 2024-01-15 91/week @ 2024-01-22 123/week @ 2024-01-29 145/week @ 2024-02-05 189/week @ 2024-02-12 134/week @ 2024-02-19 192/week @ 2024-02-26 117/week @ 2024-03-04 117/week @ 2024-03-11 312/week @ 2024-03-18 370/week @ 2024-03-25 34/week @ 2024-04-01 69/week @ 2024-04-08 39/week @ 2024-04-15

512 downloads per month
Used in 3 crates (via packetrs-impl)

MIT license

19KB
355 lines

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 ux crate for types.

Examples

let data: Vec<u8> = vec![0b11110000, 0b00001111];
let mut cursor = BitCursor::new(data);

assert_eq!(cursor.bit_read::<u4>().unwrap(), 15);
assert_eq!(cursor.bit_read::<u4>().unwrap(), 0);
assert_eq!(cursor.bit_read::<u2>().unwrap(), 0);
assert_eq!(cursor.bit_read::<u6>().unwrap(), 15);

It also supports seeking via BitSeek, which is similar to Seek:

let data: Vec<u8> = vec![0b11110000, 0b00001111];
let mut cursor = BitCursor::new(data);

assert_eq!((1, 0), cursor.seek(BitSeekFrom::Start(1, 0)).unwrap());
assert_eq!(cursor.bit_read::<u4>().unwrap(), 0);

assert_eq!((0, 3), cursor.seek(BitSeekFrom::Current(0, -6)).unwrap());
assert_eq!(cursor.bit_read::<u4>().unwrap(), 0b1100);


assert_eq!((0, 3), cursor.seek(BitSeekFrom::End(0, -4)).unwrap());
assert_eq!(cursor.bit_read::<u4>().unwrap(), 0b1111);

Dependencies

~0.5–1MB
~23K SLoC