My goal is to write a simple attribute proc macro, that can be used to annotate a function and print its arguments.
example:
#[print_arguments]
fn add(a:u64, b:u64) -> u64 {
a + b
}
Using quote i can interpolate tokens in scope and with syn parse the rust code fragement. So I tried the following:
#[proc_macro_attribute]
pub fn print_arguments(_: TokenStream, item: TokenStream) -> TokenStream {
let fn_type = parse_macro_input!(item as ItemFn);
let signature = fn_type.sig;
let ident = signature.ident;
let arguments = signature.inputs;
let return_type = signature.output;
let mut args = arguments.iter();
let fn_arg = args.next().expect("msg");
let block = fn_type.block;
TokenStream::from(quote! {
fn #ident ( #arguments ) #return_type {
println!("enhanced function {:?}", #(#args)*); // this one seems wrong, as it would iterate over FnArg, which is an enum.
#block
}
})
}
Is there any possibility to access the FnArg ident(ifiers)?