Macro bit_variants

Source
macro_rules! bit_variants {
    ($pub:vis $mod:ident;$ty:ty;$($name:ident),+$(,)?) => { ... };
    ($count:expr;$ty:ty;$name:ident $($rest:ident)* ) => { ... };
    ($count:expr;$ty:ty;) => { ... };
}
Expand description

A macro to generate a module containing bitwise constants based on a list of identifiers.

§Usage

bit_variants!(pub my_bits; u64; FLAG_A, FLAG_B, FLAG_C);

This example expands to:

pub mod my_bits {
    pub const FLAG_A: u64 = 1 << 0;
    pub const FLAG_B: u64 = 1 << 1;
    pub const FLAG_C: u64 = 1 << 2;
}

§Syntax

bit_variants!(pub_visibility module_name; type; identifier1, identifier2, ...);
  • pub_visibility: Visibility of the generated module (e.g., pub).
  • module_name: Name of the generated module.
  • type: The type for the bitwise constants (e.g., u64).
  • identifier1, identifier2, ...: A comma-separated list of identifiers to be used as constants.

§Details

  • The macro uses recursive invocations to generate constants.
  • The bit position starts at 0 and increments for each identifier.
  • Each constant is calculated as 1 << bit_position.

This macro is useful for scenarios where you need a set of bitflags or unique identifiers represented as powers of two.