Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: quote: import crate

This is a subset of the Rust `quote` crate, version 1.0.40 (released
2025-03-12), licensed under "Apache-2.0 OR MIT", from:

https://github.com/dtolnay/quote/raw/1.0.40/src

The files are copied as-is, with no modifications whatsoever (not even
adding the SPDX identifiers).

For copyright details, please see:

https://github.com/dtolnay/quote/blob/1.0.40/README.md#license
https://github.com/dtolnay/quote/blob/1.0.40/LICENSE-APACHE
https://github.com/dtolnay/quote/blob/1.0.40/LICENSE-MIT

The next patch modifies these files as needed for use within the
kernel. This patch split allows reviewers to double-check the import
and to clearly see the differences introduced.

The following script may be used to verify the contents:

for path in $(cd rust/quote/ && find . -type f -name '*.rs'); do
curl --silent --show-error --location \
https://github.com/dtolnay/quote/raw/1.0.40/src/$path \
| diff --unified rust/quote/$path - && echo $path: OK
done

Reviewed-by: Gary Guo <gary@garyguo.net>
Tested-by: Gary Guo <gary@garyguo.net>
Tested-by: Jesung Yang <y.j3ms.n@gmail.com>
Link: https://patch.msgid.link/20251124151837.2184382-12-ojeda@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+2633
+110
rust/quote/ext.rs
··· 1 + use super::ToTokens; 2 + use core::iter; 3 + use proc_macro2::{TokenStream, TokenTree}; 4 + 5 + /// TokenStream extension trait with methods for appending tokens. 6 + /// 7 + /// This trait is sealed and cannot be implemented outside of the `quote` crate. 8 + pub trait TokenStreamExt: private::Sealed { 9 + /// For use by `ToTokens` implementations. 10 + /// 11 + /// Appends the token specified to this list of tokens. 12 + fn append<U>(&mut self, token: U) 13 + where 14 + U: Into<TokenTree>; 15 + 16 + /// For use by `ToTokens` implementations. 17 + /// 18 + /// ``` 19 + /// # use quote::{quote, TokenStreamExt, ToTokens}; 20 + /// # use proc_macro2::TokenStream; 21 + /// # 22 + /// struct X; 23 + /// 24 + /// impl ToTokens for X { 25 + /// fn to_tokens(&self, tokens: &mut TokenStream) { 26 + /// tokens.append_all(&[true, false]); 27 + /// } 28 + /// } 29 + /// 30 + /// let tokens = quote!(#X); 31 + /// assert_eq!(tokens.to_string(), "true false"); 32 + /// ``` 33 + fn append_all<I>(&mut self, iter: I) 34 + where 35 + I: IntoIterator, 36 + I::Item: ToTokens; 37 + 38 + /// For use by `ToTokens` implementations. 39 + /// 40 + /// Appends all of the items in the iterator `I`, separated by the tokens 41 + /// `U`. 42 + fn append_separated<I, U>(&mut self, iter: I, op: U) 43 + where 44 + I: IntoIterator, 45 + I::Item: ToTokens, 46 + U: ToTokens; 47 + 48 + /// For use by `ToTokens` implementations. 49 + /// 50 + /// Appends all tokens in the iterator `I`, appending `U` after each 51 + /// element, including after the last element of the iterator. 52 + fn append_terminated<I, U>(&mut self, iter: I, term: U) 53 + where 54 + I: IntoIterator, 55 + I::Item: ToTokens, 56 + U: ToTokens; 57 + } 58 + 59 + impl TokenStreamExt for TokenStream { 60 + fn append<U>(&mut self, token: U) 61 + where 62 + U: Into<TokenTree>, 63 + { 64 + self.extend(iter::once(token.into())); 65 + } 66 + 67 + fn append_all<I>(&mut self, iter: I) 68 + where 69 + I: IntoIterator, 70 + I::Item: ToTokens, 71 + { 72 + for token in iter { 73 + token.to_tokens(self); 74 + } 75 + } 76 + 77 + fn append_separated<I, U>(&mut self, iter: I, op: U) 78 + where 79 + I: IntoIterator, 80 + I::Item: ToTokens, 81 + U: ToTokens, 82 + { 83 + for (i, token) in iter.into_iter().enumerate() { 84 + if i > 0 { 85 + op.to_tokens(self); 86 + } 87 + token.to_tokens(self); 88 + } 89 + } 90 + 91 + fn append_terminated<I, U>(&mut self, iter: I, term: U) 92 + where 93 + I: IntoIterator, 94 + I::Item: ToTokens, 95 + U: ToTokens, 96 + { 97 + for token in iter { 98 + token.to_tokens(self); 99 + term.to_tokens(self); 100 + } 101 + } 102 + } 103 + 104 + mod private { 105 + use proc_macro2::TokenStream; 106 + 107 + pub trait Sealed {} 108 + 109 + impl Sealed for TokenStream {} 110 + }
+168
rust/quote/format.rs
··· 1 + /// Formatting macro for constructing `Ident`s. 2 + /// 3 + /// <br> 4 + /// 5 + /// # Syntax 6 + /// 7 + /// Syntax is copied from the [`format!`] macro, supporting both positional and 8 + /// named arguments. 9 + /// 10 + /// Only a limited set of formatting traits are supported. The current mapping 11 + /// of format types to traits is: 12 + /// 13 + /// * `{}` ⇒ [`IdentFragment`] 14 + /// * `{:o}` ⇒ [`Octal`](std::fmt::Octal) 15 + /// * `{:x}` ⇒ [`LowerHex`](std::fmt::LowerHex) 16 + /// * `{:X}` ⇒ [`UpperHex`](std::fmt::UpperHex) 17 + /// * `{:b}` ⇒ [`Binary`](std::fmt::Binary) 18 + /// 19 + /// See [`std::fmt`] for more information. 20 + /// 21 + /// <br> 22 + /// 23 + /// # IdentFragment 24 + /// 25 + /// Unlike `format!`, this macro uses the [`IdentFragment`] formatting trait by 26 + /// default. This trait is like `Display`, with a few differences: 27 + /// 28 + /// * `IdentFragment` is only implemented for a limited set of types, such as 29 + /// unsigned integers and strings. 30 + /// * [`Ident`] arguments will have their `r#` prefixes stripped, if present. 31 + /// 32 + /// [`IdentFragment`]: crate::IdentFragment 33 + /// [`Ident`]: proc_macro2::Ident 34 + /// 35 + /// <br> 36 + /// 37 + /// # Hygiene 38 + /// 39 + /// The [`Span`] of the first `Ident` argument is used as the span of the final 40 + /// identifier, falling back to [`Span::call_site`] when no identifiers are 41 + /// provided. 42 + /// 43 + /// ``` 44 + /// # use quote::format_ident; 45 + /// # let ident = format_ident!("Ident"); 46 + /// // If `ident` is an Ident, the span of `my_ident` will be inherited from it. 47 + /// let my_ident = format_ident!("My{}{}", ident, "IsCool"); 48 + /// assert_eq!(my_ident, "MyIdentIsCool"); 49 + /// ``` 50 + /// 51 + /// Alternatively, the span can be overridden by passing the `span` named 52 + /// argument. 53 + /// 54 + /// ``` 55 + /// # use quote::format_ident; 56 + /// # const IGNORE_TOKENS: &'static str = stringify! { 57 + /// let my_span = /* ... */; 58 + /// # }; 59 + /// # let my_span = proc_macro2::Span::call_site(); 60 + /// format_ident!("MyIdent", span = my_span); 61 + /// ``` 62 + /// 63 + /// [`Span`]: proc_macro2::Span 64 + /// [`Span::call_site`]: proc_macro2::Span::call_site 65 + /// 66 + /// <p><br></p> 67 + /// 68 + /// # Panics 69 + /// 70 + /// This method will panic if the resulting formatted string is not a valid 71 + /// identifier. 72 + /// 73 + /// <br> 74 + /// 75 + /// # Examples 76 + /// 77 + /// Composing raw and non-raw identifiers: 78 + /// ``` 79 + /// # use quote::format_ident; 80 + /// let my_ident = format_ident!("My{}", "Ident"); 81 + /// assert_eq!(my_ident, "MyIdent"); 82 + /// 83 + /// let raw = format_ident!("r#Raw"); 84 + /// assert_eq!(raw, "r#Raw"); 85 + /// 86 + /// let my_ident_raw = format_ident!("{}Is{}", my_ident, raw); 87 + /// assert_eq!(my_ident_raw, "MyIdentIsRaw"); 88 + /// ``` 89 + /// 90 + /// Integer formatting options: 91 + /// ``` 92 + /// # use quote::format_ident; 93 + /// let num: u32 = 10; 94 + /// 95 + /// let decimal = format_ident!("Id_{}", num); 96 + /// assert_eq!(decimal, "Id_10"); 97 + /// 98 + /// let octal = format_ident!("Id_{:o}", num); 99 + /// assert_eq!(octal, "Id_12"); 100 + /// 101 + /// let binary = format_ident!("Id_{:b}", num); 102 + /// assert_eq!(binary, "Id_1010"); 103 + /// 104 + /// let lower_hex = format_ident!("Id_{:x}", num); 105 + /// assert_eq!(lower_hex, "Id_a"); 106 + /// 107 + /// let upper_hex = format_ident!("Id_{:X}", num); 108 + /// assert_eq!(upper_hex, "Id_A"); 109 + /// ``` 110 + #[macro_export] 111 + macro_rules! format_ident { 112 + ($fmt:expr) => { 113 + $crate::format_ident_impl!([ 114 + $crate::__private::Option::None, 115 + $fmt 116 + ]) 117 + }; 118 + 119 + ($fmt:expr, $($rest:tt)*) => { 120 + $crate::format_ident_impl!([ 121 + $crate::__private::Option::None, 122 + $fmt 123 + ] $($rest)*) 124 + }; 125 + } 126 + 127 + #[macro_export] 128 + #[doc(hidden)] 129 + macro_rules! format_ident_impl { 130 + // Final state 131 + ([$span:expr, $($fmt:tt)*]) => { 132 + $crate::__private::mk_ident( 133 + &$crate::__private::format!($($fmt)*), 134 + $span, 135 + ) 136 + }; 137 + 138 + // Span argument 139 + ([$old:expr, $($fmt:tt)*] span = $span:expr) => { 140 + $crate::format_ident_impl!([$old, $($fmt)*] span = $span,) 141 + }; 142 + ([$old:expr, $($fmt:tt)*] span = $span:expr, $($rest:tt)*) => { 143 + $crate::format_ident_impl!([ 144 + $crate::__private::Option::Some::<$crate::__private::Span>($span), 145 + $($fmt)* 146 + ] $($rest)*) 147 + }; 148 + 149 + // Named argument 150 + ([$span:expr, $($fmt:tt)*] $name:ident = $arg:expr) => { 151 + $crate::format_ident_impl!([$span, $($fmt)*] $name = $arg,) 152 + }; 153 + ([$span:expr, $($fmt:tt)*] $name:ident = $arg:expr, $($rest:tt)*) => { 154 + match $crate::__private::IdentFragmentAdapter(&$arg) { 155 + arg => $crate::format_ident_impl!([$span.or(arg.span()), $($fmt)*, $name = arg] $($rest)*), 156 + } 157 + }; 158 + 159 + // Positional argument 160 + ([$span:expr, $($fmt:tt)*] $arg:expr) => { 161 + $crate::format_ident_impl!([$span, $($fmt)*] $arg,) 162 + }; 163 + ([$span:expr, $($fmt:tt)*] $arg:expr, $($rest:tt)*) => { 164 + match $crate::__private::IdentFragmentAdapter(&$arg) { 165 + arg => $crate::format_ident_impl!([$span.or(arg.span()), $($fmt)*, arg] $($rest)*), 166 + } 167 + }; 168 + }
+88
rust/quote/ident_fragment.rs
··· 1 + use alloc::borrow::Cow; 2 + use core::fmt; 3 + use proc_macro2::{Ident, Span}; 4 + 5 + /// Specialized formatting trait used by `format_ident!`. 6 + /// 7 + /// [`Ident`] arguments formatted using this trait will have their `r#` prefix 8 + /// stripped, if present. 9 + /// 10 + /// See [`format_ident!`] for more information. 11 + /// 12 + /// [`format_ident!`]: crate::format_ident 13 + pub trait IdentFragment { 14 + /// Format this value as an identifier fragment. 15 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result; 16 + 17 + /// Span associated with this `IdentFragment`. 18 + /// 19 + /// If non-`None`, may be inherited by formatted identifiers. 20 + fn span(&self) -> Option<Span> { 21 + None 22 + } 23 + } 24 + 25 + impl<T: IdentFragment + ?Sized> IdentFragment for &T { 26 + fn span(&self) -> Option<Span> { 27 + <T as IdentFragment>::span(*self) 28 + } 29 + 30 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 31 + IdentFragment::fmt(*self, f) 32 + } 33 + } 34 + 35 + impl<T: IdentFragment + ?Sized> IdentFragment for &mut T { 36 + fn span(&self) -> Option<Span> { 37 + <T as IdentFragment>::span(*self) 38 + } 39 + 40 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 41 + IdentFragment::fmt(*self, f) 42 + } 43 + } 44 + 45 + impl IdentFragment for Ident { 46 + fn span(&self) -> Option<Span> { 47 + Some(self.span()) 48 + } 49 + 50 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 51 + let id = self.to_string(); 52 + if let Some(id) = id.strip_prefix("r#") { 53 + fmt::Display::fmt(id, f) 54 + } else { 55 + fmt::Display::fmt(&id[..], f) 56 + } 57 + } 58 + } 59 + 60 + impl<T> IdentFragment for Cow<'_, T> 61 + where 62 + T: IdentFragment + ToOwned + ?Sized, 63 + { 64 + fn span(&self) -> Option<Span> { 65 + T::span(self) 66 + } 67 + 68 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 69 + T::fmt(self, f) 70 + } 71 + } 72 + 73 + // Limited set of types which this is implemented for, as we want to avoid types 74 + // which will often include non-identifier characters in their `Display` impl. 75 + macro_rules! ident_fragment_display { 76 + ($($T:ty),*) => { 77 + $( 78 + impl IdentFragment for $T { 79 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 80 + fmt::Display::fmt(self, f) 81 + } 82 + } 83 + )* 84 + }; 85 + } 86 + 87 + ident_fragment_display!(bool, str, String, char); 88 + ident_fragment_display!(u8, u16, u32, u64, u128, usize);
+1454
rust/quote/lib.rs
··· 1 + //! [![github]](https://github.com/dtolnay/quote)&ensp;[![crates-io]](https://crates.io/crates/quote)&ensp;[![docs-rs]](https://docs.rs/quote) 2 + //! 3 + //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github 4 + //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust 5 + //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs 6 + //! 7 + //! <br> 8 + //! 9 + //! This crate provides the [`quote!`] macro for turning Rust syntax tree data 10 + //! structures into tokens of source code. 11 + //! 12 + //! Procedural macros in Rust receive a stream of tokens as input, execute 13 + //! arbitrary Rust code to determine how to manipulate those tokens, and produce 14 + //! a stream of tokens to hand back to the compiler to compile into the caller's 15 + //! crate. Quasi-quoting is a solution to one piece of that &mdash; producing 16 + //! tokens to return to the compiler. 17 + //! 18 + //! The idea of quasi-quoting is that we write *code* that we treat as *data*. 19 + //! Within the `quote!` macro, we can write what looks like code to our text 20 + //! editor or IDE. We get all the benefits of the editor's brace matching, 21 + //! syntax highlighting, indentation, and maybe autocompletion. But rather than 22 + //! compiling that as code into the current crate, we can treat it as data, pass 23 + //! it around, mutate it, and eventually hand it back to the compiler as tokens 24 + //! to compile into the macro caller's crate. 25 + //! 26 + //! This crate is motivated by the procedural macro use case, but is a 27 + //! general-purpose Rust quasi-quoting library and is not specific to procedural 28 + //! macros. 29 + //! 30 + //! ```toml 31 + //! [dependencies] 32 + //! quote = "1.0" 33 + //! ``` 34 + //! 35 + //! <br> 36 + //! 37 + //! # Example 38 + //! 39 + //! The following quasi-quoted block of code is something you might find in [a] 40 + //! procedural macro having to do with data structure serialization. The `#var` 41 + //! syntax performs interpolation of runtime variables into the quoted tokens. 42 + //! Check out the documentation of the [`quote!`] macro for more detail about 43 + //! the syntax. See also the [`quote_spanned!`] macro which is important for 44 + //! implementing hygienic procedural macros. 45 + //! 46 + //! [a]: https://serde.rs/ 47 + //! 48 + //! ``` 49 + //! # use quote::quote; 50 + //! # 51 + //! # let generics = ""; 52 + //! # let where_clause = ""; 53 + //! # let field_ty = ""; 54 + //! # let item_ty = ""; 55 + //! # let path = ""; 56 + //! # let value = ""; 57 + //! # 58 + //! let tokens = quote! { 59 + //! struct SerializeWith #generics #where_clause { 60 + //! value: &'a #field_ty, 61 + //! phantom: core::marker::PhantomData<#item_ty>, 62 + //! } 63 + //! 64 + //! impl #generics serde::Serialize for SerializeWith #generics #where_clause { 65 + //! fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> 66 + //! where 67 + //! S: serde::Serializer, 68 + //! { 69 + //! #path(self.value, serializer) 70 + //! } 71 + //! } 72 + //! 73 + //! SerializeWith { 74 + //! value: #value, 75 + //! phantom: core::marker::PhantomData::<#item_ty>, 76 + //! } 77 + //! }; 78 + //! ``` 79 + //! 80 + //! <br> 81 + //! 82 + //! # Non-macro code generators 83 + //! 84 + //! When using `quote` in a build.rs or main.rs and writing the output out to a 85 + //! file, consider having the code generator pass the tokens through 86 + //! [prettyplease] before writing. This way if an error occurs in the generated 87 + //! code it is convenient for a human to read and debug. 88 + //! 89 + //! [prettyplease]: https://github.com/dtolnay/prettyplease 90 + 91 + // Quote types in rustdoc of other crates get linked to here. 92 + #![doc(html_root_url = "https://docs.rs/quote/1.0.40")] 93 + #![allow( 94 + clippy::doc_markdown, 95 + clippy::elidable_lifetime_names, 96 + clippy::missing_errors_doc, 97 + clippy::missing_panics_doc, 98 + clippy::module_name_repetitions, 99 + clippy::needless_lifetimes, 100 + // false positive https://github.com/rust-lang/rust-clippy/issues/6983 101 + clippy::wrong_self_convention, 102 + )] 103 + 104 + extern crate alloc; 105 + 106 + #[cfg(feature = "proc-macro")] 107 + extern crate proc_macro; 108 + 109 + mod ext; 110 + mod format; 111 + mod ident_fragment; 112 + mod to_tokens; 113 + 114 + // Not public API. 115 + #[doc(hidden)] 116 + #[path = "runtime.rs"] 117 + pub mod __private; 118 + 119 + pub use crate::ext::TokenStreamExt; 120 + pub use crate::ident_fragment::IdentFragment; 121 + pub use crate::to_tokens::ToTokens; 122 + 123 + // Not public API. 124 + #[doc(hidden)] 125 + pub mod spanned; 126 + 127 + macro_rules! __quote { 128 + ($quote:item) => { 129 + /// The whole point. 130 + /// 131 + /// Performs variable interpolation against the input and produces it as 132 + /// [`proc_macro2::TokenStream`]. 133 + /// 134 + /// Note: for returning tokens to the compiler in a procedural macro, use 135 + /// `.into()` on the result to convert to [`proc_macro::TokenStream`]. 136 + /// 137 + /// <br> 138 + /// 139 + /// # Interpolation 140 + /// 141 + /// Variable interpolation is done with `#var` (similar to `$var` in 142 + /// `macro_rules!` macros). This grabs the `var` variable that is currently in 143 + /// scope and inserts it in that location in the output tokens. Any type 144 + /// implementing the [`ToTokens`] trait can be interpolated. This includes most 145 + /// Rust primitive types as well as most of the syntax tree types from the [Syn] 146 + /// crate. 147 + /// 148 + /// [Syn]: https://github.com/dtolnay/syn 149 + /// 150 + /// Repetition is done using `#(...)*` or `#(...),*` again similar to 151 + /// `macro_rules!`. This iterates through the elements of any variable 152 + /// interpolated within the repetition and inserts a copy of the repetition body 153 + /// for each one. The variables in an interpolation may be a `Vec`, slice, 154 + /// `BTreeSet`, or any `Iterator`. 155 + /// 156 + /// - `#(#var)*` — no separators 157 + /// - `#(#var),*` — the character before the asterisk is used as a separator 158 + /// - `#( struct #var; )*` — the repetition can contain other tokens 159 + /// - `#( #k => println!("{}", #v), )*` — even multiple interpolations 160 + /// 161 + /// <br> 162 + /// 163 + /// # Hygiene 164 + /// 165 + /// Any interpolated tokens preserve the `Span` information provided by their 166 + /// `ToTokens` implementation. Tokens that originate within the `quote!` 167 + /// invocation are spanned with [`Span::call_site()`]. 168 + /// 169 + /// [`Span::call_site()`]: proc_macro2::Span::call_site 170 + /// 171 + /// A different span can be provided through the [`quote_spanned!`] macro. 172 + /// 173 + /// <br> 174 + /// 175 + /// # Return type 176 + /// 177 + /// The macro evaluates to an expression of type `proc_macro2::TokenStream`. 178 + /// Meanwhile Rust procedural macros are expected to return the type 179 + /// `proc_macro::TokenStream`. 180 + /// 181 + /// The difference between the two types is that `proc_macro` types are entirely 182 + /// specific to procedural macros and cannot ever exist in code outside of a 183 + /// procedural macro, while `proc_macro2` types may exist anywhere including 184 + /// tests and non-macro code like main.rs and build.rs. This is why even the 185 + /// procedural macro ecosystem is largely built around `proc_macro2`, because 186 + /// that ensures the libraries are unit testable and accessible in non-macro 187 + /// contexts. 188 + /// 189 + /// There is a [`From`]-conversion in both directions so returning the output of 190 + /// `quote!` from a procedural macro usually looks like `tokens.into()` or 191 + /// `proc_macro::TokenStream::from(tokens)`. 192 + /// 193 + /// <br> 194 + /// 195 + /// # Examples 196 + /// 197 + /// ### Procedural macro 198 + /// 199 + /// The structure of a basic procedural macro is as follows. Refer to the [Syn] 200 + /// crate for further useful guidance on using `quote!` as part of a procedural 201 + /// macro. 202 + /// 203 + /// [Syn]: https://github.com/dtolnay/syn 204 + /// 205 + /// ``` 206 + /// # #[cfg(any())] 207 + /// extern crate proc_macro; 208 + /// # extern crate proc_macro2; 209 + /// 210 + /// # #[cfg(any())] 211 + /// use proc_macro::TokenStream; 212 + /// # use proc_macro2::TokenStream; 213 + /// use quote::quote; 214 + /// 215 + /// # const IGNORE_TOKENS: &'static str = stringify! { 216 + /// #[proc_macro_derive(HeapSize)] 217 + /// # }; 218 + /// pub fn derive_heap_size(input: TokenStream) -> TokenStream { 219 + /// // Parse the input and figure out what implementation to generate... 220 + /// # const IGNORE_TOKENS: &'static str = stringify! { 221 + /// let name = /* ... */; 222 + /// let expr = /* ... */; 223 + /// # }; 224 + /// # 225 + /// # let name = 0; 226 + /// # let expr = 0; 227 + /// 228 + /// let expanded = quote! { 229 + /// // The generated impl. 230 + /// impl heapsize::HeapSize for #name { 231 + /// fn heap_size_of_children(&self) -> usize { 232 + /// #expr 233 + /// } 234 + /// } 235 + /// }; 236 + /// 237 + /// // Hand the output tokens back to the compiler. 238 + /// TokenStream::from(expanded) 239 + /// } 240 + /// ``` 241 + /// 242 + /// <p><br></p> 243 + /// 244 + /// ### Combining quoted fragments 245 + /// 246 + /// Usually you don't end up constructing an entire final `TokenStream` in one 247 + /// piece. Different parts may come from different helper functions. The tokens 248 + /// produced by `quote!` themselves implement `ToTokens` and so can be 249 + /// interpolated into later `quote!` invocations to build up a final result. 250 + /// 251 + /// ``` 252 + /// # use quote::quote; 253 + /// # 254 + /// let type_definition = quote! {...}; 255 + /// let methods = quote! {...}; 256 + /// 257 + /// let tokens = quote! { 258 + /// #type_definition 259 + /// #methods 260 + /// }; 261 + /// ``` 262 + /// 263 + /// <p><br></p> 264 + /// 265 + /// ### Constructing identifiers 266 + /// 267 + /// Suppose we have an identifier `ident` which came from somewhere in a macro 268 + /// input and we need to modify it in some way for the macro output. Let's 269 + /// consider prepending the identifier with an underscore. 270 + /// 271 + /// Simply interpolating the identifier next to an underscore will not have the 272 + /// behavior of concatenating them. The underscore and the identifier will 273 + /// continue to be two separate tokens as if you had written `_ x`. 274 + /// 275 + /// ``` 276 + /// # use proc_macro2::{self as syn, Span}; 277 + /// # use quote::quote; 278 + /// # 279 + /// # let ident = syn::Ident::new("i", Span::call_site()); 280 + /// # 281 + /// // incorrect 282 + /// quote! { 283 + /// let mut _#ident = 0; 284 + /// } 285 + /// # ; 286 + /// ``` 287 + /// 288 + /// The solution is to build a new identifier token with the correct value. As 289 + /// this is such a common case, the [`format_ident!`] macro provides a 290 + /// convenient utility for doing so correctly. 291 + /// 292 + /// ``` 293 + /// # use proc_macro2::{Ident, Span}; 294 + /// # use quote::{format_ident, quote}; 295 + /// # 296 + /// # let ident = Ident::new("i", Span::call_site()); 297 + /// # 298 + /// let varname = format_ident!("_{}", ident); 299 + /// quote! { 300 + /// let mut #varname = 0; 301 + /// } 302 + /// # ; 303 + /// ``` 304 + /// 305 + /// Alternatively, the APIs provided by Syn and proc-macro2 can be used to 306 + /// directly build the identifier. This is roughly equivalent to the above, but 307 + /// will not handle `ident` being a raw identifier. 308 + /// 309 + /// ``` 310 + /// # use proc_macro2::{self as syn, Span}; 311 + /// # use quote::quote; 312 + /// # 313 + /// # let ident = syn::Ident::new("i", Span::call_site()); 314 + /// # 315 + /// let concatenated = format!("_{}", ident); 316 + /// let varname = syn::Ident::new(&concatenated, ident.span()); 317 + /// quote! { 318 + /// let mut #varname = 0; 319 + /// } 320 + /// # ; 321 + /// ``` 322 + /// 323 + /// <p><br></p> 324 + /// 325 + /// ### Making method calls 326 + /// 327 + /// Let's say our macro requires some type specified in the macro input to have 328 + /// a constructor called `new`. We have the type in a variable called 329 + /// `field_type` of type `syn::Type` and want to invoke the constructor. 330 + /// 331 + /// ``` 332 + /// # use quote::quote; 333 + /// # 334 + /// # let field_type = quote!(...); 335 + /// # 336 + /// // incorrect 337 + /// quote! { 338 + /// let value = #field_type::new(); 339 + /// } 340 + /// # ; 341 + /// ``` 342 + /// 343 + /// This works only sometimes. If `field_type` is `String`, the expanded code 344 + /// contains `String::new()` which is fine. But if `field_type` is something 345 + /// like `Vec<i32>` then the expanded code is `Vec<i32>::new()` which is invalid 346 + /// syntax. Ordinarily in handwritten Rust we would write `Vec::<i32>::new()` 347 + /// but for macros often the following is more convenient. 348 + /// 349 + /// ``` 350 + /// # use quote::quote; 351 + /// # 352 + /// # let field_type = quote!(...); 353 + /// # 354 + /// quote! { 355 + /// let value = <#field_type>::new(); 356 + /// } 357 + /// # ; 358 + /// ``` 359 + /// 360 + /// This expands to `<Vec<i32>>::new()` which behaves correctly. 361 + /// 362 + /// A similar pattern is appropriate for trait methods. 363 + /// 364 + /// ``` 365 + /// # use quote::quote; 366 + /// # 367 + /// # let field_type = quote!(...); 368 + /// # 369 + /// quote! { 370 + /// let value = <#field_type as core::default::Default>::default(); 371 + /// } 372 + /// # ; 373 + /// ``` 374 + /// 375 + /// <p><br></p> 376 + /// 377 + /// ### Interpolating text inside of doc comments 378 + /// 379 + /// Neither doc comments nor string literals get interpolation behavior in 380 + /// quote: 381 + /// 382 + /// ```compile_fail 383 + /// quote! { 384 + /// /// try to interpolate: #ident 385 + /// /// 386 + /// /// ... 387 + /// } 388 + /// ``` 389 + /// 390 + /// ```compile_fail 391 + /// quote! { 392 + /// #[doc = "try to interpolate: #ident"] 393 + /// } 394 + /// ``` 395 + /// 396 + /// Instead the best way to build doc comments that involve variables is by 397 + /// formatting the doc string literal outside of quote. 398 + /// 399 + /// ```rust 400 + /// # use proc_macro2::{Ident, Span}; 401 + /// # use quote::quote; 402 + /// # 403 + /// # const IGNORE: &str = stringify! { 404 + /// let msg = format!(...); 405 + /// # }; 406 + /// # 407 + /// # let ident = Ident::new("var", Span::call_site()); 408 + /// # let msg = format!("try to interpolate: {}", ident); 409 + /// quote! { 410 + /// #[doc = #msg] 411 + /// /// 412 + /// /// ... 413 + /// } 414 + /// # ; 415 + /// ``` 416 + /// 417 + /// <p><br></p> 418 + /// 419 + /// ### Indexing into a tuple struct 420 + /// 421 + /// When interpolating indices of a tuple or tuple struct, we need them not to 422 + /// appears suffixed as integer literals by interpolating them as [`syn::Index`] 423 + /// instead. 424 + /// 425 + /// [`syn::Index`]: https://docs.rs/syn/2.0/syn/struct.Index.html 426 + /// 427 + /// ```compile_fail 428 + /// let i = 0usize..self.fields.len(); 429 + /// 430 + /// // expands to 0 + self.0usize.heap_size() + self.1usize.heap_size() + ... 431 + /// // which is not valid syntax 432 + /// quote! { 433 + /// 0 #( + self.#i.heap_size() )* 434 + /// } 435 + /// ``` 436 + /// 437 + /// ``` 438 + /// # use proc_macro2::{Ident, TokenStream}; 439 + /// # use quote::quote; 440 + /// # 441 + /// # mod syn { 442 + /// # use proc_macro2::{Literal, TokenStream}; 443 + /// # use quote::{ToTokens, TokenStreamExt}; 444 + /// # 445 + /// # pub struct Index(usize); 446 + /// # 447 + /// # impl From<usize> for Index { 448 + /// # fn from(i: usize) -> Self { 449 + /// # Index(i) 450 + /// # } 451 + /// # } 452 + /// # 453 + /// # impl ToTokens for Index { 454 + /// # fn to_tokens(&self, tokens: &mut TokenStream) { 455 + /// # tokens.append(Literal::usize_unsuffixed(self.0)); 456 + /// # } 457 + /// # } 458 + /// # } 459 + /// # 460 + /// # struct Struct { 461 + /// # fields: Vec<Ident>, 462 + /// # } 463 + /// # 464 + /// # impl Struct { 465 + /// # fn example(&self) -> TokenStream { 466 + /// let i = (0..self.fields.len()).map(syn::Index::from); 467 + /// 468 + /// // expands to 0 + self.0.heap_size() + self.1.heap_size() + ... 469 + /// quote! { 470 + /// 0 #( + self.#i.heap_size() )* 471 + /// } 472 + /// # } 473 + /// # } 474 + /// ``` 475 + $quote 476 + }; 477 + } 478 + 479 + #[cfg(doc)] 480 + __quote![ 481 + #[macro_export] 482 + macro_rules! quote { 483 + ($($tt:tt)*) => { 484 + ... 485 + }; 486 + } 487 + ]; 488 + 489 + #[cfg(not(doc))] 490 + __quote![ 491 + #[macro_export] 492 + macro_rules! quote { 493 + () => { 494 + $crate::__private::TokenStream::new() 495 + }; 496 + 497 + // Special case rule for a single tt, for performance. 498 + ($tt:tt) => {{ 499 + let mut _s = $crate::__private::TokenStream::new(); 500 + $crate::quote_token!{$tt _s} 501 + _s 502 + }}; 503 + 504 + // Special case rules for two tts, for performance. 505 + (# $var:ident) => {{ 506 + let mut _s = $crate::__private::TokenStream::new(); 507 + $crate::ToTokens::to_tokens(&$var, &mut _s); 508 + _s 509 + }}; 510 + ($tt1:tt $tt2:tt) => {{ 511 + let mut _s = $crate::__private::TokenStream::new(); 512 + $crate::quote_token!{$tt1 _s} 513 + $crate::quote_token!{$tt2 _s} 514 + _s 515 + }}; 516 + 517 + // Rule for any other number of tokens. 518 + ($($tt:tt)*) => {{ 519 + let mut _s = $crate::__private::TokenStream::new(); 520 + $crate::quote_each_token!{_s $($tt)*} 521 + _s 522 + }}; 523 + } 524 + ]; 525 + 526 + macro_rules! __quote_spanned { 527 + ($quote_spanned:item) => { 528 + /// Same as `quote!`, but applies a given span to all tokens originating within 529 + /// the macro invocation. 530 + /// 531 + /// <br> 532 + /// 533 + /// # Syntax 534 + /// 535 + /// A span expression of type [`Span`], followed by `=>`, followed by the tokens 536 + /// to quote. The span expression should be brief &mdash; use a variable for 537 + /// anything more than a few characters. There should be no space before the 538 + /// `=>` token. 539 + /// 540 + /// [`Span`]: proc_macro2::Span 541 + /// 542 + /// ``` 543 + /// # use proc_macro2::Span; 544 + /// # use quote::quote_spanned; 545 + /// # 546 + /// # const IGNORE_TOKENS: &'static str = stringify! { 547 + /// let span = /* ... */; 548 + /// # }; 549 + /// # let span = Span::call_site(); 550 + /// # let init = 0; 551 + /// 552 + /// // On one line, use parentheses. 553 + /// let tokens = quote_spanned!(span=> Box::into_raw(Box::new(#init))); 554 + /// 555 + /// // On multiple lines, place the span at the top and use braces. 556 + /// let tokens = quote_spanned! {span=> 557 + /// Box::into_raw(Box::new(#init)) 558 + /// }; 559 + /// ``` 560 + /// 561 + /// The lack of space before the `=>` should look jarring to Rust programmers 562 + /// and this is intentional. The formatting is designed to be visibly 563 + /// off-balance and draw the eye a particular way, due to the span expression 564 + /// being evaluated in the context of the procedural macro and the remaining 565 + /// tokens being evaluated in the generated code. 566 + /// 567 + /// <br> 568 + /// 569 + /// # Hygiene 570 + /// 571 + /// Any interpolated tokens preserve the `Span` information provided by their 572 + /// `ToTokens` implementation. Tokens that originate within the `quote_spanned!` 573 + /// invocation are spanned with the given span argument. 574 + /// 575 + /// <br> 576 + /// 577 + /// # Example 578 + /// 579 + /// The following procedural macro code uses `quote_spanned!` to assert that a 580 + /// particular Rust type implements the [`Sync`] trait so that references can be 581 + /// safely shared between threads. 582 + /// 583 + /// ``` 584 + /// # use quote::{quote_spanned, TokenStreamExt, ToTokens}; 585 + /// # use proc_macro2::{Span, TokenStream}; 586 + /// # 587 + /// # struct Type; 588 + /// # 589 + /// # impl Type { 590 + /// # fn span(&self) -> Span { 591 + /// # Span::call_site() 592 + /// # } 593 + /// # } 594 + /// # 595 + /// # impl ToTokens for Type { 596 + /// # fn to_tokens(&self, _tokens: &mut TokenStream) {} 597 + /// # } 598 + /// # 599 + /// # let ty = Type; 600 + /// # let call_site = Span::call_site(); 601 + /// # 602 + /// let ty_span = ty.span(); 603 + /// let assert_sync = quote_spanned! {ty_span=> 604 + /// struct _AssertSync where #ty: Sync; 605 + /// }; 606 + /// ``` 607 + /// 608 + /// If the assertion fails, the user will see an error like the following. The 609 + /// input span of their type is highlighted in the error. 610 + /// 611 + /// ```text 612 + /// error[E0277]: the trait bound `*const (): std::marker::Sync` is not satisfied 613 + /// --> src/main.rs:10:21 614 + /// | 615 + /// 10 | static ref PTR: *const () = &(); 616 + /// | ^^^^^^^^^ `*const ()` cannot be shared between threads safely 617 + /// ``` 618 + /// 619 + /// In this example it is important for the where-clause to be spanned with the 620 + /// line/column information of the user's input type so that error messages are 621 + /// placed appropriately by the compiler. 622 + $quote_spanned 623 + }; 624 + } 625 + 626 + #[cfg(doc)] 627 + __quote_spanned![ 628 + #[macro_export] 629 + macro_rules! quote_spanned { 630 + ($span:expr=> $($tt:tt)*) => { 631 + ... 632 + }; 633 + } 634 + ]; 635 + 636 + #[cfg(not(doc))] 637 + __quote_spanned![ 638 + #[macro_export] 639 + macro_rules! quote_spanned { 640 + ($span:expr=>) => {{ 641 + let _: $crate::__private::Span = $crate::__private::get_span($span).__into_span(); 642 + $crate::__private::TokenStream::new() 643 + }}; 644 + 645 + // Special case rule for a single tt, for performance. 646 + ($span:expr=> $tt:tt) => {{ 647 + let mut _s = $crate::__private::TokenStream::new(); 648 + let _span: $crate::__private::Span = $crate::__private::get_span($span).__into_span(); 649 + $crate::quote_token_spanned!{$tt _s _span} 650 + _s 651 + }}; 652 + 653 + // Special case rules for two tts, for performance. 654 + ($span:expr=> # $var:ident) => {{ 655 + let mut _s = $crate::__private::TokenStream::new(); 656 + let _: $crate::__private::Span = $crate::__private::get_span($span).__into_span(); 657 + $crate::ToTokens::to_tokens(&$var, &mut _s); 658 + _s 659 + }}; 660 + ($span:expr=> $tt1:tt $tt2:tt) => {{ 661 + let mut _s = $crate::__private::TokenStream::new(); 662 + let _span: $crate::__private::Span = $crate::__private::get_span($span).__into_span(); 663 + $crate::quote_token_spanned!{$tt1 _s _span} 664 + $crate::quote_token_spanned!{$tt2 _s _span} 665 + _s 666 + }}; 667 + 668 + // Rule for any other number of tokens. 669 + ($span:expr=> $($tt:tt)*) => {{ 670 + let mut _s = $crate::__private::TokenStream::new(); 671 + let _span: $crate::__private::Span = $crate::__private::get_span($span).__into_span(); 672 + $crate::quote_each_token_spanned!{_s _span $($tt)*} 673 + _s 674 + }}; 675 + } 676 + ]; 677 + 678 + // Extract the names of all #metavariables and pass them to the $call macro. 679 + // 680 + // in: pounded_var_names!(then!(...) a #b c #( #d )* #e) 681 + // out: then!(... b); 682 + // then!(... d); 683 + // then!(... e); 684 + #[macro_export] 685 + #[doc(hidden)] 686 + macro_rules! pounded_var_names { 687 + ($call:ident! $extra:tt $($tts:tt)*) => { 688 + $crate::pounded_var_names_with_context!{$call! $extra 689 + (@ $($tts)*) 690 + ($($tts)* @) 691 + } 692 + }; 693 + } 694 + 695 + #[macro_export] 696 + #[doc(hidden)] 697 + macro_rules! pounded_var_names_with_context { 698 + ($call:ident! $extra:tt ($($b1:tt)*) ($($curr:tt)*)) => { 699 + $( 700 + $crate::pounded_var_with_context!{$call! $extra $b1 $curr} 701 + )* 702 + }; 703 + } 704 + 705 + #[macro_export] 706 + #[doc(hidden)] 707 + macro_rules! pounded_var_with_context { 708 + ($call:ident! $extra:tt $b1:tt ( $($inner:tt)* )) => { 709 + $crate::pounded_var_names!{$call! $extra $($inner)*} 710 + }; 711 + 712 + ($call:ident! $extra:tt $b1:tt [ $($inner:tt)* ]) => { 713 + $crate::pounded_var_names!{$call! $extra $($inner)*} 714 + }; 715 + 716 + ($call:ident! $extra:tt $b1:tt { $($inner:tt)* }) => { 717 + $crate::pounded_var_names!{$call! $extra $($inner)*} 718 + }; 719 + 720 + ($call:ident!($($extra:tt)*) # $var:ident) => { 721 + $crate::$call!($($extra)* $var); 722 + }; 723 + 724 + ($call:ident! $extra:tt $b1:tt $curr:tt) => {}; 725 + } 726 + 727 + #[macro_export] 728 + #[doc(hidden)] 729 + macro_rules! quote_bind_into_iter { 730 + ($has_iter:ident $var:ident) => { 731 + // `mut` may be unused if $var occurs multiple times in the list. 732 + #[allow(unused_mut)] 733 + let (mut $var, i) = $var.quote_into_iter(); 734 + let $has_iter = $has_iter | i; 735 + }; 736 + } 737 + 738 + #[macro_export] 739 + #[doc(hidden)] 740 + macro_rules! quote_bind_next_or_break { 741 + ($var:ident) => { 742 + let $var = match $var.next() { 743 + Some(_x) => $crate::__private::RepInterp(_x), 744 + None => break, 745 + }; 746 + }; 747 + } 748 + 749 + // The obvious way to write this macro is as a tt muncher. This implementation 750 + // does something more complex for two reasons. 751 + // 752 + // - With a tt muncher it's easy to hit Rust's built-in recursion_limit, which 753 + // this implementation avoids because it isn't tail recursive. 754 + // 755 + // - Compile times for a tt muncher are quadratic relative to the length of 756 + // the input. This implementation is linear, so it will be faster 757 + // (potentially much faster) for big inputs. However, the constant factors 758 + // of this implementation are higher than that of a tt muncher, so it is 759 + // somewhat slower than a tt muncher if there are many invocations with 760 + // short inputs. 761 + // 762 + // An invocation like this: 763 + // 764 + // quote_each_token!(_s a b c d e f g h i j); 765 + // 766 + // expands to this: 767 + // 768 + // quote_tokens_with_context!(_s 769 + // (@ @ @ @ @ @ a b c d e f g h i j) 770 + // (@ @ @ @ @ a b c d e f g h i j @) 771 + // (@ @ @ @ a b c d e f g h i j @ @) 772 + // (@ @ @ (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) @ @ @) 773 + // (@ @ a b c d e f g h i j @ @ @ @) 774 + // (@ a b c d e f g h i j @ @ @ @ @) 775 + // (a b c d e f g h i j @ @ @ @ @ @) 776 + // ); 777 + // 778 + // which gets transposed and expanded to this: 779 + // 780 + // quote_token_with_context!(_s @ @ @ @ @ @ a); 781 + // quote_token_with_context!(_s @ @ @ @ @ a b); 782 + // quote_token_with_context!(_s @ @ @ @ a b c); 783 + // quote_token_with_context!(_s @ @ @ (a) b c d); 784 + // quote_token_with_context!(_s @ @ a (b) c d e); 785 + // quote_token_with_context!(_s @ a b (c) d e f); 786 + // quote_token_with_context!(_s a b c (d) e f g); 787 + // quote_token_with_context!(_s b c d (e) f g h); 788 + // quote_token_with_context!(_s c d e (f) g h i); 789 + // quote_token_with_context!(_s d e f (g) h i j); 790 + // quote_token_with_context!(_s e f g (h) i j @); 791 + // quote_token_with_context!(_s f g h (i) j @ @); 792 + // quote_token_with_context!(_s g h i (j) @ @ @); 793 + // quote_token_with_context!(_s h i j @ @ @ @); 794 + // quote_token_with_context!(_s i j @ @ @ @ @); 795 + // quote_token_with_context!(_s j @ @ @ @ @ @); 796 + // 797 + // Without having used muncher-style recursion, we get one invocation of 798 + // quote_token_with_context for each original tt, with three tts of context on 799 + // either side. This is enough for the longest possible interpolation form (a 800 + // repetition with separator, as in `# (#var) , *`) to be fully represented with 801 + // the first or last tt in the middle. 802 + // 803 + // The middle tt (surrounded by parentheses) is the tt being processed. 804 + // 805 + // - When it is a `#`, quote_token_with_context can do an interpolation. The 806 + // interpolation kind will depend on the three subsequent tts. 807 + // 808 + // - When it is within a later part of an interpolation, it can be ignored 809 + // because the interpolation has already been done. 810 + // 811 + // - When it is not part of an interpolation it can be pushed as a single 812 + // token into the output. 813 + // 814 + // - When the middle token is an unparenthesized `@`, that call is one of the 815 + // first 3 or last 3 calls of quote_token_with_context and does not 816 + // correspond to one of the original input tokens, so turns into nothing. 817 + #[macro_export] 818 + #[doc(hidden)] 819 + macro_rules! quote_each_token { 820 + ($tokens:ident $($tts:tt)*) => { 821 + $crate::quote_tokens_with_context!{$tokens 822 + (@ @ @ @ @ @ $($tts)*) 823 + (@ @ @ @ @ $($tts)* @) 824 + (@ @ @ @ $($tts)* @ @) 825 + (@ @ @ $(($tts))* @ @ @) 826 + (@ @ $($tts)* @ @ @ @) 827 + (@ $($tts)* @ @ @ @ @) 828 + ($($tts)* @ @ @ @ @ @) 829 + } 830 + }; 831 + } 832 + 833 + // See the explanation on quote_each_token. 834 + #[macro_export] 835 + #[doc(hidden)] 836 + macro_rules! quote_each_token_spanned { 837 + ($tokens:ident $span:ident $($tts:tt)*) => { 838 + $crate::quote_tokens_with_context_spanned!{$tokens $span 839 + (@ @ @ @ @ @ $($tts)*) 840 + (@ @ @ @ @ $($tts)* @) 841 + (@ @ @ @ $($tts)* @ @) 842 + (@ @ @ $(($tts))* @ @ @) 843 + (@ @ $($tts)* @ @ @ @) 844 + (@ $($tts)* @ @ @ @ @) 845 + ($($tts)* @ @ @ @ @ @) 846 + } 847 + }; 848 + } 849 + 850 + // See the explanation on quote_each_token. 851 + #[macro_export] 852 + #[doc(hidden)] 853 + macro_rules! quote_tokens_with_context { 854 + ($tokens:ident 855 + ($($b3:tt)*) ($($b2:tt)*) ($($b1:tt)*) 856 + ($($curr:tt)*) 857 + ($($a1:tt)*) ($($a2:tt)*) ($($a3:tt)*) 858 + ) => { 859 + $( 860 + $crate::quote_token_with_context!{$tokens $b3 $b2 $b1 $curr $a1 $a2 $a3} 861 + )* 862 + }; 863 + } 864 + 865 + // See the explanation on quote_each_token. 866 + #[macro_export] 867 + #[doc(hidden)] 868 + macro_rules! quote_tokens_with_context_spanned { 869 + ($tokens:ident $span:ident 870 + ($($b3:tt)*) ($($b2:tt)*) ($($b1:tt)*) 871 + ($($curr:tt)*) 872 + ($($a1:tt)*) ($($a2:tt)*) ($($a3:tt)*) 873 + ) => { 874 + $( 875 + $crate::quote_token_with_context_spanned!{$tokens $span $b3 $b2 $b1 $curr $a1 $a2 $a3} 876 + )* 877 + }; 878 + } 879 + 880 + // See the explanation on quote_each_token. 881 + #[macro_export] 882 + #[doc(hidden)] 883 + macro_rules! quote_token_with_context { 884 + // Unparenthesized `@` indicates this call does not correspond to one of the 885 + // original input tokens. Ignore it. 886 + ($tokens:ident $b3:tt $b2:tt $b1:tt @ $a1:tt $a2:tt $a3:tt) => {}; 887 + 888 + // A repetition with no separator. 889 + ($tokens:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) * $a3:tt) => {{ 890 + use $crate::__private::ext::*; 891 + let has_iter = $crate::__private::ThereIsNoIteratorInRepetition; 892 + $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*} 893 + let _: $crate::__private::HasIterator = has_iter; 894 + // This is `while true` instead of `loop` because if there are no 895 + // iterators used inside of this repetition then the body would not 896 + // contain any `break`, so the compiler would emit unreachable code 897 + // warnings on anything below the loop. We use has_iter to detect and 898 + // fail to compile when there are no iterators, so here we just work 899 + // around the unneeded extra warning. 900 + while true { 901 + $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*} 902 + $crate::quote_each_token!{$tokens $($inner)*} 903 + } 904 + }}; 905 + // ... and one step later. 906 + ($tokens:ident $b3:tt $b2:tt # (( $($inner:tt)* )) * $a2:tt $a3:tt) => {}; 907 + // ... and one step later. 908 + ($tokens:ident $b3:tt # ( $($inner:tt)* ) (*) $a1:tt $a2:tt $a3:tt) => {}; 909 + 910 + // A repetition with separator. 911 + ($tokens:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) $sep:tt *) => {{ 912 + use $crate::__private::ext::*; 913 + let mut _i = 0usize; 914 + let has_iter = $crate::__private::ThereIsNoIteratorInRepetition; 915 + $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*} 916 + let _: $crate::__private::HasIterator = has_iter; 917 + while true { 918 + $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*} 919 + if _i > 0 { 920 + $crate::quote_token!{$sep $tokens} 921 + } 922 + _i += 1; 923 + $crate::quote_each_token!{$tokens $($inner)*} 924 + } 925 + }}; 926 + // ... and one step later. 927 + ($tokens:ident $b3:tt $b2:tt # (( $($inner:tt)* )) $sep:tt * $a3:tt) => {}; 928 + // ... and one step later. 929 + ($tokens:ident $b3:tt # ( $($inner:tt)* ) ($sep:tt) * $a2:tt $a3:tt) => {}; 930 + // (A special case for `#(var)**`, where the first `*` is treated as the 931 + // repetition symbol and the second `*` is treated as an ordinary token.) 932 + ($tokens:ident # ( $($inner:tt)* ) * (*) $a1:tt $a2:tt $a3:tt) => { 933 + // https://github.com/dtolnay/quote/issues/130 934 + $crate::quote_token!{* $tokens} 935 + }; 936 + // ... and one step later. 937 + ($tokens:ident # ( $($inner:tt)* ) $sep:tt (*) $a1:tt $a2:tt $a3:tt) => {}; 938 + 939 + // A non-repetition interpolation. 940 + ($tokens:ident $b3:tt $b2:tt $b1:tt (#) $var:ident $a2:tt $a3:tt) => { 941 + $crate::ToTokens::to_tokens(&$var, &mut $tokens); 942 + }; 943 + // ... and one step later. 944 + ($tokens:ident $b3:tt $b2:tt # ($var:ident) $a1:tt $a2:tt $a3:tt) => {}; 945 + 946 + // An ordinary token, not part of any interpolation. 947 + ($tokens:ident $b3:tt $b2:tt $b1:tt ($curr:tt) $a1:tt $a2:tt $a3:tt) => { 948 + $crate::quote_token!{$curr $tokens} 949 + }; 950 + } 951 + 952 + // See the explanation on quote_each_token, and on the individual rules of 953 + // quote_token_with_context. 954 + #[macro_export] 955 + #[doc(hidden)] 956 + macro_rules! quote_token_with_context_spanned { 957 + ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt @ $a1:tt $a2:tt $a3:tt) => {}; 958 + 959 + ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) * $a3:tt) => {{ 960 + use $crate::__private::ext::*; 961 + let has_iter = $crate::__private::ThereIsNoIteratorInRepetition; 962 + $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*} 963 + let _: $crate::__private::HasIterator = has_iter; 964 + while true { 965 + $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*} 966 + $crate::quote_each_token_spanned!{$tokens $span $($inner)*} 967 + } 968 + }}; 969 + ($tokens:ident $span:ident $b3:tt $b2:tt # (( $($inner:tt)* )) * $a2:tt $a3:tt) => {}; 970 + ($tokens:ident $span:ident $b3:tt # ( $($inner:tt)* ) (*) $a1:tt $a2:tt $a3:tt) => {}; 971 + 972 + ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) $sep:tt *) => {{ 973 + use $crate::__private::ext::*; 974 + let mut _i = 0usize; 975 + let has_iter = $crate::__private::ThereIsNoIteratorInRepetition; 976 + $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*} 977 + let _: $crate::__private::HasIterator = has_iter; 978 + while true { 979 + $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*} 980 + if _i > 0 { 981 + $crate::quote_token_spanned!{$sep $tokens $span} 982 + } 983 + _i += 1; 984 + $crate::quote_each_token_spanned!{$tokens $span $($inner)*} 985 + } 986 + }}; 987 + ($tokens:ident $span:ident $b3:tt $b2:tt # (( $($inner:tt)* )) $sep:tt * $a3:tt) => {}; 988 + ($tokens:ident $span:ident $b3:tt # ( $($inner:tt)* ) ($sep:tt) * $a2:tt $a3:tt) => {}; 989 + ($tokens:ident $span:ident # ( $($inner:tt)* ) * (*) $a1:tt $a2:tt $a3:tt) => { 990 + // https://github.com/dtolnay/quote/issues/130 991 + $crate::quote_token_spanned!{* $tokens $span} 992 + }; 993 + ($tokens:ident $span:ident # ( $($inner:tt)* ) $sep:tt (*) $a1:tt $a2:tt $a3:tt) => {}; 994 + 995 + ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) $var:ident $a2:tt $a3:tt) => { 996 + $crate::ToTokens::to_tokens(&$var, &mut $tokens); 997 + }; 998 + ($tokens:ident $span:ident $b3:tt $b2:tt # ($var:ident) $a1:tt $a2:tt $a3:tt) => {}; 999 + 1000 + ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt ($curr:tt) $a1:tt $a2:tt $a3:tt) => { 1001 + $crate::quote_token_spanned!{$curr $tokens $span} 1002 + }; 1003 + } 1004 + 1005 + // These rules are ordered by approximate token frequency, at least for the 1006 + // first 10 or so, to improve compile times. Having `ident` first is by far the 1007 + // most important because it's typically 2-3x more common than the next most 1008 + // common token. 1009 + // 1010 + // Separately, we put the token being matched in the very front so that failing 1011 + // rules may fail to match as quickly as possible. 1012 + #[macro_export] 1013 + #[doc(hidden)] 1014 + macro_rules! quote_token { 1015 + ($ident:ident $tokens:ident) => { 1016 + $crate::__private::push_ident(&mut $tokens, stringify!($ident)); 1017 + }; 1018 + 1019 + (:: $tokens:ident) => { 1020 + $crate::__private::push_colon2(&mut $tokens); 1021 + }; 1022 + 1023 + (( $($inner:tt)* ) $tokens:ident) => { 1024 + $crate::__private::push_group( 1025 + &mut $tokens, 1026 + $crate::__private::Delimiter::Parenthesis, 1027 + $crate::quote!($($inner)*), 1028 + ); 1029 + }; 1030 + 1031 + ([ $($inner:tt)* ] $tokens:ident) => { 1032 + $crate::__private::push_group( 1033 + &mut $tokens, 1034 + $crate::__private::Delimiter::Bracket, 1035 + $crate::quote!($($inner)*), 1036 + ); 1037 + }; 1038 + 1039 + ({ $($inner:tt)* } $tokens:ident) => { 1040 + $crate::__private::push_group( 1041 + &mut $tokens, 1042 + $crate::__private::Delimiter::Brace, 1043 + $crate::quote!($($inner)*), 1044 + ); 1045 + }; 1046 + 1047 + (# $tokens:ident) => { 1048 + $crate::__private::push_pound(&mut $tokens); 1049 + }; 1050 + 1051 + (, $tokens:ident) => { 1052 + $crate::__private::push_comma(&mut $tokens); 1053 + }; 1054 + 1055 + (. $tokens:ident) => { 1056 + $crate::__private::push_dot(&mut $tokens); 1057 + }; 1058 + 1059 + (; $tokens:ident) => { 1060 + $crate::__private::push_semi(&mut $tokens); 1061 + }; 1062 + 1063 + (: $tokens:ident) => { 1064 + $crate::__private::push_colon(&mut $tokens); 1065 + }; 1066 + 1067 + (+ $tokens:ident) => { 1068 + $crate::__private::push_add(&mut $tokens); 1069 + }; 1070 + 1071 + (+= $tokens:ident) => { 1072 + $crate::__private::push_add_eq(&mut $tokens); 1073 + }; 1074 + 1075 + (& $tokens:ident) => { 1076 + $crate::__private::push_and(&mut $tokens); 1077 + }; 1078 + 1079 + (&& $tokens:ident) => { 1080 + $crate::__private::push_and_and(&mut $tokens); 1081 + }; 1082 + 1083 + (&= $tokens:ident) => { 1084 + $crate::__private::push_and_eq(&mut $tokens); 1085 + }; 1086 + 1087 + (@ $tokens:ident) => { 1088 + $crate::__private::push_at(&mut $tokens); 1089 + }; 1090 + 1091 + (! $tokens:ident) => { 1092 + $crate::__private::push_bang(&mut $tokens); 1093 + }; 1094 + 1095 + (^ $tokens:ident) => { 1096 + $crate::__private::push_caret(&mut $tokens); 1097 + }; 1098 + 1099 + (^= $tokens:ident) => { 1100 + $crate::__private::push_caret_eq(&mut $tokens); 1101 + }; 1102 + 1103 + (/ $tokens:ident) => { 1104 + $crate::__private::push_div(&mut $tokens); 1105 + }; 1106 + 1107 + (/= $tokens:ident) => { 1108 + $crate::__private::push_div_eq(&mut $tokens); 1109 + }; 1110 + 1111 + (.. $tokens:ident) => { 1112 + $crate::__private::push_dot2(&mut $tokens); 1113 + }; 1114 + 1115 + (... $tokens:ident) => { 1116 + $crate::__private::push_dot3(&mut $tokens); 1117 + }; 1118 + 1119 + (..= $tokens:ident) => { 1120 + $crate::__private::push_dot_dot_eq(&mut $tokens); 1121 + }; 1122 + 1123 + (= $tokens:ident) => { 1124 + $crate::__private::push_eq(&mut $tokens); 1125 + }; 1126 + 1127 + (== $tokens:ident) => { 1128 + $crate::__private::push_eq_eq(&mut $tokens); 1129 + }; 1130 + 1131 + (>= $tokens:ident) => { 1132 + $crate::__private::push_ge(&mut $tokens); 1133 + }; 1134 + 1135 + (> $tokens:ident) => { 1136 + $crate::__private::push_gt(&mut $tokens); 1137 + }; 1138 + 1139 + (<= $tokens:ident) => { 1140 + $crate::__private::push_le(&mut $tokens); 1141 + }; 1142 + 1143 + (< $tokens:ident) => { 1144 + $crate::__private::push_lt(&mut $tokens); 1145 + }; 1146 + 1147 + (*= $tokens:ident) => { 1148 + $crate::__private::push_mul_eq(&mut $tokens); 1149 + }; 1150 + 1151 + (!= $tokens:ident) => { 1152 + $crate::__private::push_ne(&mut $tokens); 1153 + }; 1154 + 1155 + (| $tokens:ident) => { 1156 + $crate::__private::push_or(&mut $tokens); 1157 + }; 1158 + 1159 + (|= $tokens:ident) => { 1160 + $crate::__private::push_or_eq(&mut $tokens); 1161 + }; 1162 + 1163 + (|| $tokens:ident) => { 1164 + $crate::__private::push_or_or(&mut $tokens); 1165 + }; 1166 + 1167 + (? $tokens:ident) => { 1168 + $crate::__private::push_question(&mut $tokens); 1169 + }; 1170 + 1171 + (-> $tokens:ident) => { 1172 + $crate::__private::push_rarrow(&mut $tokens); 1173 + }; 1174 + 1175 + (<- $tokens:ident) => { 1176 + $crate::__private::push_larrow(&mut $tokens); 1177 + }; 1178 + 1179 + (% $tokens:ident) => { 1180 + $crate::__private::push_rem(&mut $tokens); 1181 + }; 1182 + 1183 + (%= $tokens:ident) => { 1184 + $crate::__private::push_rem_eq(&mut $tokens); 1185 + }; 1186 + 1187 + (=> $tokens:ident) => { 1188 + $crate::__private::push_fat_arrow(&mut $tokens); 1189 + }; 1190 + 1191 + (<< $tokens:ident) => { 1192 + $crate::__private::push_shl(&mut $tokens); 1193 + }; 1194 + 1195 + (<<= $tokens:ident) => { 1196 + $crate::__private::push_shl_eq(&mut $tokens); 1197 + }; 1198 + 1199 + (>> $tokens:ident) => { 1200 + $crate::__private::push_shr(&mut $tokens); 1201 + }; 1202 + 1203 + (>>= $tokens:ident) => { 1204 + $crate::__private::push_shr_eq(&mut $tokens); 1205 + }; 1206 + 1207 + (* $tokens:ident) => { 1208 + $crate::__private::push_star(&mut $tokens); 1209 + }; 1210 + 1211 + (- $tokens:ident) => { 1212 + $crate::__private::push_sub(&mut $tokens); 1213 + }; 1214 + 1215 + (-= $tokens:ident) => { 1216 + $crate::__private::push_sub_eq(&mut $tokens); 1217 + }; 1218 + 1219 + ($lifetime:lifetime $tokens:ident) => { 1220 + $crate::__private::push_lifetime(&mut $tokens, stringify!($lifetime)); 1221 + }; 1222 + 1223 + (_ $tokens:ident) => { 1224 + $crate::__private::push_underscore(&mut $tokens); 1225 + }; 1226 + 1227 + ($other:tt $tokens:ident) => { 1228 + $crate::__private::parse(&mut $tokens, stringify!($other)); 1229 + }; 1230 + } 1231 + 1232 + // See the comment above `quote_token!` about the rule ordering. 1233 + #[macro_export] 1234 + #[doc(hidden)] 1235 + macro_rules! quote_token_spanned { 1236 + ($ident:ident $tokens:ident $span:ident) => { 1237 + $crate::__private::push_ident_spanned(&mut $tokens, $span, stringify!($ident)); 1238 + }; 1239 + 1240 + (:: $tokens:ident $span:ident) => { 1241 + $crate::__private::push_colon2_spanned(&mut $tokens, $span); 1242 + }; 1243 + 1244 + (( $($inner:tt)* ) $tokens:ident $span:ident) => { 1245 + $crate::__private::push_group_spanned( 1246 + &mut $tokens, 1247 + $span, 1248 + $crate::__private::Delimiter::Parenthesis, 1249 + $crate::quote_spanned!($span=> $($inner)*), 1250 + ); 1251 + }; 1252 + 1253 + ([ $($inner:tt)* ] $tokens:ident $span:ident) => { 1254 + $crate::__private::push_group_spanned( 1255 + &mut $tokens, 1256 + $span, 1257 + $crate::__private::Delimiter::Bracket, 1258 + $crate::quote_spanned!($span=> $($inner)*), 1259 + ); 1260 + }; 1261 + 1262 + ({ $($inner:tt)* } $tokens:ident $span:ident) => { 1263 + $crate::__private::push_group_spanned( 1264 + &mut $tokens, 1265 + $span, 1266 + $crate::__private::Delimiter::Brace, 1267 + $crate::quote_spanned!($span=> $($inner)*), 1268 + ); 1269 + }; 1270 + 1271 + (# $tokens:ident $span:ident) => { 1272 + $crate::__private::push_pound_spanned(&mut $tokens, $span); 1273 + }; 1274 + 1275 + (, $tokens:ident $span:ident) => { 1276 + $crate::__private::push_comma_spanned(&mut $tokens, $span); 1277 + }; 1278 + 1279 + (. $tokens:ident $span:ident) => { 1280 + $crate::__private::push_dot_spanned(&mut $tokens, $span); 1281 + }; 1282 + 1283 + (; $tokens:ident $span:ident) => { 1284 + $crate::__private::push_semi_spanned(&mut $tokens, $span); 1285 + }; 1286 + 1287 + (: $tokens:ident $span:ident) => { 1288 + $crate::__private::push_colon_spanned(&mut $tokens, $span); 1289 + }; 1290 + 1291 + (+ $tokens:ident $span:ident) => { 1292 + $crate::__private::push_add_spanned(&mut $tokens, $span); 1293 + }; 1294 + 1295 + (+= $tokens:ident $span:ident) => { 1296 + $crate::__private::push_add_eq_spanned(&mut $tokens, $span); 1297 + }; 1298 + 1299 + (& $tokens:ident $span:ident) => { 1300 + $crate::__private::push_and_spanned(&mut $tokens, $span); 1301 + }; 1302 + 1303 + (&& $tokens:ident $span:ident) => { 1304 + $crate::__private::push_and_and_spanned(&mut $tokens, $span); 1305 + }; 1306 + 1307 + (&= $tokens:ident $span:ident) => { 1308 + $crate::__private::push_and_eq_spanned(&mut $tokens, $span); 1309 + }; 1310 + 1311 + (@ $tokens:ident $span:ident) => { 1312 + $crate::__private::push_at_spanned(&mut $tokens, $span); 1313 + }; 1314 + 1315 + (! $tokens:ident $span:ident) => { 1316 + $crate::__private::push_bang_spanned(&mut $tokens, $span); 1317 + }; 1318 + 1319 + (^ $tokens:ident $span:ident) => { 1320 + $crate::__private::push_caret_spanned(&mut $tokens, $span); 1321 + }; 1322 + 1323 + (^= $tokens:ident $span:ident) => { 1324 + $crate::__private::push_caret_eq_spanned(&mut $tokens, $span); 1325 + }; 1326 + 1327 + (/ $tokens:ident $span:ident) => { 1328 + $crate::__private::push_div_spanned(&mut $tokens, $span); 1329 + }; 1330 + 1331 + (/= $tokens:ident $span:ident) => { 1332 + $crate::__private::push_div_eq_spanned(&mut $tokens, $span); 1333 + }; 1334 + 1335 + (.. $tokens:ident $span:ident) => { 1336 + $crate::__private::push_dot2_spanned(&mut $tokens, $span); 1337 + }; 1338 + 1339 + (... $tokens:ident $span:ident) => { 1340 + $crate::__private::push_dot3_spanned(&mut $tokens, $span); 1341 + }; 1342 + 1343 + (..= $tokens:ident $span:ident) => { 1344 + $crate::__private::push_dot_dot_eq_spanned(&mut $tokens, $span); 1345 + }; 1346 + 1347 + (= $tokens:ident $span:ident) => { 1348 + $crate::__private::push_eq_spanned(&mut $tokens, $span); 1349 + }; 1350 + 1351 + (== $tokens:ident $span:ident) => { 1352 + $crate::__private::push_eq_eq_spanned(&mut $tokens, $span); 1353 + }; 1354 + 1355 + (>= $tokens:ident $span:ident) => { 1356 + $crate::__private::push_ge_spanned(&mut $tokens, $span); 1357 + }; 1358 + 1359 + (> $tokens:ident $span:ident) => { 1360 + $crate::__private::push_gt_spanned(&mut $tokens, $span); 1361 + }; 1362 + 1363 + (<= $tokens:ident $span:ident) => { 1364 + $crate::__private::push_le_spanned(&mut $tokens, $span); 1365 + }; 1366 + 1367 + (< $tokens:ident $span:ident) => { 1368 + $crate::__private::push_lt_spanned(&mut $tokens, $span); 1369 + }; 1370 + 1371 + (*= $tokens:ident $span:ident) => { 1372 + $crate::__private::push_mul_eq_spanned(&mut $tokens, $span); 1373 + }; 1374 + 1375 + (!= $tokens:ident $span:ident) => { 1376 + $crate::__private::push_ne_spanned(&mut $tokens, $span); 1377 + }; 1378 + 1379 + (| $tokens:ident $span:ident) => { 1380 + $crate::__private::push_or_spanned(&mut $tokens, $span); 1381 + }; 1382 + 1383 + (|= $tokens:ident $span:ident) => { 1384 + $crate::__private::push_or_eq_spanned(&mut $tokens, $span); 1385 + }; 1386 + 1387 + (|| $tokens:ident $span:ident) => { 1388 + $crate::__private::push_or_or_spanned(&mut $tokens, $span); 1389 + }; 1390 + 1391 + (? $tokens:ident $span:ident) => { 1392 + $crate::__private::push_question_spanned(&mut $tokens, $span); 1393 + }; 1394 + 1395 + (-> $tokens:ident $span:ident) => { 1396 + $crate::__private::push_rarrow_spanned(&mut $tokens, $span); 1397 + }; 1398 + 1399 + (<- $tokens:ident $span:ident) => { 1400 + $crate::__private::push_larrow_spanned(&mut $tokens, $span); 1401 + }; 1402 + 1403 + (% $tokens:ident $span:ident) => { 1404 + $crate::__private::push_rem_spanned(&mut $tokens, $span); 1405 + }; 1406 + 1407 + (%= $tokens:ident $span:ident) => { 1408 + $crate::__private::push_rem_eq_spanned(&mut $tokens, $span); 1409 + }; 1410 + 1411 + (=> $tokens:ident $span:ident) => { 1412 + $crate::__private::push_fat_arrow_spanned(&mut $tokens, $span); 1413 + }; 1414 + 1415 + (<< $tokens:ident $span:ident) => { 1416 + $crate::__private::push_shl_spanned(&mut $tokens, $span); 1417 + }; 1418 + 1419 + (<<= $tokens:ident $span:ident) => { 1420 + $crate::__private::push_shl_eq_spanned(&mut $tokens, $span); 1421 + }; 1422 + 1423 + (>> $tokens:ident $span:ident) => { 1424 + $crate::__private::push_shr_spanned(&mut $tokens, $span); 1425 + }; 1426 + 1427 + (>>= $tokens:ident $span:ident) => { 1428 + $crate::__private::push_shr_eq_spanned(&mut $tokens, $span); 1429 + }; 1430 + 1431 + (* $tokens:ident $span:ident) => { 1432 + $crate::__private::push_star_spanned(&mut $tokens, $span); 1433 + }; 1434 + 1435 + (- $tokens:ident $span:ident) => { 1436 + $crate::__private::push_sub_spanned(&mut $tokens, $span); 1437 + }; 1438 + 1439 + (-= $tokens:ident $span:ident) => { 1440 + $crate::__private::push_sub_eq_spanned(&mut $tokens, $span); 1441 + }; 1442 + 1443 + ($lifetime:lifetime $tokens:ident $span:ident) => { 1444 + $crate::__private::push_lifetime_spanned(&mut $tokens, $span, stringify!($lifetime)); 1445 + }; 1446 + 1447 + (_ $tokens:ident $span:ident) => { 1448 + $crate::__private::push_underscore_spanned(&mut $tokens, $span); 1449 + }; 1450 + 1451 + ($other:tt $tokens:ident $span:ident) => { 1452 + $crate::__private::parse_spanned(&mut $tokens, $span, stringify!($other)); 1453 + }; 1454 + }
+492
rust/quote/runtime.rs
··· 1 + use self::get_span::{GetSpan, GetSpanBase, GetSpanInner}; 2 + use crate::{IdentFragment, ToTokens, TokenStreamExt}; 3 + use core::fmt; 4 + use core::iter; 5 + use core::ops::BitOr; 6 + use proc_macro2::{Group, Ident, Punct, Spacing, TokenTree}; 7 + 8 + #[doc(hidden)] 9 + pub use alloc::format; 10 + #[doc(hidden)] 11 + pub use core::option::Option; 12 + 13 + #[doc(hidden)] 14 + pub type Delimiter = proc_macro2::Delimiter; 15 + #[doc(hidden)] 16 + pub type Span = proc_macro2::Span; 17 + #[doc(hidden)] 18 + pub type TokenStream = proc_macro2::TokenStream; 19 + 20 + #[doc(hidden)] 21 + pub struct HasIterator; // True 22 + #[doc(hidden)] 23 + pub struct ThereIsNoIteratorInRepetition; // False 24 + 25 + impl BitOr<ThereIsNoIteratorInRepetition> for ThereIsNoIteratorInRepetition { 26 + type Output = ThereIsNoIteratorInRepetition; 27 + fn bitor(self, _rhs: ThereIsNoIteratorInRepetition) -> ThereIsNoIteratorInRepetition { 28 + ThereIsNoIteratorInRepetition 29 + } 30 + } 31 + 32 + impl BitOr<ThereIsNoIteratorInRepetition> for HasIterator { 33 + type Output = HasIterator; 34 + fn bitor(self, _rhs: ThereIsNoIteratorInRepetition) -> HasIterator { 35 + HasIterator 36 + } 37 + } 38 + 39 + impl BitOr<HasIterator> for ThereIsNoIteratorInRepetition { 40 + type Output = HasIterator; 41 + fn bitor(self, _rhs: HasIterator) -> HasIterator { 42 + HasIterator 43 + } 44 + } 45 + 46 + impl BitOr<HasIterator> for HasIterator { 47 + type Output = HasIterator; 48 + fn bitor(self, _rhs: HasIterator) -> HasIterator { 49 + HasIterator 50 + } 51 + } 52 + 53 + /// Extension traits used by the implementation of `quote!`. These are defined 54 + /// in separate traits, rather than as a single trait due to ambiguity issues. 55 + /// 56 + /// These traits expose a `quote_into_iter` method which should allow calling 57 + /// whichever impl happens to be applicable. Calling that method repeatedly on 58 + /// the returned value should be idempotent. 59 + #[doc(hidden)] 60 + pub mod ext { 61 + use super::RepInterp; 62 + use super::{HasIterator as HasIter, ThereIsNoIteratorInRepetition as DoesNotHaveIter}; 63 + use crate::ToTokens; 64 + use alloc::collections::btree_set::{self, BTreeSet}; 65 + use core::slice; 66 + 67 + /// Extension trait providing the `quote_into_iter` method on iterators. 68 + #[doc(hidden)] 69 + pub trait RepIteratorExt: Iterator + Sized { 70 + fn quote_into_iter(self) -> (Self, HasIter) { 71 + (self, HasIter) 72 + } 73 + } 74 + 75 + impl<T: Iterator> RepIteratorExt for T {} 76 + 77 + /// Extension trait providing the `quote_into_iter` method for 78 + /// non-iterable types. These types interpolate the same value in each 79 + /// iteration of the repetition. 80 + #[doc(hidden)] 81 + pub trait RepToTokensExt { 82 + /// Pretend to be an iterator for the purposes of `quote_into_iter`. 83 + /// This allows repeated calls to `quote_into_iter` to continue 84 + /// correctly returning DoesNotHaveIter. 85 + fn next(&self) -> Option<&Self> { 86 + Some(self) 87 + } 88 + 89 + fn quote_into_iter(&self) -> (&Self, DoesNotHaveIter) { 90 + (self, DoesNotHaveIter) 91 + } 92 + } 93 + 94 + impl<T: ToTokens + ?Sized> RepToTokensExt for T {} 95 + 96 + /// Extension trait providing the `quote_into_iter` method for types that 97 + /// can be referenced as an iterator. 98 + #[doc(hidden)] 99 + pub trait RepAsIteratorExt<'q> { 100 + type Iter: Iterator; 101 + 102 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter); 103 + } 104 + 105 + impl<'q, T: RepAsIteratorExt<'q> + ?Sized> RepAsIteratorExt<'q> for &T { 106 + type Iter = T::Iter; 107 + 108 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { 109 + <T as RepAsIteratorExt>::quote_into_iter(*self) 110 + } 111 + } 112 + 113 + impl<'q, T: RepAsIteratorExt<'q> + ?Sized> RepAsIteratorExt<'q> for &mut T { 114 + type Iter = T::Iter; 115 + 116 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { 117 + <T as RepAsIteratorExt>::quote_into_iter(*self) 118 + } 119 + } 120 + 121 + impl<'q, T: 'q> RepAsIteratorExt<'q> for [T] { 122 + type Iter = slice::Iter<'q, T>; 123 + 124 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { 125 + (self.iter(), HasIter) 126 + } 127 + } 128 + 129 + impl<'q, T: 'q, const N: usize> RepAsIteratorExt<'q> for [T; N] { 130 + type Iter = slice::Iter<'q, T>; 131 + 132 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { 133 + (self.iter(), HasIter) 134 + } 135 + } 136 + 137 + impl<'q, T: 'q> RepAsIteratorExt<'q> for Vec<T> { 138 + type Iter = slice::Iter<'q, T>; 139 + 140 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { 141 + (self.iter(), HasIter) 142 + } 143 + } 144 + 145 + impl<'q, T: 'q> RepAsIteratorExt<'q> for BTreeSet<T> { 146 + type Iter = btree_set::Iter<'q, T>; 147 + 148 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { 149 + (self.iter(), HasIter) 150 + } 151 + } 152 + 153 + impl<'q, T: RepAsIteratorExt<'q>> RepAsIteratorExt<'q> for RepInterp<T> { 154 + type Iter = T::Iter; 155 + 156 + fn quote_into_iter(&'q self) -> (Self::Iter, HasIter) { 157 + self.0.quote_into_iter() 158 + } 159 + } 160 + } 161 + 162 + // Helper type used within interpolations to allow for repeated binding names. 163 + // Implements the relevant traits, and exports a dummy `next()` method. 164 + #[derive(Copy, Clone)] 165 + #[doc(hidden)] 166 + pub struct RepInterp<T>(pub T); 167 + 168 + impl<T> RepInterp<T> { 169 + // This method is intended to look like `Iterator::next`, and is called when 170 + // a name is bound multiple times, as the previous binding will shadow the 171 + // original `Iterator` object. This allows us to avoid advancing the 172 + // iterator multiple times per iteration. 173 + pub fn next(self) -> Option<T> { 174 + Some(self.0) 175 + } 176 + } 177 + 178 + impl<T: Iterator> Iterator for RepInterp<T> { 179 + type Item = T::Item; 180 + 181 + fn next(&mut self) -> Option<Self::Item> { 182 + self.0.next() 183 + } 184 + } 185 + 186 + impl<T: ToTokens> ToTokens for RepInterp<T> { 187 + fn to_tokens(&self, tokens: &mut TokenStream) { 188 + self.0.to_tokens(tokens); 189 + } 190 + } 191 + 192 + #[doc(hidden)] 193 + #[inline] 194 + pub fn get_span<T>(span: T) -> GetSpan<T> { 195 + GetSpan(GetSpanInner(GetSpanBase(span))) 196 + } 197 + 198 + mod get_span { 199 + use core::ops::Deref; 200 + use proc_macro2::extra::DelimSpan; 201 + use proc_macro2::Span; 202 + 203 + pub struct GetSpan<T>(pub(crate) GetSpanInner<T>); 204 + 205 + pub struct GetSpanInner<T>(pub(crate) GetSpanBase<T>); 206 + 207 + pub struct GetSpanBase<T>(pub(crate) T); 208 + 209 + impl GetSpan<Span> { 210 + #[inline] 211 + pub fn __into_span(self) -> Span { 212 + ((self.0).0).0 213 + } 214 + } 215 + 216 + impl GetSpanInner<DelimSpan> { 217 + #[inline] 218 + pub fn __into_span(&self) -> Span { 219 + (self.0).0.join() 220 + } 221 + } 222 + 223 + impl<T> GetSpanBase<T> { 224 + #[allow(clippy::unused_self)] 225 + pub fn __into_span(&self) -> T { 226 + unreachable!() 227 + } 228 + } 229 + 230 + impl<T> Deref for GetSpan<T> { 231 + type Target = GetSpanInner<T>; 232 + 233 + #[inline] 234 + fn deref(&self) -> &Self::Target { 235 + &self.0 236 + } 237 + } 238 + 239 + impl<T> Deref for GetSpanInner<T> { 240 + type Target = GetSpanBase<T>; 241 + 242 + #[inline] 243 + fn deref(&self) -> &Self::Target { 244 + &self.0 245 + } 246 + } 247 + } 248 + 249 + #[doc(hidden)] 250 + pub fn push_group(tokens: &mut TokenStream, delimiter: Delimiter, inner: TokenStream) { 251 + tokens.append(Group::new(delimiter, inner)); 252 + } 253 + 254 + #[doc(hidden)] 255 + pub fn push_group_spanned( 256 + tokens: &mut TokenStream, 257 + span: Span, 258 + delimiter: Delimiter, 259 + inner: TokenStream, 260 + ) { 261 + let mut g = Group::new(delimiter, inner); 262 + g.set_span(span); 263 + tokens.append(g); 264 + } 265 + 266 + #[doc(hidden)] 267 + pub fn parse(tokens: &mut TokenStream, s: &str) { 268 + let s: TokenStream = s.parse().expect("invalid token stream"); 269 + tokens.extend(iter::once(s)); 270 + } 271 + 272 + #[doc(hidden)] 273 + pub fn parse_spanned(tokens: &mut TokenStream, span: Span, s: &str) { 274 + let s: TokenStream = s.parse().expect("invalid token stream"); 275 + tokens.extend(s.into_iter().map(|t| respan_token_tree(t, span))); 276 + } 277 + 278 + // Token tree with every span replaced by the given one. 279 + fn respan_token_tree(mut token: TokenTree, span: Span) -> TokenTree { 280 + match &mut token { 281 + TokenTree::Group(g) => { 282 + let stream = g 283 + .stream() 284 + .into_iter() 285 + .map(|token| respan_token_tree(token, span)) 286 + .collect(); 287 + *g = Group::new(g.delimiter(), stream); 288 + g.set_span(span); 289 + } 290 + other => other.set_span(span), 291 + } 292 + token 293 + } 294 + 295 + #[doc(hidden)] 296 + pub fn push_ident(tokens: &mut TokenStream, s: &str) { 297 + let span = Span::call_site(); 298 + push_ident_spanned(tokens, span, s); 299 + } 300 + 301 + #[doc(hidden)] 302 + pub fn push_ident_spanned(tokens: &mut TokenStream, span: Span, s: &str) { 303 + tokens.append(ident_maybe_raw(s, span)); 304 + } 305 + 306 + #[doc(hidden)] 307 + pub fn push_lifetime(tokens: &mut TokenStream, lifetime: &str) { 308 + tokens.extend([ 309 + TokenTree::Punct(Punct::new('\'', Spacing::Joint)), 310 + TokenTree::Ident(Ident::new(&lifetime[1..], Span::call_site())), 311 + ]); 312 + } 313 + 314 + #[doc(hidden)] 315 + pub fn push_lifetime_spanned(tokens: &mut TokenStream, span: Span, lifetime: &str) { 316 + tokens.extend([ 317 + TokenTree::Punct({ 318 + let mut apostrophe = Punct::new('\'', Spacing::Joint); 319 + apostrophe.set_span(span); 320 + apostrophe 321 + }), 322 + TokenTree::Ident(Ident::new(&lifetime[1..], span)), 323 + ]); 324 + } 325 + 326 + macro_rules! push_punct { 327 + ($name:ident $spanned:ident $char1:tt) => { 328 + #[doc(hidden)] 329 + pub fn $name(tokens: &mut TokenStream) { 330 + tokens.append(Punct::new($char1, Spacing::Alone)); 331 + } 332 + #[doc(hidden)] 333 + pub fn $spanned(tokens: &mut TokenStream, span: Span) { 334 + let mut punct = Punct::new($char1, Spacing::Alone); 335 + punct.set_span(span); 336 + tokens.append(punct); 337 + } 338 + }; 339 + ($name:ident $spanned:ident $char1:tt $char2:tt) => { 340 + #[doc(hidden)] 341 + pub fn $name(tokens: &mut TokenStream) { 342 + tokens.append(Punct::new($char1, Spacing::Joint)); 343 + tokens.append(Punct::new($char2, Spacing::Alone)); 344 + } 345 + #[doc(hidden)] 346 + pub fn $spanned(tokens: &mut TokenStream, span: Span) { 347 + let mut punct = Punct::new($char1, Spacing::Joint); 348 + punct.set_span(span); 349 + tokens.append(punct); 350 + let mut punct = Punct::new($char2, Spacing::Alone); 351 + punct.set_span(span); 352 + tokens.append(punct); 353 + } 354 + }; 355 + ($name:ident $spanned:ident $char1:tt $char2:tt $char3:tt) => { 356 + #[doc(hidden)] 357 + pub fn $name(tokens: &mut TokenStream) { 358 + tokens.append(Punct::new($char1, Spacing::Joint)); 359 + tokens.append(Punct::new($char2, Spacing::Joint)); 360 + tokens.append(Punct::new($char3, Spacing::Alone)); 361 + } 362 + #[doc(hidden)] 363 + pub fn $spanned(tokens: &mut TokenStream, span: Span) { 364 + let mut punct = Punct::new($char1, Spacing::Joint); 365 + punct.set_span(span); 366 + tokens.append(punct); 367 + let mut punct = Punct::new($char2, Spacing::Joint); 368 + punct.set_span(span); 369 + tokens.append(punct); 370 + let mut punct = Punct::new($char3, Spacing::Alone); 371 + punct.set_span(span); 372 + tokens.append(punct); 373 + } 374 + }; 375 + } 376 + 377 + push_punct!(push_add push_add_spanned '+'); 378 + push_punct!(push_add_eq push_add_eq_spanned '+' '='); 379 + push_punct!(push_and push_and_spanned '&'); 380 + push_punct!(push_and_and push_and_and_spanned '&' '&'); 381 + push_punct!(push_and_eq push_and_eq_spanned '&' '='); 382 + push_punct!(push_at push_at_spanned '@'); 383 + push_punct!(push_bang push_bang_spanned '!'); 384 + push_punct!(push_caret push_caret_spanned '^'); 385 + push_punct!(push_caret_eq push_caret_eq_spanned '^' '='); 386 + push_punct!(push_colon push_colon_spanned ':'); 387 + push_punct!(push_colon2 push_colon2_spanned ':' ':'); 388 + push_punct!(push_comma push_comma_spanned ','); 389 + push_punct!(push_div push_div_spanned '/'); 390 + push_punct!(push_div_eq push_div_eq_spanned '/' '='); 391 + push_punct!(push_dot push_dot_spanned '.'); 392 + push_punct!(push_dot2 push_dot2_spanned '.' '.'); 393 + push_punct!(push_dot3 push_dot3_spanned '.' '.' '.'); 394 + push_punct!(push_dot_dot_eq push_dot_dot_eq_spanned '.' '.' '='); 395 + push_punct!(push_eq push_eq_spanned '='); 396 + push_punct!(push_eq_eq push_eq_eq_spanned '=' '='); 397 + push_punct!(push_ge push_ge_spanned '>' '='); 398 + push_punct!(push_gt push_gt_spanned '>'); 399 + push_punct!(push_le push_le_spanned '<' '='); 400 + push_punct!(push_lt push_lt_spanned '<'); 401 + push_punct!(push_mul_eq push_mul_eq_spanned '*' '='); 402 + push_punct!(push_ne push_ne_spanned '!' '='); 403 + push_punct!(push_or push_or_spanned '|'); 404 + push_punct!(push_or_eq push_or_eq_spanned '|' '='); 405 + push_punct!(push_or_or push_or_or_spanned '|' '|'); 406 + push_punct!(push_pound push_pound_spanned '#'); 407 + push_punct!(push_question push_question_spanned '?'); 408 + push_punct!(push_rarrow push_rarrow_spanned '-' '>'); 409 + push_punct!(push_larrow push_larrow_spanned '<' '-'); 410 + push_punct!(push_rem push_rem_spanned '%'); 411 + push_punct!(push_rem_eq push_rem_eq_spanned '%' '='); 412 + push_punct!(push_fat_arrow push_fat_arrow_spanned '=' '>'); 413 + push_punct!(push_semi push_semi_spanned ';'); 414 + push_punct!(push_shl push_shl_spanned '<' '<'); 415 + push_punct!(push_shl_eq push_shl_eq_spanned '<' '<' '='); 416 + push_punct!(push_shr push_shr_spanned '>' '>'); 417 + push_punct!(push_shr_eq push_shr_eq_spanned '>' '>' '='); 418 + push_punct!(push_star push_star_spanned '*'); 419 + push_punct!(push_sub push_sub_spanned '-'); 420 + push_punct!(push_sub_eq push_sub_eq_spanned '-' '='); 421 + 422 + #[doc(hidden)] 423 + pub fn push_underscore(tokens: &mut TokenStream) { 424 + push_underscore_spanned(tokens, Span::call_site()); 425 + } 426 + 427 + #[doc(hidden)] 428 + pub fn push_underscore_spanned(tokens: &mut TokenStream, span: Span) { 429 + tokens.append(Ident::new("_", span)); 430 + } 431 + 432 + // Helper method for constructing identifiers from the `format_ident!` macro, 433 + // handling `r#` prefixes. 434 + #[doc(hidden)] 435 + pub fn mk_ident(id: &str, span: Option<Span>) -> Ident { 436 + let span = span.unwrap_or_else(Span::call_site); 437 + ident_maybe_raw(id, span) 438 + } 439 + 440 + fn ident_maybe_raw(id: &str, span: Span) -> Ident { 441 + if let Some(id) = id.strip_prefix("r#") { 442 + Ident::new_raw(id, span) 443 + } else { 444 + Ident::new(id, span) 445 + } 446 + } 447 + 448 + // Adapts from `IdentFragment` to `fmt::Display` for use by the `format_ident!` 449 + // macro, and exposes span information from these fragments. 450 + // 451 + // This struct also has forwarding implementations of the formatting traits 452 + // `Octal`, `LowerHex`, `UpperHex`, and `Binary` to allow for their use within 453 + // `format_ident!`. 454 + #[derive(Copy, Clone)] 455 + #[doc(hidden)] 456 + pub struct IdentFragmentAdapter<T: IdentFragment>(pub T); 457 + 458 + impl<T: IdentFragment> IdentFragmentAdapter<T> { 459 + pub fn span(&self) -> Option<Span> { 460 + self.0.span() 461 + } 462 + } 463 + 464 + impl<T: IdentFragment> fmt::Display for IdentFragmentAdapter<T> { 465 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 466 + IdentFragment::fmt(&self.0, f) 467 + } 468 + } 469 + 470 + impl<T: IdentFragment + fmt::Octal> fmt::Octal for IdentFragmentAdapter<T> { 471 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 472 + fmt::Octal::fmt(&self.0, f) 473 + } 474 + } 475 + 476 + impl<T: IdentFragment + fmt::LowerHex> fmt::LowerHex for IdentFragmentAdapter<T> { 477 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 478 + fmt::LowerHex::fmt(&self.0, f) 479 + } 480 + } 481 + 482 + impl<T: IdentFragment + fmt::UpperHex> fmt::UpperHex for IdentFragmentAdapter<T> { 483 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 484 + fmt::UpperHex::fmt(&self.0, f) 485 + } 486 + } 487 + 488 + impl<T: IdentFragment + fmt::Binary> fmt::Binary for IdentFragmentAdapter<T> { 489 + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 490 + fmt::Binary::fmt(&self.0, f) 491 + } 492 + }
+50
rust/quote/spanned.rs
··· 1 + use crate::ToTokens; 2 + use proc_macro2::extra::DelimSpan; 3 + use proc_macro2::{Span, TokenStream}; 4 + 5 + // Not public API other than via the syn crate. Use syn::spanned::Spanned. 6 + pub trait Spanned: private::Sealed { 7 + fn __span(&self) -> Span; 8 + } 9 + 10 + impl Spanned for Span { 11 + fn __span(&self) -> Span { 12 + *self 13 + } 14 + } 15 + 16 + impl Spanned for DelimSpan { 17 + fn __span(&self) -> Span { 18 + self.join() 19 + } 20 + } 21 + 22 + impl<T: ?Sized + ToTokens> Spanned for T { 23 + fn __span(&self) -> Span { 24 + join_spans(self.into_token_stream()) 25 + } 26 + } 27 + 28 + fn join_spans(tokens: TokenStream) -> Span { 29 + let mut iter = tokens.into_iter().map(|tt| tt.span()); 30 + 31 + let first = match iter.next() { 32 + Some(span) => span, 33 + None => return Span::call_site(), 34 + }; 35 + 36 + iter.fold(None, |_prev, next| Some(next)) 37 + .and_then(|last| first.join(last)) 38 + .unwrap_or(first) 39 + } 40 + 41 + mod private { 42 + use crate::ToTokens; 43 + use proc_macro2::extra::DelimSpan; 44 + use proc_macro2::Span; 45 + 46 + pub trait Sealed {} 47 + impl Sealed for Span {} 48 + impl Sealed for DelimSpan {} 49 + impl<T: ?Sized + ToTokens> Sealed for T {} 50 + }
+271
rust/quote/to_tokens.rs
··· 1 + use super::TokenStreamExt; 2 + use alloc::borrow::Cow; 3 + use alloc::rc::Rc; 4 + use core::iter; 5 + use proc_macro2::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree}; 6 + use std::ffi::{CStr, CString}; 7 + 8 + /// Types that can be interpolated inside a `quote!` invocation. 9 + pub trait ToTokens { 10 + /// Write `self` to the given `TokenStream`. 11 + /// 12 + /// The token append methods provided by the [`TokenStreamExt`] extension 13 + /// trait may be useful for implementing `ToTokens`. 14 + /// 15 + /// # Example 16 + /// 17 + /// Example implementation for a struct representing Rust paths like 18 + /// `std::cmp::PartialEq`: 19 + /// 20 + /// ``` 21 + /// use proc_macro2::{TokenTree, Spacing, Span, Punct, TokenStream}; 22 + /// use quote::{TokenStreamExt, ToTokens}; 23 + /// 24 + /// pub struct Path { 25 + /// pub global: bool, 26 + /// pub segments: Vec<PathSegment>, 27 + /// } 28 + /// 29 + /// impl ToTokens for Path { 30 + /// fn to_tokens(&self, tokens: &mut TokenStream) { 31 + /// for (i, segment) in self.segments.iter().enumerate() { 32 + /// if i > 0 || self.global { 33 + /// // Double colon `::` 34 + /// tokens.append(Punct::new(':', Spacing::Joint)); 35 + /// tokens.append(Punct::new(':', Spacing::Alone)); 36 + /// } 37 + /// segment.to_tokens(tokens); 38 + /// } 39 + /// } 40 + /// } 41 + /// # 42 + /// # pub struct PathSegment; 43 + /// # 44 + /// # impl ToTokens for PathSegment { 45 + /// # fn to_tokens(&self, tokens: &mut TokenStream) { 46 + /// # unimplemented!() 47 + /// # } 48 + /// # } 49 + /// ``` 50 + fn to_tokens(&self, tokens: &mut TokenStream); 51 + 52 + /// Convert `self` directly into a `TokenStream` object. 53 + /// 54 + /// This method is implicitly implemented using `to_tokens`, and acts as a 55 + /// convenience method for consumers of the `ToTokens` trait. 56 + fn to_token_stream(&self) -> TokenStream { 57 + let mut tokens = TokenStream::new(); 58 + self.to_tokens(&mut tokens); 59 + tokens 60 + } 61 + 62 + /// Convert `self` directly into a `TokenStream` object. 63 + /// 64 + /// This method is implicitly implemented using `to_tokens`, and acts as a 65 + /// convenience method for consumers of the `ToTokens` trait. 66 + fn into_token_stream(self) -> TokenStream 67 + where 68 + Self: Sized, 69 + { 70 + self.to_token_stream() 71 + } 72 + } 73 + 74 + impl<T: ?Sized + ToTokens> ToTokens for &T { 75 + fn to_tokens(&self, tokens: &mut TokenStream) { 76 + (**self).to_tokens(tokens); 77 + } 78 + } 79 + 80 + impl<T: ?Sized + ToTokens> ToTokens for &mut T { 81 + fn to_tokens(&self, tokens: &mut TokenStream) { 82 + (**self).to_tokens(tokens); 83 + } 84 + } 85 + 86 + impl<'a, T: ?Sized + ToOwned + ToTokens> ToTokens for Cow<'a, T> { 87 + fn to_tokens(&self, tokens: &mut TokenStream) { 88 + (**self).to_tokens(tokens); 89 + } 90 + } 91 + 92 + impl<T: ?Sized + ToTokens> ToTokens for Box<T> { 93 + fn to_tokens(&self, tokens: &mut TokenStream) { 94 + (**self).to_tokens(tokens); 95 + } 96 + } 97 + 98 + impl<T: ?Sized + ToTokens> ToTokens for Rc<T> { 99 + fn to_tokens(&self, tokens: &mut TokenStream) { 100 + (**self).to_tokens(tokens); 101 + } 102 + } 103 + 104 + impl<T: ToTokens> ToTokens for Option<T> { 105 + fn to_tokens(&self, tokens: &mut TokenStream) { 106 + if let Some(t) = self { 107 + t.to_tokens(tokens); 108 + } 109 + } 110 + } 111 + 112 + impl ToTokens for str { 113 + fn to_tokens(&self, tokens: &mut TokenStream) { 114 + tokens.append(Literal::string(self)); 115 + } 116 + } 117 + 118 + impl ToTokens for String { 119 + fn to_tokens(&self, tokens: &mut TokenStream) { 120 + self.as_str().to_tokens(tokens); 121 + } 122 + } 123 + 124 + impl ToTokens for i8 { 125 + fn to_tokens(&self, tokens: &mut TokenStream) { 126 + tokens.append(Literal::i8_suffixed(*self)); 127 + } 128 + } 129 + 130 + impl ToTokens for i16 { 131 + fn to_tokens(&self, tokens: &mut TokenStream) { 132 + tokens.append(Literal::i16_suffixed(*self)); 133 + } 134 + } 135 + 136 + impl ToTokens for i32 { 137 + fn to_tokens(&self, tokens: &mut TokenStream) { 138 + tokens.append(Literal::i32_suffixed(*self)); 139 + } 140 + } 141 + 142 + impl ToTokens for i64 { 143 + fn to_tokens(&self, tokens: &mut TokenStream) { 144 + tokens.append(Literal::i64_suffixed(*self)); 145 + } 146 + } 147 + 148 + impl ToTokens for i128 { 149 + fn to_tokens(&self, tokens: &mut TokenStream) { 150 + tokens.append(Literal::i128_suffixed(*self)); 151 + } 152 + } 153 + 154 + impl ToTokens for isize { 155 + fn to_tokens(&self, tokens: &mut TokenStream) { 156 + tokens.append(Literal::isize_suffixed(*self)); 157 + } 158 + } 159 + 160 + impl ToTokens for u8 { 161 + fn to_tokens(&self, tokens: &mut TokenStream) { 162 + tokens.append(Literal::u8_suffixed(*self)); 163 + } 164 + } 165 + 166 + impl ToTokens for u16 { 167 + fn to_tokens(&self, tokens: &mut TokenStream) { 168 + tokens.append(Literal::u16_suffixed(*self)); 169 + } 170 + } 171 + 172 + impl ToTokens for u32 { 173 + fn to_tokens(&self, tokens: &mut TokenStream) { 174 + tokens.append(Literal::u32_suffixed(*self)); 175 + } 176 + } 177 + 178 + impl ToTokens for u64 { 179 + fn to_tokens(&self, tokens: &mut TokenStream) { 180 + tokens.append(Literal::u64_suffixed(*self)); 181 + } 182 + } 183 + 184 + impl ToTokens for u128 { 185 + fn to_tokens(&self, tokens: &mut TokenStream) { 186 + tokens.append(Literal::u128_suffixed(*self)); 187 + } 188 + } 189 + 190 + impl ToTokens for usize { 191 + fn to_tokens(&self, tokens: &mut TokenStream) { 192 + tokens.append(Literal::usize_suffixed(*self)); 193 + } 194 + } 195 + 196 + impl ToTokens for f32 { 197 + fn to_tokens(&self, tokens: &mut TokenStream) { 198 + tokens.append(Literal::f32_suffixed(*self)); 199 + } 200 + } 201 + 202 + impl ToTokens for f64 { 203 + fn to_tokens(&self, tokens: &mut TokenStream) { 204 + tokens.append(Literal::f64_suffixed(*self)); 205 + } 206 + } 207 + 208 + impl ToTokens for char { 209 + fn to_tokens(&self, tokens: &mut TokenStream) { 210 + tokens.append(Literal::character(*self)); 211 + } 212 + } 213 + 214 + impl ToTokens for bool { 215 + fn to_tokens(&self, tokens: &mut TokenStream) { 216 + let word = if *self { "true" } else { "false" }; 217 + tokens.append(Ident::new(word, Span::call_site())); 218 + } 219 + } 220 + 221 + impl ToTokens for CStr { 222 + fn to_tokens(&self, tokens: &mut TokenStream) { 223 + tokens.append(Literal::c_string(self)); 224 + } 225 + } 226 + 227 + impl ToTokens for CString { 228 + fn to_tokens(&self, tokens: &mut TokenStream) { 229 + tokens.append(Literal::c_string(self)); 230 + } 231 + } 232 + 233 + impl ToTokens for Group { 234 + fn to_tokens(&self, tokens: &mut TokenStream) { 235 + tokens.append(self.clone()); 236 + } 237 + } 238 + 239 + impl ToTokens for Ident { 240 + fn to_tokens(&self, tokens: &mut TokenStream) { 241 + tokens.append(self.clone()); 242 + } 243 + } 244 + 245 + impl ToTokens for Punct { 246 + fn to_tokens(&self, tokens: &mut TokenStream) { 247 + tokens.append(self.clone()); 248 + } 249 + } 250 + 251 + impl ToTokens for Literal { 252 + fn to_tokens(&self, tokens: &mut TokenStream) { 253 + tokens.append(self.clone()); 254 + } 255 + } 256 + 257 + impl ToTokens for TokenTree { 258 + fn to_tokens(&self, tokens: &mut TokenStream) { 259 + tokens.append(self.clone()); 260 + } 261 + } 262 + 263 + impl ToTokens for TokenStream { 264 + fn to_tokens(&self, tokens: &mut TokenStream) { 265 + tokens.extend(iter::once(self.clone())); 266 + } 267 + 268 + fn into_token_stream(self) -> TokenStream { 269 + self 270 + } 271 + }