function-wrapper
Rust attribute macro library that makes it easy to wrap functions in code that runs before and / or after a function executes.
This function
#[wrap]
fn hello() -> bool
{
println!("Hello there!");
println!("This is some code.");
true
}
which is being wrapped by this attribute
use function_wrapper::WrappedFn;
extern crate proc_macro;
extern crate proc_macro2;
use syn::parse_macro_input;
use quote::quote;
#[proc_macro_attribute]
pub fn wrap(_: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream
{
let mut function = parse_macro_input!(item as WrappedFn);
function.set_pre_code(quote!{ println!("Hi at the start :)"); });
function.set_post_code(quote!{ println!("Hi at the end :)"); });
proc_macro2::TokenStream::from(function).into()
}
will turn into this after being compiled.
fn hello() -> bool
{
println!("Hi at the start :)");
let mut wrapper = ||
{
println!("Hello there!");
println!("This is some code.");
true
};
let result = wrapper();
println!("Hi at the end :)");
result
}