Expand description
The bnb guide — worked, runnable walkthroughs.
Every page here is a normal rustdoc module whose examples are compiled and run as doctests, so nothing in the guide can drift from the code. Read the pages in this order (the same reading order given in the crate root), or jump to a topic:
quick_start— a five-minute tour of every macro.numbers—u1..u127and theBitstrait.bitfields—#[bitfield]: bit/byte order, widths, ranges, nesting.enums—#[derive(BitEnum)]: catch-all, closed,num_enumparity.flags—#[bitflags]: single-bit flag sets with set algebra.builders—#[derive(BitsBuilder)]: the required-by-default builder.bin_codec—#[bin]: a whole protocol header, end to end.directives— the field-directive reference, one example each.mapping—#[bin(map/bw_map = …)]: a whole struct mapped to/from a wire type.dispatch—#[bin]on an enum: tagged-union dispatch by wiremagicor off-wiretag.io— theSource/SinkI/O ladder.errors— position-aware errors and the streamingIncompletesignal.dual_use— compliant by default, deliberately violatable.composition— how the pieces nest and size each other.migrating— coming frombinrw/modular-bitfield/num_enum.
§How the crate fits together
One trait, Bits, is the keystone: a value that occupies a fixed
number of bits. The arbitrary-width integers (u1..u127), bool, the primitive
integers, and every type the macros generate all implement it, so they compose
as fields without glue:
#[bitfield]packs severalBitsvalues into one backing integer.#[derive(BitEnum)]makes an enum aBitsvalue (an integer discriminant).#[bitflags]makes a flag set aBitsvalue.#[bin]reads/writes a whole message ofBitsfields at arbitrary bit offsets, and a#[bitfield]/BitEnum/#[bitflags]drops in as one field.#[derive(BitsBuilder)]adds a required-by-default builder to any of the above.
Because the unit of composition is “a value of N bits”, a 5-bit enum nests in a 16-bit bitfield which nests in a byte-aligned message — all checked at compile time by const-evaluated width arithmetic, never by the proc-macro guessing widths.
Modules§
- bin_
codec #[bin]— the unified whole-message codec.- bitfields
#[bitfield]— pack typed fields into one backing integer.- builders
#[derive(BitsBuilder)]— a required-by-default builder.- composition
- Composition — how the pieces nest and size each other.
- directives
- Field-directive reference —
#[br](read),#[bw](write), and the standalone field attributes, one runnable example each. - dispatch
#[bin]on an enum — tagged-union dispatch.- dual_
use - Dual-use: compliant by default, deliberately violatable.
- enums
#[derive(BitEnum)]— map an enum to a fixed-width integer discriminant.- errors
- Errors — position-aware, and the streaming
Incompletesignal. - flags
#[bitflags]— a named set of single-bit flags with set algebra.- io
- The I/O ladder — where the codec reads from and writes to.
- mapping
- Whole-struct wire mapping — a logical type that serializes via a separate wire type.
- migrating
- Coming from
binrw,modular-bitfield, ornum_enum— how the mental models map. - numbers
- Arbitrary-width integers and the
Bitstrait. - quick_
start - A five-minute tour of every macro.