aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32-wpan/src/wb55/mac/macros.rs
blob: 1a988a7794a863031daaf68db8dbebb8e68a7f82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#[macro_export]
macro_rules! numeric_enum {
    (#[repr($repr:ident)]
     $(#$attrs:tt)* $vis:vis enum $name:ident {
        $($(#$enum_attrs:tt)* $enum:ident = $constant:expr),* $(,)?
    } ) => {
        #[repr($repr)]
        $(#$attrs)*
        $vis enum $name {
            $($(#$enum_attrs)* $enum = $constant),*
        }

        impl ::core::convert::TryFrom<$repr> for $name {
            type Error = ();

            fn try_from(value: $repr) -> ::core::result::Result<Self, ()> {
                match value {
                    $($constant => Ok( $name :: $enum ),)*
                    _ => Err(())
                }
            }
        }

        impl ::core::convert::From<$name> for $repr {
            fn from(value: $name) -> $repr {
                match value {
                    $($name :: $enum => $constant,)*
                }
            }
        }
    }
}