[−][src]Crate bitter
Bitter takes a slice of byte data and reads little-endian bits platform agonistically.
There are two main APIs available: checked and unchecked functions. A checked function will
return a Option
that will be None
if there is not enough bits left in the stream.
Unchecked functions, which are denoted by having "unchecked" in their name, will exhibit
undefined behavior if there is not enough data left, but happen to be 2x faster (your numbers
will vary depending on use case).
Tips:
- Prefer checked functions for all but the most performance critical code
- Group all unchecked functions in a single block guarded by a
has_bits_remaining
call
Example
use bitter::BitGet; let mut bitter = BitGet::new(&[0xff, 0x04]); assert_eq!(bitter.read_bit(), Some(true)); assert_eq!(bitter.read_u8(), Some(0x7f)); assert_eq!(bitter.read_u32_bits(7), Some(0x02));
Below, is a demonstration of guarding against potential panics:
let mut bitter = BitGet::new(&[0xff, 0x04]); if bitter.has_bits_remaining(16) { assert_eq!(bitter.read_bit_unchecked(), true); assert_eq!(bitter.read_u8_unchecked(), 0x7f); assert_eq!(bitter.read_u32_bits_unchecked(7), 0x02); }
Another guard usage:
let mut bitter = BitGet::new(&[0xff, 0x04]); if bitter.has_bits_remaining(16) { for _ in 0..8 { assert_eq!(bitter.read_bit_unchecked(), true); } assert_eq!(bitter.read_u8_unchecked(), 0x04); }
Structs
BitGet | Reads little-endian bits platform agonistically from a slice of byte data. |
Functions
bit_width | Calculates the width of the input in bits |