[][src]Macro genco_macros::quote_fn

quote_fn!() { /* proc-macro */ }

Convenience macro for constructing a FormatInto implementation in-place.

Constructing FormatInto implementation instead of short lived token streams can be more beneficial for memory use and performance.

Comparison

In the below example, f1 and f2 are equivalent. In here quote_fn! simply makes it easier to build.

use genco::prelude::*;
use genco::tokens::from_fn;

let f1 = from_fn(move |t| {
    quote_in!{ *t =>
        println!("Hello World");
    }
});

let f2 = quote_fn!{
    println!("Hello World");
};

let tokens: rust::Tokens = quote!{
    #f1
    #f2
};

assert_eq!{
    vec![
        "println!(\"Hello World\");",
        "println!(\"Hello World\");",
    ],
    tokens.to_file_vec()?,
};

Examples which borrow

use genco::prelude::*;

fn greeting(name: &str) -> impl FormatInto<Rust> + '_ {
    quote_fn! {
        println!(#_(Hello #name))
    }
}

fn advanced_greeting<'a>(first: &'a str, last: &'a str) -> impl FormatInto<Rust> + 'a {
    quote_fn! {
        println!(#_(Hello #first #last))
    }
}

let tokens = quote! {
    #(greeting("Mio"));
    #(advanced_greeting("Jane", "Doe"));
};

assert_eq!{
    vec![
        "println!(\"Hello Mio\");",
        "println!(\"Hello Jane Doe\");",
    ],
    tokens.to_file_vec()?
};