Hi!
I'm trying to create a code generator using the quote
crate, but I'm having difficulty using the proc_macro2
crate.
Using quote!
in main.ts
seems to work fine:
// example usage: cargo build | rustc -o hello -
use quote::quote;
fn main() {
let tokens = quote! {
fn main() {
println!("Hello, world!");
}
};
println!("{}", tokens);
}
The trouble happens when I try to return the result of quote!
from a function. Based on the quote
docs, it seems like I should be converting it to a proc_macro2::TokenStream
like so:
use proc_macro2::TokenStream;
use quote::quote;
fn gen_hello() -> TokenStream {
let tokens = quote! {
fn main() {
println!("Hello, world!");
}
};
TokenStream::from(tokens)
}
fn main() {
println!("{}", gen_hello());
}
But, I get the following error when I run cargo run
:
thread 'main' panicked at 'procedural macro API is used outside of a procedural macro'
Does anyone have experience using quote!
in main.rs
? How do you get around this issue?
Thanks!
EDIT:
Seems like I can work around it by using quote::__private::TokenStream
, but that feels wrong. The quote
docs state:
proc_macro2 types may exist anywhere including tests and non-macro code like main.rs and build.rs
EDIT2:
Doh, looks like I don't need to call from
use proc_macro2::TokenStream;
use quote::quote;
fn gen_hello() -> TokenStream {
quote! {
fn main() {
println!("Hello, world!");
}
}
}