Skip to content

Commit 6f7f4b3

Browse files
committed
rust: allow any constexpr to be used as param default
Signed-off-by: Gary Guo <gary@garyguo.net>
1 parent 38e0f28 commit 6f7f4b3

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

rust/macros/module.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,34 @@ fn param_ops_path(param_type: &str) -> &'static str {
226226
}
227227
}
228228

229+
fn parse_expr(it: &mut Cursor<'_>) -> TokenStream {
230+
let mut tt = Vec::new();
231+
while !it.eof() {
232+
if let Some((punct, _)) = it.punct() {
233+
if let ',' | ';' = punct.as_char() {
234+
break;
235+
}
236+
}
237+
let (token, next) = it.token_tree().unwrap();
238+
tt.push(token);
239+
*it = next;
240+
}
241+
if tt.is_empty() {
242+
panic!("Expected expression");
243+
}
244+
tt.into_iter().collect()
245+
}
246+
229247
fn expect_simple_param_val(param_type: &str) -> Box<dyn Fn(&mut Cursor<'_>) -> String> {
230248
match param_type {
231-
"bool" => Box::new(|param_it| {
232-
let (ident, next) = param_it.ident().expect("Expected ident");
233-
*param_it = next;
234-
ident.to_string()
235-
}),
236249
"str" => Box::new(|param_it| {
237250
let s = expect_string(param_it);
238251
format!(
239252
"kernel::module_param::StringParam::Ref({})",
240253
Literal::byte_string(s.as_bytes())
241254
)
242255
}),
243-
_ => Box::new(|param_it| {
244-
let (lit, next) = param_it.literal().expect("Expected literal");
245-
*param_it = next;
246-
lit.to_string()
247-
}),
256+
_ => Box::new(|param_it| parse_expr(param_it).to_string()),
248257
}
249258
}
250259

0 commit comments

Comments
 (0)