macro_rules! enum_iter {
(@all_array_impl,
$vis:vis,
~const_array($all_array:ident)
$($variant:ident)*
) => { ... };
(@all_array_impl,
$vis:vis,
~const_array($all_array:ident)
$($variant:ident $((
$nested_enum:ty
))?)*
) => { ... };
(@all_array_impl,
$vis:vis,
$($variant:ident $((
$nested_enum:ty
))?)*
) => { ... };
(@add_variant_impl, $buff:ident, $variant:ident $nested_enum:ty) => { ... };
(@add_variant_impl, $buff:ident, $variant:ident) => { ... };
(
$(~const_array($all_array:ident))?
$( #[ $enum_attr:meta ] )*
$vis:vis enum $enum_name:ident {
$(
$( #[ $variant_attr:meta ] )*
$variant:ident $(($nested_enum:ty))? $(= $idx:literal)?
),* $(,)?
}
) => { ... };
}
Expand description
strum::EnumIter alternative that supports nested enums
Implements following items:
NUM_KINDS
associated constant, number of defined kinds.iter
function, returns the iterator of all possible variants, including ones from the nested variants.
If you use ~const_array
prefix, you can also generate ALL constant, which
has a constant array of all possible values, but only available for simple
enums.
ยงExample
enum_iter! {
~const_array(ALL)
#[derive(Eq, PartialEq, Debug)]
enum Shade {
Good,
Meh,
Bad,
}
}
enum_iter! {
#[derive(Debug, Eq, PartialEq)]
#[repr(u8)]
enum Color {
Green = 1,
Red(Shade) = 2,
Blue = 3,
}
}
assert_eq!(Shade::NUM_KINDS, 3);
const ALL_SHADES: [Shade; Shade::NUM_KINDS] = Shade::ALL;
assert_eq!(ALL_SHADES, [Shade::Good, Shade::Meh, Shade::Bad]);
let results: Vec<_> = Shade::iter().collect();
assert_eq!(results, vec![Shade::Good, Shade::Meh, Shade::Bad]);
let results: Vec<_> = Color::iter().collect();
assert_eq!(results, vec![
Color::Green,
Color::Red(Shade::Good),
Color::Red(Shade::Meh),
Color::Red(Shade::Bad),
Color::Blue,
]);