1#[macro_export]
2macro_rules! enum_array {
3 (
4 $(#[$attr:meta])*
5 $visibility:vis enum $name:ident {
6 $($id:ident),*$(,)?
7 }
8 ) => {
9 $(#[$attr])*
10 $visibility enum $name {
11 $($id),*
12 }
13 impl $name {
14 $visibility const ENTRIES: [$name; <[$name]>::len(&[$($name::$id),*])] = [$($name::$id),*];
15 }
16 };
17}
18
19#[cfg(test)]
20mod test {
21 #[test]
22 fn empty() {
23 enum_array!{
24 enum Empty {}
25 }
26 assert!(Empty::ENTRIES.is_empty())
27 }
28 #[test]
29 fn full() {
30 enum_array!{
31 #[derive(Debug, PartialEq, Eq)]
32 pub enum Example {
33 A,
34 B,
35 }
36 }
37 assert_eq!(Example::ENTRIES, [Example::A, Example::B])
38 }
39}