2 Copyright ⓒ 2015 rust-custom-derive contributors.
4 Licensed under the MIT license (see LICENSE or <http://opensource.org
5 /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
6 <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
7 files in the project carrying such notice may not be copied, modified,
8 or distributed except according to those terms.
10 #[macro_use] extern crate custom_derive;
12 macro_rules
! EnumIterator
{
13 (() $
(pub)* enum $name
:ident { $($body:tt)* }
) => {
15 @
collect_variants ($name
),
21 @
collect_variants ($name
:ident
),
22 ($
(,)*) -> ($
($var_names
:ident
,)*)
24 type NameIter
= ::std
::vec
::IntoIter
<&'
static str>;
25 type VariantIter
= ::std
::vec
::IntoIter
<$name
>;
29 pub fn iter_variants() -> VariantIter
{
30 vec
![$
($name
::$var_names
),*].into_iter()
34 pub fn iter_variant_names() -> NameIter
{
35 vec
![$
(stringify
!($var_names
)),*].into_iter()
41 @collect_variants $fixed
:tt
,
42 ($var
:ident $
(= $_val
:expr
)*, $
($tail
:tt
)*) -> ($
($var_names
:tt
)*)
45 @collect_variants $fixed
,
46 ($
($tail
)*) -> ($
($var_names
)* $var
,)
51 @
collect_variants ($name
:ident
),
52 ($var
:ident $_struct
:tt
, $
($tail
:tt
)*) -> ($
($var_names
:tt
)*)
54 const _error
: () = concat
!(
55 "cannot derive EnumIterator for ",
57 ", due to non-unitary variant ",
65 #[derive(Debug, PartialEq, EnumIterator)]
66 enum Get { Up, Down, AllAround }
70 fn test_enum_iterator() {
71 let vs
: Vec
<_
> = Get
::iter_variant_names().zip(Get
::iter_variants()).collect();
72 assert_eq
!(&*vs
, &[("Up", Get
::Up
), ("Down", Get
::Down
), ("AllAround", Get
::AllAround
)]);