Attribute Macro to_bitflags

Source
#[to_bitflags]
Expand description

Converts a regular enum into a bitflags-compatible type using its variant values.

The #[to_bitflags] attribute macro transforms a plain Rust enum definition into a bitflags struct, while keeping the original enum syntax intact. This is particularly useful when you want to use bitflags functionality without switching to a bitflags! macro or changing enum semantics.

§Syntax

#[commonlibsse_ng_derive_internal::to_bitflags]
#[derive(Default)]
pub enum MyFlags {
    A = 0b0001,
    B = 0b0010,
    C = 0b0100,

    #[default] // Optional, sets the default bitflags (requires `#[derive(Default)]`)
    None = 0,
}

Optionally, you can specify the output type name using the fn_name parameter:

#[commonlibsse_ng_derive_internal::to_bitflags(fn_name = "MyFlagsBits")]
pub enum MyFlags {
    A = 1,
    B = 2,
}

§Attributes

AttributeTypeRequiredDescription
fn_name&strNoThe name of the generated bitflags struct. Defaults to the enum name.

§Features

  • Automatically implements bitflags! for the enum using its variant values.
  • Honors #[default] on a variant if #[derive(Default)] is also used.
  • Avoids the need to rewrite your enum as a bitflags! block.
  • Works with enums using explicit discriminant values.

§Example

#[commonlibsse_ng_derive_internal::to_bitflags]
#[derive(Default)]
pub enum RenderFlags {
    Alpha = 0b0001,
    Depth = 0b0010,
    Stencil = 0b0100,

    #[default]
    None = 0,
}

let flags = RenderFlags::ALPHA | RenderFlags::DEPTH;
assert!(flags.contains(RenderFlags::ALPHA));

§Notes

  • The macro expects all enum variants to have constant discriminant values (e.g., integers or const exprs).
  • The original enum is preserved in the output (i.e., not removed or renamed).

§Dependencies

  • #[derive(Default)] and #[default] can be used to control the default value of the generated flag struct.