Skip to main content

bitflags

Attribute Macro bitflags 

Source
#[bitflags]
Expand description

Packs named single-bit flags into one backing integer, with set algebra.

#[bitflags(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct TcpFlags {
    fin: bool,   // bit 0 (auto, LSB-first)
    syn: bool,   // bit 1
    #[flag(5)] ack: bool, // pinned to bit 5
}

§Attribute arguments

  • backing (first, required): u8/u16/u32/u64/u128.
  • bytes = big | little (default big): byte order when serialized.

§Generated API

A const per flag (upper-cased: finTcpFlags::FIN); empty()/all()/ bits()/from_bits (retains unknown bits) / from_bits_truncate; contains/intersects/is_empty/insert/remove/toggle/set; union/intersection/difference/complement (for combination consts); per-flag fin()/with_fin(bool)/set_fin(bool); iter(); the | & ^ - ! (+ assign) operators; and Bits/Bitfield impls so a flag set nests in a #[bitfield] and serializes. Everything except iter() and the operator impls is a const fn, and the emitted struct is #[repr(transparent)] over its backing (suppressed by writing your own #[repr(...)]), as for #[bitfield].

See the bnb::guide::flags page for runnable examples (set algebra, iteration, retain-vs-truncate, nesting).