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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
use proc_macro::{TokenStream, TokenTree};
use quote::quote;
use syn::{parse, Ident, ItemEnum};
/// Subtracts two numbers and calls a given callback macro with the result. Optionally, a
/// additional argument (delimeted) can be given which will also be passed to the
/// callback.
///
/// # Input
/// `$num_a - $num_b, $callback $(, $user_data)?`
///
/// # Examples
/// ```
/// # use std::any::TypeId;
/// use util_macros::sub;
///
/// macro_rules! sub_cb {
/// ($num: literal) => {
/// $num
/// };
/// }
///
/// type Foo = [u8; sub!(5 - 2, sub_cb)];
///
/// assert_eq!(TypeId::of::<Foo>(), TypeId::of::<[u8; 3]>());
/// ```
/// <br>
///
/// The callback macro can be called with extra arguments.
/// ```
/// # use std::any::TypeId;
/// use util_macros::sub;
///
/// macro_rules! sub_cb {
/// ($num: literal, $to_multiply: literal) => {
/// $num * $to_multiply
/// };
/// }
///
/// type Foo = [u8; sub!(5 - 2, sub_cb, (20))];
///
/// assert_eq!(TypeId::of::<Foo>(), TypeId::of::<[u8; 60]>());
/// ```
/// <br>
///
/// The callback is called with the identifier `overflow` if a overflow occurs.
/// ```
/// # use std::any::TypeId;
/// use util_macros::sub;
///
/// macro_rules! sub_cb {
/// ($num: literal) => {
/// $num
/// };
///
/// (overflow) => {
/// 128
/// };
/// }
///
/// type Foo = [u8; sub!(3 - 10, sub_cb)];
///
/// assert_eq!(TypeId::of::<Foo>(), TypeId::of::<[u8; 128]>());
/// ```
#[proc_macro]
pub fn sub(input: TokenStream) -> TokenStream
{
let mut input_tt_iter = input.into_iter();
let num_a = match input_tt_iter.next().unwrap() {
TokenTree::Literal(lit) => lit.to_string().parse::<u32>().unwrap(),
_ => {
panic!("Expected a number literal");
}
};
match input_tt_iter.next().unwrap() {
TokenTree::Punct(punct) if punct.as_char() == '-' => {}
_ => {
panic!("Expected a '-' token");
}
};
let num_b = match input_tt_iter.next().unwrap() {
TokenTree::Literal(lit) => lit.to_string().parse::<u32>().unwrap(),
_ => {
panic!("Expected a number literal");
}
};
match input_tt_iter.next().unwrap() {
TokenTree::Punct(punct) if punct.as_char() == ',' => {}
_ => {
panic!("Expected a ',' token");
}
};
let cb_ident = match input_tt_iter.next().unwrap() {
TokenTree::Ident(cb_ident) => {
proc_macro2::Ident::new(&cb_ident.to_string(), cb_ident.span().into())
}
_ => {
panic!("Expected a identifier");
}
};
let opt_user_data = input_tt_iter
.next()
.map(|comma_tt| {
match comma_tt {
TokenTree::Punct(punct) if punct.as_char() == ',' => {}
_ => {
panic!("Expected a ',' token");
}
};
let user_data_tt = input_tt_iter.next().unwrap();
let TokenTree::Group(group) = user_data_tt else {
panic!("User data must be a delimeted")
};
let inside: proc_macro2::TokenStream = group.stream().into();
quote! {, #inside }
})
.unwrap_or_default();
let Some(subtracted) = num_a.checked_sub(num_b) else {
return quote! {
#cb_ident!(overflow)
}
.into();
};
let subtracted_lit = proc_macro2::Literal::u32_unsuffixed(subtracted);
quote! {
#cb_ident!(#subtracted_lit #opt_user_data)
}
.into()
}
#[proc_macro_derive(FromRepr)]
pub fn from_repr(input: TokenStream) -> TokenStream
{
let enum_item = parse::<ItemEnum>(input).unwrap();
let repr_attr = enum_item
.attrs
.iter()
.find(|attr| {
attr.path()
.get_ident()
.is_some_and(|attr_ident| attr_ident == "repr")
})
.unwrap();
let repr = repr_attr.parse_args::<Ident>().unwrap();
let repr_str = repr.to_string();
if !((repr_str.starts_with('u') || repr_str.starts_with('i'))
&& repr_str
.chars()
.skip(1)
.all(|character| character.is_ascii_digit()))
{
panic!("Invalid repr. Must be u* or i* where * is a number");
}
let variants = enum_item.variants.iter().map(|variant| {
let Some((_, discriminant)) = &variant.discriminant else {
panic!("All variants must have discriminants");
};
(variant.ident.clone(), discriminant.clone())
});
let match_arms = variants.map(|(variant, discriminant)| {
quote! {
#discriminant => Some(Self::#variant),
}
});
let enum_ident = enum_item.ident.clone();
quote! {
impl #enum_ident
{
pub fn from_repr(repr: #repr) -> Option<Self>
{
match repr {
#(#match_arms)*
_ => None
}
}
}
}
.into()
}
|