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
macro_rules! cenum {
($name:ident: $ty:ty { $( $(#[$cond:meta])* const $symbol:ident = $value:expr,)* }) => (
#[repr(C)]
#[derive(Default, Copy, Clone, PartialEq, Eq, Hash)]
pub struct $name($ty);
impl $name {
pub fn from_raw(v: $ty) -> Self {
$name(v)
}
pub fn as_raw(&self) -> $ty {
self.0
}
}
$(
$(#[$cond])*
pub const $symbol: $name = $name($value);
)*
#[allow(unused_mut)]
impl ::std::fmt::Debug for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, concat!(stringify!($name), "({} = "), self.0)?;
let mut first = true;
$(
$(#[$cond])*
{
if self.0 == $value {
if !first {
write!(f, ", ")?;
}
write!(f, stringify!($symbol))?;
first = false;
}
}
)*
if first {
write!(f, "<unknown>")?;
}
write!(f, ")")
}
}
)
}