ToTokens

Trait ToTokens 

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

    // Provided method
    fn into_tokens(self) -> Tokens
       where Self: Sized { ... }
}
Expand description

Types that can be interpolated inside a quote! invocation.

Required Methods§

Source

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

Write self to the given Tokens.

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

extern crate quote;
use quote::{Tokens, ToTokens};

extern crate proc_macro2;
use proc_macro2::{TokenTree, TokenNode, Spacing, Span};

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

impl ToTokens for Path {
    fn to_tokens(&self, tokens: &mut Tokens) {
        for (i, segment) in self.segments.iter().enumerate() {
            if i > 0 || self.global {
                // Double colon `::`
                tokens.append(TokenTree {
                    span: Span::def_site(),
                    kind: TokenNode::Op(':', Spacing::Joint),
                });
                tokens.append(TokenTree {
                    span: Span::def_site(),
                    kind: TokenNode::Op(':', Spacing::Alone),
                });
            }
            segment.to_tokens(tokens);
        }
    }
}

Provided Methods§

Source

fn into_tokens(self) -> Tokens
where Self: Sized,

Convert self directly into a Tokens 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 TokenNode

Source§

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

Source§

impl ToTokens for bool

Source§

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

Source§

impl ToTokens for char

Source§

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

Source§

impl ToTokens for f32

Source§

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

Source§

impl ToTokens for f64

Source§

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

Source§

impl ToTokens for i8

Source§

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

Source§

impl ToTokens for i16

Source§

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

Source§

impl ToTokens for i32

Source§

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

Source§

impl ToTokens for i64

Source§

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

Source§

impl ToTokens for isize

Source§

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

Source§

impl ToTokens for str

Source§

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

Source§

impl ToTokens for u8

Source§

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

Source§

impl ToTokens for u16

Source§

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

Source§

impl ToTokens for u32

Source§

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

Source§

impl ToTokens for u64

Source§

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

Source§

impl ToTokens for usize

Source§

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

Source§

impl ToTokens for String

Source§

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

Source§

impl ToTokens for Literal

Source§

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

Source§

impl ToTokens for Term

Source§

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

Source§

impl ToTokens for TokenStream

Source§

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

Source§

impl ToTokens for TokenTree

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Implementors§