#[derive(UnitEnum)]
{
// Attributes available to this derive:
#[unit_enum]
}
Expand description
Derives the UnitEnum
trait for an enum.
This macro can be used on enums with unit variants (no fields) and optionally one “other” variant that can hold arbitrary discriminant values.
§Attributes
#[repr(type)]
: Optional for regular enums, defaults to i32. Required when using an “other” variant.#[unit_enum(other)]
: Marks a variant as the catch-all for undefined discriminant values. The type of this variant must match the repr type.
§Requirements
- The enum must contain only unit variants, except for one optional “other” variant
- The “other” variant, if present, must:
- Be marked with
#[unit_enum(other)]
- Have exactly one unnamed field matching the repr type
- Be the only variant with the “other” attribute
- Have a matching
#[repr(type)]
attribute
- Be marked with
§Examples
Basic usage with unit variants (repr is optional):
#[derive(UnitEnum)]
enum Example {
A,
B = 10,
C,
}
Usage with explicit repr:
#[derive(UnitEnum)]
#[repr(u16)]
enum Color {
Red = 10,
Green,
Blue = 45654,
}
Usage with an “other” variant (repr required):
#[derive(UnitEnum)]
#[repr(u16)]
enum Status {
Active = 1,
Inactive = 2,
#[unit_enum(other)]
Unknown(u16), // type must match repr
}