Crates.io | bit-cursor |
lib.rs | bit-cursor |
version | 0.1.2 |
source | src |
created_at | 2022-05-23 20:50:06.475809 |
updated_at | 2024-09-06 04:03:27.467176 |
description | A cursor that supports bit-level reads and writes |
homepage | |
repository | |
max_upload_size | |
id | 592148 |
size | 43,743 |
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.
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));
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
.
BitCursor
BitCursor
is analogous to the std::io::Cursor
type, but its API is defined in terms of bits instead of bytes.