userspace/macros/enums/
typed.rs1#[macro_export]
2#[rustfmt::skip]
3macro_rules! enum_typed {
4 (
5 $enum_identifier:ident;
6 $variant:ty;
7 $label:expr;
8 $($module:tt)::*;
9 [
10 $(
11 [
12 $discriminant:expr;
13 $identifier:ident;
14 $type:ty;
15 |$argumento:ident : $tipo:ty|{$($lambda:tt)*};
16 $const_identifier:ident;
17 $acronym:expr;
18 $description:expr
19 ]
20 ),* $(,)?
21 ]
22 ) => {
23 pub type Franco = *const u8;
24 pub use $($module)::*::*;
25
26 pub mod constants {
28 $(
29 pub const $const_identifier: $variant = $discriminant;
30 )*
31 }
32
33 pub mod types {
34 pub use $($module)::*::*;
35 $( pub type $identifier = $type; )*
36 }
37
38 #[repr(C)]
39 #[derive(Copy, Clone, Eq, PartialEq)]
40 pub enum $enum_identifier {
41 $($identifier = $discriminant,)*
42 TODO,
43 }
44
45 impl $enum_identifier {
46 pub fn from(discriminant: $variant) -> Self {
47 match discriminant {
48 $($discriminant => Self::$identifier,)*
49 _ => Self::TODO,
50 }
51 }
52
53 pub fn to(&self) -> $variant {
54 match *self {
55 $(Self::$identifier => $discriminant,)*
56 _ => <$variant>::MAX
57 }
58 }
59
60 pub fn str(&self) -> &str {
61 match self {
62 $(Self::$identifier => $description,)*
63 _ => "TODO"
64 }
65 }
66
67 pub fn acronym(&self) -> &str {
68 match *self {
69 $(Self::$identifier => $acronym,)*
70 _ => "Unknown error",
71 }
72 }
73 }
74
75 impl core::ops::Add for $enum_identifier {
76 type Output = Self;
77
78 fn add(self, rhs: Self) -> Self::Output {
79 $enum_identifier::from(self.to() | rhs.to())
80 }
81 }
82
83 impl core::ops::Sub for $enum_identifier {
84 type Output = Self;
85
86 fn sub(self, rhs: Self) -> Self::Output {
87 $enum_identifier::from(self.to() & !rhs.to())
88 }
89 }
90
91 impl core::fmt::Display for $enum_identifier {
92 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
93 write!(f, "{}:{}",self.to(), self.str())
94 }
95 }
96
97 impl core::fmt::Debug for $enum_identifier {
98 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
99 write!(f, "{}:{}",self.to(), self.acronym())
100 }
101 }
102
103 #[derive(Debug, Clone, Copy)]
104 pub enum EnumTyped {
105 $($identifier($type),)*
106 TODO((usize,usize)),
107 }
108
109 impl EnumTyped {
110 pub fn from_kv(etype: *const $variant, p: *const u8) -> Self {
111 match $enum_identifier::from(unsafe { *etype }) {
112 $( $enum_identifier::$identifier => EnumTyped::$identifier( unsafe { (|$argumento:$tipo|{ $($lambda)* })(p)} ),)*
113 _ => EnumTyped::TODO( unsafe {(*etype, *(p as *const usize)) }),
114 }
115 }
116
117 pub fn to_kv(&self) -> ($variant, *const u8) {
118 match *self {
119 $(EnumTyped::$identifier(v) => (($enum_identifier::$identifier).to(), v as *const u8),)*
120 EnumTyped::TODO(id) => ($enum_identifier::TODO.to(), id.0 as *const u8),
121 }
122 }
123
124 pub fn to_k(&self) -> $enum_identifier {
125 match *self {
126 $(EnumTyped::$identifier(_) => $enum_identifier::$identifier,)*
127 EnumTyped::TODO(id) => $enum_identifier::TODO,
128 }
129 }
130
131 pub fn is_null(&self) -> bool {
132 match self {
133 EnumTyped::Null(0) => true,
134 _ => false,
135 }
136 }
137 }
138 };
139}
140pub use enum_typed;