typed-quote
A fully typed quote!() alternative for both proc-macro and proc-macro2.
Quick start
Output TokenStream
typed_quote::quote!(...) returns a fully typed value that implements
[IntoTokens], [ToTokens], [WithSpan], [IntoTokenTree] or [ToTokenTree]
depending on the quoted content.
- Call ts.into_token_stream() to get [
proc_macro::TokenStream]. - Call ts.into_token_stream2() to get [
proc_macro2::TokenStream].
use *;
let crate_name = quote!;
let tokens = quote!;
let ts: TokenStream = tokens.into_token_stream2;
assert_eq!;
Set span
-
Call
ts.with_default_span(span)to specify a span only for un-spanned tokens ints.quote!(un-spanned).with_default_span(span)will specify span for all tokens.quote!(#quoted_var).with_default_span(span)will callquoted_var.with_default_span(span).proc_macro::TokenStream::from_str("...").unwrap().with_default_span(span)will not change span becauseproc_macro::TokenStreamalready has a span.
-
Call
ts.with_replaced_span(span)to set span for all tokens ints.- Note that, if the ident of [
proc_macro::Ident] and [proc_macro2::Ident] is exactly$crate, then its span will not be replaced.
- Note that, if the ident of [
Comparison with ::quote
typed_quote::typed_quote!() is an alias of typed_quote::quote!()
so you can disambiguate typed_quote!() from quote::quote!().
-
typed_quoteis new, not well tested, and will contain breaking changes in the future. You should use::quotefor production. -
::quote::quote!()returns aproc_macro2::TokenStream.typed_quote!()returns a fully typed struct that implementsIntoTokensand you can decide to outputproc_macro::TokenStreamorproc_macro2::TokenStream. -
::quotehasquote_spanned!(span => ...).But in
typed_quote, usetyped_quote!(...).with_default_span(span)instead. -
::quote::quote!(#var)only references&varbuttyped_quote!(#var)movesvar. Since&impl ToTokensimplements bothIntoTokenandCopy,let var = &var; typed_quote!(#var)will work ifvaris a value ofimpl ToTokens. -
::quote::ToTokensis dyn-
cargo features
-
alloc(default)This feature implements traits for
Box,Rc.This library is no_std and only
allocis enabled by default. -
proc-macroandproc-macro2Enable one or both of these features if necessary:
You don't need to enable them when you're writing a library that just inputs/outputs tokens. Users of your library can decide which feature to enable and call the corresponding
into_token_streamorinto_token_stream2.use *;