Skip to main content

bitflags_quick

Macro bitflags_quick 

Source
macro_rules! bitflags_quick {
    ($name:ident, $($bit_name:ident : { $($bits:expr),* $(,)? }),* $(,)?) => { ... };
}
Expand description

Defines quick named compositions of existing flags.

bitflags! only accepts literal values for const X = ..., so it cannot express something like const AB = A | B;. This helper macro adds extra factory methods to an existing flags type, each one OR-ing together a list of $T expressions and truncating any bits that are not declared on the type.

§Example

bitflags! {
    pub struct Flags: u32 {
        const A = 0b001;
        const B = 0b010;
        const C = 0b100;
    }
}

bitflags_quick! {
    Flags,
    ab: { Flags::A().bits(), Flags::B().bits() },
    ac: { Flags::A().bits(), Flags::C().bits() },
}

let f = Flags::ab();          // bits == 0b011
assert!(f.contains(Flags::A()));