1 use super::TokenStreamExt
;
6 use proc_macro2
::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree}
;
8 /// Types that can be interpolated inside a [`quote!`] invocation.
10 /// [`quote!`]: macro.quote.html
12 /// Write `self` to the given `TokenStream`.
14 /// The token append methods provided by the [`TokenStreamExt`] extension
15 /// trait may be useful for implementing `ToTokens`.
17 /// [`TokenStreamExt`]: trait.TokenStreamExt.html
21 /// Example implementation for a struct representing Rust paths like
22 /// `std::cmp::PartialEq`:
25 /// use proc_macro2::{TokenTree, Spacing, Span, Punct, TokenStream};
26 /// use quote::{TokenStreamExt, ToTokens};
30 /// pub segments: Vec<PathSegment>,
33 /// impl ToTokens for Path {
34 /// fn to_tokens(&self, tokens: &mut TokenStream) {
35 /// for (i, segment) in self.segments.iter().enumerate() {
36 /// if i > 0 || self.global {
37 /// // Double colon `::`
38 /// tokens.append(Punct::new(':', Spacing::Joint));
39 /// tokens.append(Punct::new(':', Spacing::Alone));
41 /// segment.to_tokens(tokens);
46 /// # pub struct PathSegment;
48 /// # impl ToTokens for PathSegment {
49 /// # fn to_tokens(&self, tokens: &mut TokenStream) {
50 /// # unimplemented!()
54 fn to_tokens(&self, tokens
: &mut TokenStream
);
56 /// Convert `self` directly into a `TokenStream` object.
58 /// This method is implicitly implemented using `to_tokens`, and acts as a
59 /// convenience method for consumers of the `ToTokens` trait.
60 fn into_token_stream(self) -> TokenStream
64 let mut tokens
= TokenStream
::new();
65 self.to_tokens(&mut tokens
);
70 impl<'a
, T
: ?Sized
+ ToTokens
> ToTokens
for &'a T
{
71 fn to_tokens(&self, tokens
: &mut TokenStream
) {
72 (**self).to_tokens(tokens
);
76 impl<'a
, T
: ?Sized
+ ToTokens
> ToTokens
for &'a
mut T
{
77 fn to_tokens(&self, tokens
: &mut TokenStream
) {
78 (**self).to_tokens(tokens
);
82 impl<'a
, T
: ?Sized
+ ToOwned
+ ToTokens
> ToTokens
for Cow
<'a
, T
> {
83 fn to_tokens(&self, tokens
: &mut TokenStream
) {
84 (**self).to_tokens(tokens
);
88 impl<T
: ?Sized
+ ToTokens
> ToTokens
for Box
<T
> {
89 fn to_tokens(&self, tokens
: &mut TokenStream
) {
90 (**self).to_tokens(tokens
);
94 impl<T
: ToTokens
> ToTokens
for Option
<T
> {
95 fn to_tokens(&self, tokens
: &mut TokenStream
) {
96 if let Some(ref t
) = *self {
102 impl ToTokens
for str {
103 fn to_tokens(&self, tokens
: &mut TokenStream
) {
104 tokens
.append(Literal
::string(self));
108 impl ToTokens
for String
{
109 fn to_tokens(&self, tokens
: &mut TokenStream
) {
110 self.as_str().to_tokens(tokens
);
114 macro_rules
! primitive
{
115 ($
($t
:ident
=> $name
:ident
)*) => ($
(
116 impl ToTokens
for $t
{
117 fn to_tokens(&self, tokens
: &mut TokenStream
) {
118 tokens
.append(Literal
::$
name(*self));
129 isize => isize_suffixed
135 usize => usize_suffixed
143 i128
=> i128_suffixed
144 u128
=> u128_suffixed
147 impl ToTokens
for char {
148 fn to_tokens(&self, tokens
: &mut TokenStream
) {
149 tokens
.append(Literal
::character(*self));
153 impl ToTokens
for bool
{
154 fn to_tokens(&self, tokens
: &mut TokenStream
) {
155 let word
= if *self { "true" }
else { "false" }
;
156 tokens
.append(Ident
::new(word
, Span
::call_site()));
160 impl ToTokens
for Group
{
161 fn to_tokens(&self, tokens
: &mut TokenStream
) {
162 tokens
.append(self.clone());
166 impl ToTokens
for Ident
{
167 fn to_tokens(&self, tokens
: &mut TokenStream
) {
168 tokens
.append(self.clone());
172 impl ToTokens
for Punct
{
173 fn to_tokens(&self, tokens
: &mut TokenStream
) {
174 tokens
.append(self.clone());
178 impl ToTokens
for Literal
{
179 fn to_tokens(&self, tokens
: &mut TokenStream
) {
180 tokens
.append(self.clone());
184 impl ToTokens
for TokenTree
{
185 fn to_tokens(&self, dst
: &mut TokenStream
) {
186 dst
.append(self.clone());
190 impl ToTokens
for TokenStream
{
191 fn to_tokens(&self, dst
: &mut TokenStream
) {
192 dst
.extend(iter
::once(self.clone()));
195 fn into_token_stream(self) -> TokenStream
{