bitreader

Trait ReadInto

Source
pub trait ReadInto
where 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);

Required Methods§

Source

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ReadInto for bool

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for i8

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for i16

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for i32

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for i64

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for u8

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for u16

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for u32

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Source§

impl ReadInto for u64

Source§

fn read(reader: &mut BitReader<'_>, bits: u8) -> Result<Self>

Implementors§