[−][src]Macro genco_macros::quote_in
quote_in!() { /* proc-macro */ }
Same as quote!, except that it allows for quoting directly to a token stream.
You specify the destination stream as the first argument, followed by a =>
and then the code to generate. To avoid taking ownership of the parameter
argument you can use the syntax &mut *<ident>
. This can prevent borrowing
issues you encounter (see the Borrowing
section below).
For example: quote_in! { &mut *tokens => fn foo() { } }
.
Note that there is a potential issue with reborrowing
Reborrowing
In case you get a borrow issue like the following:
9 | let tokens = &mut tokens;
| ------ help: consider changing this to be mutable: `mut tokens`
...
12 | / quote_in! { tokens =>
13 | | fn #name() -> u32 {
14 | | #{tokens => {
15 | | tokens.append("42");
16 | | }}
17 | | }
18 | | }
| |_____^ cannot borrow as mutable
This is because inner scoped like #{tokens => { <block> }}
take ownership
of their variable by default. To have it perform a proper reborrow, you can
do the following instead:
#![feature(proc_macro_hygiene)] use genco::prelude::*; let mut tokens = Tokens::<Rust>::new(); let tokens = &mut tokens; for name in &["foo", "bar", "baz"] { quote_in! { &mut *tokens => fn #(*name)() -> u32 { #{tokens => { tokens.append("42"); }} } } }
Examples
#![feature(proc_macro_hygiene)] use genco::prelude::*; let mut tokens = Tokens::<Rust>::new(); quote_in! { tokens => fn foo() -> u32 { 42 } }
Examples
#![feature(proc_macro_hygiene)] use genco::{quote_in, Rust, Tokens}; let mut tokens = Tokens::<Rust>::new(); quote_in!(tokens => fn hello() -> u32 { 42 }); assert_eq!(vec!["fn hello() -> u32 { 42 }", ""], tokens.to_file_vec().unwrap());