Skip to main content

enum_table/
macros.rs

1/// A macro to create an `EnumTable` for a given enumeration and value type.
2///
3/// # Arguments
4///
5/// * `$variant` - The enumeration type that implements the `Enumable` trait.
6/// * `$value` - The type of values to be associated with each enumeration variant.
7/// * `$count` - The number of variants in the enumeration.
8/// * `$variable` - The variable name to use in the closure for each variant.
9/// * `$($tt:tt)*` - The closure that maps each variant to a value.
10///
11/// # Example
12///
13/// ```rust
14/// use enum_table::{EnumTable, Enumable, et};
15///
16/// #[derive(Copy, Clone)]
17/// enum Test {
18///     A,
19///     B,
20///     C,
21/// }
22///
23/// impl enum_table::Enumable for Test {
24///     const VARIANTS: &'static [Self] = &[Test::A, Test::B, Test::C];
25/// }
26///
27/// const TABLE: EnumTable<Test, &'static str, { Test::COUNT }> =
28///     et!(Test, &'static str, |t| match t {
29///         Test::A => "A",
30///         Test::B => "B",
31///         Test::C => "C",
32///     });
33///
34/// assert_eq!(TABLE.get(&Test::A), &"A");
35/// assert_eq!(TABLE.get(&Test::B), &"B");
36/// assert_eq!(TABLE.get(&Test::C), &"C");
37///
38#[macro_export]
39macro_rules! et {
40    ($variant:ty, $value:ty, $COUNT:block, |$variable:ident| $($tt:tt)*) => {
41        {
42            let mut builder = $crate::builder::EnumTableBuilder::<$variant, $value, $COUNT>::new();
43
44            let mut i = 0;
45            while i < builder.capacity() {
46                let $variable = &<$variant as $crate::Enumable>::VARIANTS[i];
47                let value = $($tt)*;
48                unsafe {
49                    builder.push_unchecked($variable, value);
50                }
51                i += 1;
52            }
53
54            unsafe { builder.build_to_unchecked() }
55        }
56    };
57    ($variant:ty, $value:ty, |$variable:ident| $($tt:tt)*) => {
58        $crate::et!($variant, $value, { <$variant as $crate::Enumable>::COUNT }, |$variable| $($tt)*)
59    };
60}
61
62#[cfg(test)]
63mod tests {
64    use crate::{EnumTable, Enumable};
65
66    #[test]
67    fn et_macro() {
68        #[derive(Clone, Copy, Enumable)]
69        enum Test {
70            A,
71            B,
72            C,
73        }
74
75        const TABLE: EnumTable<Test, &'static str, { Test::COUNT }> =
76            et!(Test, &'static str, |t| match t {
77                Test::A => "A",
78                Test::B => "B",
79                Test::C => "C",
80            });
81
82        assert_eq!(TABLE.get(&Test::A), &"A");
83        assert_eq!(TABLE.get(&Test::B), &"B");
84        assert_eq!(TABLE.get(&Test::C), &"C");
85    }
86}