1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45/// Generates an enum value that supports an |all_variants| method that returns all
6/// possible values of the enum.
7#[macro_export]
8macro_rules! enumerable_enum {
9 ($(#[$meta:meta])* $name:ident {
10 $($(#[$variant_meta:meta])* $variant:ident),*,
11 }) => {
12 $(#[$meta])*
13pub enum $name {
14 $($(#[$variant_meta])* $variant),*
15 }
1617impl $name {
18pub fn all_variants() -> Vec<Self> {
19vec![
20 $($name::$variant),*
21 ]
22 }
23 }
24 }
25}
2627#[cfg(test)]
28mod test {
29#[test]
30fn test_all_variants() {
31enumerable_enum! {
32#[derive(Debug, PartialEq)]
33TestEnum {
34 VarOne,
35 VarTwo,
36 }
37 }
3839assert_eq!(TestEnum::all_variants(), vec![TestEnum::VarOne, TestEnum::VarTwo,]);
40 }
41}