Is there a way to adjust the macro below, in order to detect when the last argument of the macro call is a closure? The code below works, but it’s using the trick of using a semicolon, and that doesn’t look right.
Could the closure argument be identified specifically at invocation, e.g. closure: || { ... }
?
macro_rules! invoke_me {
($func_name:ident, $($arg:expr),*; || $closure:expr ) => {{
println!("{}({})", stringify!($func_name), stringify!($($arg),*));
println!("With closure");
let result: Result<(),String> = $closure;
result
}};
($func_name:ident, $($arg:expr),*) => {{
println!("{}({})", stringify!($func_name), stringify!($($arg),*));
println!("No closure");
Ok::<(),String>(())
}};
}
fn main() {
invoke_me!(foo, 1, 2, 3).unwrap();
invoke_me!(foo, 1, 2, 3; || {
println!("Ok from closure");
Ok(())
}).unwrap();
let _ = invoke_me!(foo, 1, 2, 3; || {
println!("Error from closure");
Err("Error".to_string())
});
}