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".

Implementations on Foreign Types§

Source§

impl BitDecode for Ipv4Addr

Available on crate feature std only.
Source§

impl BitDecode for Ipv6Addr

Available on crate feature std only.
Source§

impl BitDecode for bool

Source§

impl BitDecode for u8

Source§

impl BitDecode for u16

Source§

impl BitDecode for u32

Source§

impl BitDecode for u64

Source§

impl BitDecode for u128

Implementors§

Source§

impl<T, const N: usize> BitDecode for UInt<T, N>
where UInt<T, N>: Bits,

Source§

impl<T: Bits> BitDecode for WireLen<T>