ToTokens in proc_macro - Rust
Description: Types that can be interpolated inside a `quote!` invocation.

## ToTokens

# Trait ToTokens

Source

    pub trait ToTokens {
        // Required method
        fn to_tokens(&self, tokens: &mut TokenStream);

        // Provided methods
        fn to_token_stream(&self) -> TokenStream { ... }
        fn into_token_stream(self) -> TokenStream
           where Self: Sized { ... }
    }

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Expand description

Types that can be interpolated inside a `quote!` invocation.

## Required Methods§

Source

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)

Write `self` to the given `TokenStream`.

##### §Example

Example implementation for a struct representing Rust paths like `std::cmp::PartialEq`:

    #![feature(proc_macro_totokens)]

    use std::iter;
    use proc_macro::{Spacing, Punct, TokenStream, TokenTree, ToTokens};

    pub struct Path {
        pub global: bool,
        pub segments: Vec<PathSegment>,
    }

    impl ToTokens for Path {
        fn to_tokens(&self, tokens: &mut TokenStream) {
            for (i, segment) in self.segments.iter().enumerate() {
                if i > 0 || self.global {
                    // Double colon `::`
                    tokens.extend(iter::once(TokenTree::from(Punct::new(':', Spacing::Joint))));
                    tokens.extend(iter::once(TokenTree::from(Punct::new(':', Spacing::Alone))));
                }
                segment.to_tokens(tokens);
            }
        }
    }

## Provided Methods§

Source

#### fn to_token_stream(&self) -> TokenStream

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)

Convert `self` directly into a `TokenStream` object.

This method is implicitly implemented using `to_tokens`, and acts as a convenience method for consumers of the `ToTokens` trait.

Source

#### fn into_token_stream(self) -> TokenStream
where Self: Sized,

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)

Convert `self` directly into a `TokenStream` object.

This method is implicitly implemented using `to_tokens`, and acts as a convenience method for consumers of the `ToTokens` trait.

## Implementations on Foreign Types§

Source§

### impl ToTokens for bool

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for char

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for f32

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for f64

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for i8

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for i16

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for i32

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for i64

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for i128

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for isize

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for str

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for u8

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for u16

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for u32

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for u64

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for u128

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for usize

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for CString

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for String

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl ToTokens for CStr

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl<T: ToTokens + ToOwned + ?Sized> ToTokens for Cow<'_, T>

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl<T: ToTokens + ?Sized> ToTokens for &T

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl<T: ToTokens + ?Sized> ToTokens for &mut T

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl<T: ToTokens + ?Sized> ToTokens for Box<T>

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl<T: ToTokens + ?Sized> ToTokens for Rc<T>

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)
Source§

### impl<T: ToTokens> ToTokens for Option<T>

Source§

#### fn to_tokens(&self, tokens: &mut TokenStream)

🔬This is a nightly-only experimental API. (`proc_macro_totokens` #130977)

## Implementors§

Source§

### impl ToTokens for TokenTree

Source§

### impl ToTokens for Group

Source§

### impl ToTokens for Ident

Source§

### impl ToTokens for Literal

Source§

### impl ToTokens for Punct

Source§

### impl ToTokens for TokenStream