1pub trait OptionalFrom<T: Sized>
4where
5 Self: Sized,
6{
7 fn optional_from(value: T) -> Option<Self>;
8}
9
10#[macro_export]
13macro_rules! extended_enum {
14 ($(#[$outer:meta])* $name:ident, $ty:ty, $($(#[$inner:meta])* $var:ident => $val:expr),+ $(,)*) => (
15
16 $(#[$outer])*
17 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
18 #[cfg_attr(feature = "defmt", derive(defmt::Format))]
19 pub enum $name {
20 $(
21 $(#[$inner])*
22 $var,
23 )*
24 }
25
26 impl From<$ty> for $name {
27 fn from(v: $ty) -> Self {
28 match v {
29 $( $val => $name::$var,)*
30 _ => panic!("Invalid value"),
31 }
32 }
33 }
34
35 impl From<$name> for $ty {
36 fn from(v: $name) -> Self {
37 match v {
38 $( $name::$var => $val, )*
39 }
40 }
41 }
42
43 impl OptionalFrom<$ty> for $name {
44 fn optional_from(v: $ty) -> Option<Self> {
45 match v {
46 $( $val => Some($name::$var),)*
47 _ => None,
48 }
49 }
50 }
51
52 impl PartialEq<$name> for $ty {
53 fn eq(&self, other: &$name) -> bool {
54 match *other {
55 $( $name::$var => *self == $val, )*
56 }
57 }
58
59 fn ne(&self, other: &$name) -> bool {
60 match *other {
61 $( $name::$var => *self != $val, )*
62 }
63 }
64 }
65 );
66}