Trait quote::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 { ... }
}
Expand description

Types that can be interpolated inside a quote! invocation.

Required Methods§

source

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

Write self to the given TokenStream.

The token append methods provided by the TokenStreamExt extension trait may be useful for implementing ToTokens.

Example

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

use proc_macro2::{TokenTree, Spacing, Span, Punct, TokenStream};
use quote::{TokenStreamExt, 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.append(Punct::new(':', Spacing::Joint));
                tokens.append(Punct::new(':', Spacing::Alone));
            }
            segment.to_tokens(tokens);
        }
    }
}

Provided Methods§

source

fn to_token_stream(&self) -> TokenStream

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) -> TokenStreamwhere Self: Sized,

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 Punct

source§

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

source§

impl ToTokens for u64

source§

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

source§

impl ToTokens for Group

source§

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

source§

impl ToTokens for i8

source§

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

source§

impl<'a, T: ?Sized + ToTokens> ToTokens for &'a T

source§

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

source§

impl ToTokens for u8

source§

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

source§

impl ToTokens for f32

source§

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

source§

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

source§

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

source§

impl ToTokens for isize

source§

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

source§

impl ToTokens for Ident

source§

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

source§

impl ToTokens for TokenTree

source§

fn to_tokens(&self, dst: &mut TokenStream)

source§

impl ToTokens for i64

source§

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

source§

impl ToTokens for char

source§

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

source§

impl ToTokens for u128

source§

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

source§

impl ToTokens for str

source§

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

source§

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

source§

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

source§

impl ToTokens for Literal

source§

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

source§

impl ToTokens for u32

source§

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

source§

impl ToTokens for f64

source§

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

source§

impl<'a, T: ?Sized + ToTokens> ToTokens for &'a mut T

source§

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

source§

impl ToTokens for bool

source§

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

source§

impl ToTokens for usize

source§

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

source§

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

source§

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

source§

impl ToTokens for i32

source§

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

source§

impl ToTokens for String

source§

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

source§

impl ToTokens for i16

source§

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

source§

impl ToTokens for TokenStream

source§

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

source§

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

source§

impl ToTokens for u16

source§

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

source§

impl ToTokens for i128

source§

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

Implementors§