1#[macro_export]
20macro_rules! nutype_enum {
21 (
22 $(#[$attr:meta])*
23 $vis:vis enum $name:ident($type:ty) {
24 $(
25 $(#[$variant_attr:meta])*
26 $variant:ident = $value:expr
27 ),*$(,)?
28 }
29 ) => {
30 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
31 $(#[$attr])*
32 #[repr(transparent)]
33 $vis struct $name(pub $type);
34
35 impl ::std::fmt::Debug for $name {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 match self {
38 $(
39 &$name::$variant => write!(f, "{}::{}", stringify!($name), stringify!($variant)),
40 )*
41 _ => write!(f, "{}({:?})", stringify!($name), self.0),
42 }
43 }
44 }
45
46 impl $name {
47 $(
48 $(#[$variant_attr])*
49 #[allow(non_upper_case_globals)]
50 pub const $variant: Self = Self($value);
51 )*
52 }
53
54 impl From<$type> for $name {
55 fn from(value: $type) -> Self {
56 Self(value)
57 }
58 }
59
60 impl From<$name> for $type {
61 fn from(value: $name) -> Self {
62 value.0
63 }
64 }
65 };
66}
67
68#[macro_export]
88macro_rules! bitwise_enum {
89 ($name:ident) => {
90 impl ::std::ops::BitAnd for $name {
91 type Output = Self;
92
93 fn bitand(self, rhs: Self) -> Self::Output {
94 Self(self.0 & rhs.0)
95 }
96 }
97
98 impl ::std::ops::BitOr for $name {
99 type Output = Self;
100
101 fn bitor(self, rhs: Self) -> Self::Output {
102 Self(self.0 | rhs.0)
103 }
104 }
105
106 impl ::std::ops::BitXor for $name {
107 type Output = Self;
108
109 fn bitxor(self, rhs: Self) -> Self::Output {
110 Self(self.0 ^ rhs.0)
111 }
112 }
113
114 impl ::std::ops::Not for $name {
115 type Output = Self;
116
117 fn not(self) -> Self::Output {
118 Self(!self.0)
119 }
120 }
121
122 impl ::std::ops::BitAndAssign for $name {
123 fn bitand_assign(&mut self, rhs: Self) {
124 self.0 &= rhs.0;
125 }
126 }
127
128 impl ::std::ops::BitOrAssign for $name {
129 fn bitor_assign(&mut self, rhs: Self) {
130 self.0 |= rhs.0;
131 }
132 }
133
134 impl ::std::ops::BitXorAssign for $name {
135 fn bitxor_assign(&mut self, rhs: Self) {
136 self.0 ^= rhs.0;
137 }
138 }
139 };
140}