Skip to content

Commit

Permalink
rust: allow any constexpr to be used as param default
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Guo <[email protected]>
  • Loading branch information
nbdd0121 committed May 26, 2021
1 parent 38e0f28 commit 6f7f4b3
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions rust/macros/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,34 @@ fn param_ops_path(param_type: &str) -> &'static str {
}
}

fn parse_expr(it: &mut Cursor<'_>) -> TokenStream {
let mut tt = Vec::new();
while !it.eof() {
if let Some((punct, _)) = it.punct() {
if let ',' | ';' = punct.as_char() {
break;
}
}
let (token, next) = it.token_tree().unwrap();
tt.push(token);
*it = next;
}
if tt.is_empty() {
panic!("Expected expression");
}
tt.into_iter().collect()
}

fn expect_simple_param_val(param_type: &str) -> Box<dyn Fn(&mut Cursor<'_>) -> String> {
match param_type {
"bool" => Box::new(|param_it| {
let (ident, next) = param_it.ident().expect("Expected ident");
*param_it = next;
ident.to_string()
}),
"str" => Box::new(|param_it| {
let s = expect_string(param_it);
format!(
"kernel::module_param::StringParam::Ref({})",
Literal::byte_string(s.as_bytes())
)
}),
_ => Box::new(|param_it| {
let (lit, next) = param_it.literal().expect("Expected literal");
*param_it = next;
lit.to_string()
}),
_ => Box::new(|param_it| parse_expr(param_it).to_string()),
}
}

Expand Down

0 comments on commit 6f7f4b3

Please sign in to comment.