6

I need to create a macro derive where the name is part of the function name. (This code does not work, it is only to show the problem)

fn impl_logic(ast: &syn::DeriveInput) -> TokenStream {
    let name:&syn::Ident = &ast.ident;

    let gen = quote! {
       pub fn #name_logic() -> Arc<Mutex<UiAplicacion>> {
           ...
       }
    };

    gen.into()
}

How can I do this?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253

1 Answers1

7

Based on quote's docs, you can construct a new identifier with syn::Ident:

let fname = format!("{}_logic", name);
let varname = syn::Ident::new(&fname, ident.span());

and then interpolate it:

let gen = quote! {
   pub fn #varname() -> Arc<Mutex<UiAplicacion>> {
       ...
   }
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253