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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pub trait OptionalFrom<T: Sized>
where
Self: Sized,
{
fn optional_from(value: T) -> Option<Self>;
}
#[macro_export]
macro_rules! extended_enum {
($(#[$outer:meta])* $name:ident, $ty:ty, $($(#[$inner:meta])* $var:ident => $val:expr),+ $(,)*) => (
$(#[$outer])*
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum $name {
$(
$(#[$inner])*
$var,
)*
}
impl From<$ty> for $name {
fn from(v: $ty) -> Self {
match v {
$( $val => $name::$var,)*
_ => panic!("Invalid value"),
}
}
}
impl From<$name> for $ty {
fn from(v: $name) -> Self {
match v {
$( $name::$var => $val, )*
}
}
}
impl OptionalFrom<$ty> for $name {
fn optional_from(v: $ty) -> Option<Self> {
match v {
$( $val => Some($name::$var),)*
_ => None,
}
}
}
impl PartialEq<$name> for $ty {
fn eq(&self, other: &$name) -> bool {
match *other {
$( $name::$var => *self == $val, )*
}
}
fn ne(&self, other: &$name) -> bool {
match *other {
$( $name::$var => *self != $val, )*
}
}
}
);
}