#[derive(BitEnum)]
{
// Attributes available to this derive:
#[bit_enum]
#[catch_all]
}
Expand description
Derives an enum ⇄ integer mapping of a fixed bit width.
#[derive(BitEnum, Clone, Copy, Debug, PartialEq, Eq)]
#[bit_enum(u4)]
enum RCode {
NoError = 0,
FormErr = 1,
#[catch_all]
Other(u4), // preserves unknown values (dual-use)
}#[bit_enum(uN)] sets the width. Exactly one #[catch_all] tuple variant
(holding a uN/integer) may capture unknown discriminants. Without a catch-all
the variants must cover the whole width, or the enum must be declared
#[bit_enum(uN, closed)] to assert a closed set — otherwise it is a compile
error, because from_bits (the infallible codec / #[bitfield]-getter path)
would panic on an unknown discriminant. A closed enum still unreachable!s on
that path; its checked TryFrom rejects unknowns instead.
§Generated API
Always: the bnb::{Bits, BitEnum} impls (so the enum nests in a
#[bitfield]). For a primitive width (u8/u16/u32/u64/u128)
additionally — for num_enum parity:
From<Enum> for uN(every variant maps to a value);- with
#[catch_all]:From<uN> for Enum(total — unknowns absorbed); - without it:
TryFrom<uN> for Enum, erroring withbnb::UnknownDiscriminanton an unknown value.
A non-primitive width (u4, or even a byte-aligned u24) gets none of these — such an
enum is only meaningful nested in a #[bitfield].
See the bnb::guide::enums page for runnable examples (catch-all, closed, the
num_enum parity, and nesting).