pub trait ReadIntowhere
Self: Sized,{
// Required method
fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>;
}
Expand description
Helper trait to allow reading bits into a variable without explicitly mentioning its type.
If you can’t or want, for some reason, to use BitReader’s read methods (read_u8
etc.) but
want to rely on type inference instead, you can use the ReadInto trait. The trait is
implemented for all basic integer types (8/16/32/64 bits, signed/unsigned)
and the boolean type.
use bitreader::{BitReader,ReadInto};
let slice_of_u8 = &[0b1110_0000];
let mut reader = BitReader::new(slice_of_u8);
struct Foo {
bar: u8,
valid: bool,
}
// No type mentioned here, instead the type of bits is inferred from the type of Foo::bar,
// and consequently the correct "overload" is used.
let bits = ReadInto::read(&mut reader, 2).unwrap();
let valid = ReadInto::read(&mut reader, 1).unwrap();
let foo = Foo { bar: bits, valid: valid };
assert_eq!(foo.bar, 3);
assert!(foo.valid);