Expand description
§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 typed_quote::prelude::*;
let crate_name = quote!("typed-quote");
let tokens = quote!(hello #crate_name !);
let ts: proc_macro2::TokenStream = tokens.into_token_stream2();
assert_eq!(ts.to_string(), r#"hello "typed-quote" !"#);§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::Identandproc_macro2::Identis 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:
cargo add typed-quote --features proc-macro cargo add typed-quote --features proc-macro2 cargo add typed-quote --features proc-macro,proc-macro2You 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.cargo add typed-quoteuse typed_quote::prelude::*; fn braced(stream: impl IntoTokens) -> impl IntoTokenTree { quote!({ stream }) }
§Cheat sheet
| Type | derived Traits | value examples |
|---|---|---|
| ||
| ||
|
|
|
|
| ||
|
|
| |
| ||
|
grouped token stream:
|
| |
| ||
| ||
| ||
| ||
|
|
| |
|
punctuation |
| |
|
punctuations |
| |
|
|
| |
|
|
|
Re-exports§
pub use quote as typed_quote;
Modules§
Macros§
Enums§
Traits§
- Into
Token Tree - Into a token tree.
- Into
Tokens - Into token stream.
- RefWith
Span - To tokens with new span.
- Span
- A
proc_macro::Spanorproc_macro2::Span. - ToToken
Tree - To a token tree.
- ToTokens
- To token stream.
- With
Span - Into tokens with new span.