Why this fires¶
A flag's value is neither a single power-of-two bit nor a combination of flags declared earlier in the same enum. @flags variants must each be either a fresh bit (auto-incremented, or an explicit power of two) or a named composite built from bits already declared above it.
@flags
enum Perm: u8 {
All = 3,
// ^ error[TE120]: flag `Perm::All` (value 3) is neither a power of two
// nor a composite of previously-declared flags
Read,
Write,
}3 (0b11) would be a valid composite of Read | Write — but only after Read and Write are declared, since composites can only reference bits declared earlier.
Fix it¶
1. Declare the bits first, then the composite¶
@flags
enum Perm: u8 {
Read,
Write,
All = 3, // ok — Read (1) | Write (2)
}2. Use a real power of two¶
@flags
enum Perm: u8 {
Read = 1, // ok
Write = 2,
}See also¶
TE121— a flag value doesn't fit the backing type.TE122— two flags occupy the same bit.- /docs/data-structures#enums —
@flagsbit-flag enums.