I'm trying to create a macro that generates two functions from a function signature. As the signature of the functions is dependent from what is passed to the macro, the number of function arguments is varying.
The functions I want to create are (i) an internal function, that actually does something (which I was able to generate as I wanted) and (ii) a public function that should wrap around the internal function. The function signatures are the same regarding the parameters. Therefore, it should be easy to pass all the arguments that were passed to the public function down into the internal function.
The following snippet shows the simplified macro generation for the public function. How can I achieve that the arguments are passed down correctly? (#params_from_public_function
)
let public_function_ident = ...;
let internal_function_ident = ...;
// ast_function_stub-Type: ForeignItemFn
let params_from_public_function: Punctuated<FnArg, Comma> = ast_function_stub.sig.inputs.clone();
quote! {
pub fn #public_name(#params_from_public_function) {
#internal_function_ident(#params_from_public_function);
}
}
Here are two examples, how the resulting macro should work:
generate_both_functions!(fn foo(someParam: &str) -> u8;);
// What the macro should generate:
fn _foo(someParam: &str) -> u8 { // Internal function
// Some internal stuff happens here
}
pub fn foo(someParam: &str) { // Public function
_foo(someParam);
}
// Second example (having two instead of one argument and different types)
generate_both_functions!(fn bar(param1: i32, param2: String) -> String;);
// What the macro should generate
fn _bar(param1: i32, param2: String) -> String {
// Internal stuff again
}
fn bar(param1: i32, param2: String) {
_bar(param1, param2);
}
Just to clarify: I was able to correctly parse the macro's input (generate_both_functions!
) and extract the function argument metadata from the AST. However, I am not able to pass the actual inputs dynamically to the internal function.