Skip to main content

BitDecode

Derive Macro BitDecode 

Source
#[derive(BitDecode)]
{
    // Attributes available to this derive:
    #[bit_stream]
    #[nested]
    #[br]
    #[bw]
    #[brw]
    #[reserved]
    #[reserved_with]
}
Expand description

Derives a bnb::BitDecode impl that reads the struct’s named fields, in declaration order, from a bit cursor (a bnb::BitReader).

#[derive(BitDecode, BitEncode, Copy, Clone, Debug, PartialEq, Eq)]
struct GenericBurst {   // a 264-bit DMR burst, fields at bit offsets
    p1: u108,           // bits   0..108
    pattern: SyncPattern,// bits 108..156 (a 48-bit #[derive(BitEnum)])
    p2: u108,           // bits 156..264
}

Each leaf field is read with Source::read, so any bnb::Bits type (u1..u127, #[bitfield], #[derive(BitEnum)]) works as a field — no byte-alignment, seeks, or shift glue. A field marked #[nested] is itself a BitDecode/BitEncode message and is recursed into (a fixed one’s FixedBitLen::BIT_LEN counts toward the parent’s). [u8; N] payloads and #[br(count = …)] Vecs are supported; a count-bearing message is variable-length and so does not implement bnb::FixedBitLen.

§Which codec? (the derive steers you)

Your dataUseWhy
Fields straddle byte boundaries (e.g. 108 | 48 | 108 bits)#[derive(BitDecode/BitEncode)] or #[bin]reads at arbitrary bit offsets
A whole message (magic/counts/Vec/nesting/ctx/…), bit- or byte-aligned#[bin]the unified codec
A run of sub-byte fields that fills one integer#[bitfield]one packed word with named accessors

To enforce that, the bare derive rejects an all-byte-aligned struct at compile time (every field a whole number of bytes ⇒ the cursor never leaves byte boundaries ⇒ #[bin] is the better tool). Override with the struct-level #[bit_stream(allow_byte_aligned)] when you really mean it.

See the bnb::guide::directives page for a runnable example of each #[br]/#[bw] directive, and bnb::guide::bin_codec for the #[bin] front-end.