Skip to main content

BitDecode

Trait BitDecode 

Source
pub trait BitDecode: Sized {
    // Required method
    fn bit_decode<S: Source>(r: &mut S) -> Result<Self, BitError>;
}
Expand description

A message decoded from a bit stream — the recursion point a #[derive(BitDecode)] struct implements (reading each field in declaration order). Leaf fields are any Bits type; nested messages recurse. Fixed- or variable-length; a fixed-length message also implements FixedBitLen.

Most users reach for #[bin] (which derives this plus BitEncode and a builder); the bare derives are the codec on its own, for fields that straddle byte boundaries.

§Examples

use bnb::{BitDecode, BitEncode, u4, u12};

// A 4-bit tag + a 12-bit length, straddling the byte boundary.
#[derive(BitDecode, BitEncode, Debug, PartialEq)]
struct Frame { tag: u4, len: u12 }

let f = Frame::decode_exact(&[0xAB, 0xCD]).unwrap();
assert_eq!(f, Frame { tag: u4::new(0xA), len: u12::new(0xBCD) });
assert_eq!(f.to_bytes().unwrap(), [0xAB, 0xCD]); // round-trips

Required Methods§

Source

fn bit_decode<S: Source>(r: &mut S) -> Result<Self, BitError>

Decodes Self from any Source, advancing its cursor.

§Errors

Propagates the source’s BitError.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§