Hello! I want to write a macro that acts on a function and produces a new function that performs the same operations as the original function twice.
I am trying to write an attribute macro
. As I thought it was better suited for the task.
#[double]
fn print_hello() {
println!("hello");
}
I want this to produce a new function print_hello_double
(for example)
fn print_hello_double() {
println!("hello");
println!("hello");
}
I am using syn
and quote
crates. I am able to get the parsed version of the original function's contents using the syn
crate. syn::Block.
But I am not able to understand how to use this to get the contents inside the new function produced (using quote! macro)
I tried to just include the parsed statements inside the quote!
macro
quote! {
// ....
#stmts // stmts is the syn::Block instance obtained
// ....
}
But I get the error saying that this doesn't implement the ToTokens
trait from quote
crate.
the trait bound
std::vec::Vec<syn::stmt::Stmt>: quote::to_tokens::ToTokens
is not satisfied
required by `quote::to_tokens::ToTokens::to_tokens