| Crates.io | enum-bitmasks |
| lib.rs | enum-bitmasks |
| version | 0.1.0 |
| created_at | 2026-01-11 15:00:20.372391+00 |
| updated_at | 2026-01-11 15:00:20.372391+00 |
| description | A no_std compatible bitmask macro that can be implemented on enums. |
| homepage | |
| repository | https://github.com/PappAdam/bitmasks |
| max_upload_size | |
| id | 2035933 |
| size | 25,075 |
#[bitmask]Declares a bitmask definition using an enum and
generates a corresponding transparent bits type for runtime use.
This macro is intentionally low-level. It does not attempt to model permissions, states, or invariants. It only provides:
Given an enum definition:
#[bitmask(enable_auto_assign)]
#[repr(u8)]
pub enum Permissions {
Read,
Write,
#[compound(Read | Write)]
ReadWrite,
}
This macro:
#[compound(...)] expressions at compile timePermissionsBits wrapper around the underlying
integer representationA concrete integer representation is required:
#[repr(u8 | u16 | u32 | u64 | u128 | usize)]
Signed integer representations are not supported. Bitmasks are defined in terms of unsigned bitwise operations only.
Each enum variant must satisfy exactly one of the following:
Have an explicit discriminant:
A = 0b0001
Use #[compound(...)] to combine previously defined variants:
#[compound(A | B)]
C
Be automatically assigned a single-bit value only if
enable_auto_assign is enabled
Mixing these forms incorrectly is a compile-time error.
#[compound(...)]The #[compound] attribute allows defining a variant in terms of other
variants using constant expressions.
Supported expression forms:
|)!)Example:
#[compound(A | (B | C))]
D
Compound expressions are:
Cyclic definitions are rejected with a compile-time error.
enable_auto_assignWhen enabled, variants without explicit values or #[compound] are assigned
sequential single-bit values:
A = 1 << 0
B = 1 << 1
C = 1 << 2
Notes:
For an enum named Permissions, this macro generates:
pub struct PermissionsBits(repr_type);
Properties:
#[repr(transparent)]The bits type is a thin wrapper around the raw integer.
The following operators are implemented:
PermissionsBitsPermissionsBits valuesPermissionsBitsSupported operators:
|, |=
&, &=
^, ^=
!
-= (bit subtraction: a &= !b)
The following conversions are provided:
Permissions → PermissionsBitsPermissionsBits → repr_typerepr_type → PermissionsBitsPermissions → repr_typePermissionsBits implements Debug by attempting to decompose the stored
bits into known enum variants.
Example output:
PermissionsBits(Read | Write)
If no known variants match:
0 is printed as 0x0