Skip to content

Commit acceed7

Browse files
committed
rust: allow any constexpr to be used as param default
Signed-off-by: Gary Guo <[email protected]>
1 parent 8f9f074 commit acceed7

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

rust/macros/module.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,23 @@ fn param_ops_path(param_type: &str) -> &'static str {
238238
}
239239
}
240240

241+
fn parse_expr(it: &mut Cursor<'_>) -> TokenStream {
242+
let mut tt = Vec::new();
243+
while !it.eof() {
244+
if let Some((punct, _)) = it.punct() {
245+
if let ',' | ';' = punct.as_char() {
246+
break;
247+
}
248+
}
249+
let (token, next) = it.token_tree().unwrap();
250+
tt.push(token);
251+
*it = next;
252+
}
253+
tt.into_iter().collect()
254+
}
255+
241256
fn expect_simple_param_val(param_type: &str) -> Box<dyn Fn(&mut Cursor<'_>) -> String> {
242257
match param_type {
243-
"bool" => Box::new(|param_it| {
244-
let (ident, next) = param_it.ident().expect("Expected ident");
245-
*param_it = next;
246-
ident.to_string()
247-
}),
248258
"str" => Box::new(|param_it| {
249259
let s = expect_string(param_it);
250260
format!(
@@ -253,9 +263,11 @@ fn expect_simple_param_val(param_type: &str) -> Box<dyn Fn(&mut Cursor<'_>) -> S
253263
)
254264
}),
255265
_ => Box::new(|param_it| {
256-
let (lit, next) = param_it.literal().expect("Expected literal");
257-
*param_it = next;
258-
lit.to_string()
266+
let expr = parse_expr(param_it);
267+
if expr.is_empty() {
268+
panic!("Expected expression");
269+
}
270+
expr.to_string()
259271
}),
260272
}
261273
}

0 commit comments

Comments
 (0)