[−][src]Derive Macro enum_utils::IterVariants
#[derive(IterVariants)]
{
// Attributes available to this derive:
#[enumeration]
}Derives a static method, iter(), which iterates over the variants of an enum.
Examples
Variants are yielded in the order they are defined. If the discriminants of the enum form a
single, increasing run and #[repr(...)] is specified as in the following example, a fast
implementation of iter can be generated which does not require the enum to implement Clone.
/// The discriminants of this enum are `[1, 2, 3, 4]`. #[derive(Debug, PartialEq, Eq, enum_utils::IterVariants)] #[repr(u8)] pub enum Direction { North = 1, East, South, West, } use Direction::*; assert_eq!(Direction::iter().collect::<Vec<_>>(), vec![North, East, South, West]);
If the preceding conditions are not met, Clone must be implemented to successfully derive
IterVariants. enum_utils will create a const array containing each variant and iterate
over that.
#[derive(Debug, Clone, Copy, PartialEq, Eq, enum_utils::IterVariants)] #[repr(u16)] pub enum Bitmask { Empty = 0x0000, Full = 0xffff, } use Bitmask::*; assert_eq!(Bitmask::iter().collect::<Vec<_>>(), vec![Empty, Full]);
Named constants or complex expressions (beyond an integer literal) are not evaluated when used
as a discriminant and will cause IterVariants to default to the Clone-based implementation.
#[derive(Debug, PartialEq, Eq, enum_utils::IterVariants)] // Missing `Clone` impl #[repr(u8)] pub enum Bitmask { Bit1 = 1 << 0, Bit2 = 1 << 1, } use Bitmask::*; assert_eq!(Bitmask::iter().collect::<Vec<_>>(), vec![Empty, Full]);
Attributes
#[enumeration(skip)]
Use #[enumeration(skip)] to avoid iterating over a variant. This can be useful when an enum
contains a "catch-all" variant.
#[derive(Debug, Clone, Copy, PartialEq, Eq, enum_utils::IterVariants)] pub enum Http2FrameType { Data, Headers, /* ... */ Continuation, #[enumeration(skip)] Unknown(u8), } use Http2FrameType::*; assert_eq!(Http2FrameType::iter().collect::<Vec<_>>(), vec![Data, Headers, /* ... */ Continuation]);