Macro rubedo::variants

source ·
macro_rules! variants {
    ($enum:ident: $variant:ident $(, $other_variants:ident)*) => { ... };
    ($enum:ident: $variant:ident $(, $other_variants:ident)*,) => { ... };
    ($enum:ident:) => { ... };
    () => { ... };
}
Expand description

Allows shorthand for referring to multiple variants of the same enum.

This macro provides syntactic sugar to specify multiple enum variants using reduced syntax, making such usage more concise, and easier to read.

It supports lists of variants separated by commas. It would be nice to also use the boolean OR operator for use in matches, but this is not possible at present, as match arms must be explicit, and cannot rely on expressions.

This macro returns the variants as a Vec, and there is an alternative macro, variants_hashset!, which returns a HashSet instead.

It is is exported as vv! (meaning “variants vector”) as well as variants!, to allow for more concise usage.

§Examples

use rubedo::sugar::vv;
 
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Foo {
    Bar,
    Baz,
    Qux,
}
 
assert_eq!(vv![ Foo: Bar, Baz, Qux ], vec![ Foo::Bar, Foo::Baz, Foo::Qux ]);
assert_eq!(vv![ Foo: ], vec![]);

§See also