Trait quote::ToTokens [] [src]

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

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

Types that can be interpolated inside a quote! invocation.

Required Methods

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, Spacing, Span, Op};

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(Op::new(':', Spacing::Joint));
                tokens.append(Op::new(':', Spacing::Alone));
            }
            segment.to_tokens(tokens);
        }
    }
}

Provided Methods

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

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

[src]

[src]

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

[src]

[src]

impl<T: ?Sized + ToTokens> ToTokens for Box<T>
[src]

[src]

[src]

impl<T: ToTokens> ToTokens for Option<T>
[src]

[src]

[src]

impl ToTokens for str
[src]

[src]

[src]

impl ToTokens for String
[src]

[src]

[src]

impl ToTokens for i8
[src]

[src]

[src]

impl ToTokens for i16
[src]

[src]

[src]

impl ToTokens for i32
[src]

[src]

[src]

impl ToTokens for i64
[src]

[src]

[src]

impl ToTokens for isize
[src]

[src]

[src]

impl ToTokens for u8
[src]

[src]

[src]

impl ToTokens for u16
[src]

[src]

[src]

impl ToTokens for u32
[src]

[src]

[src]

impl ToTokens for u64
[src]

[src]

[src]

impl ToTokens for usize
[src]

[src]

[src]

impl ToTokens for f32
[src]

[src]

[src]

impl ToTokens for f64
[src]

[src]

[src]

impl ToTokens for char
[src]

[src]

[src]

impl ToTokens for bool
[src]

[src]

[src]

impl ToTokens for Group
[src]

[src]

[src]

impl ToTokens for Term
[src]

[src]

[src]

impl ToTokens for Op
[src]

[src]

[src]

impl ToTokens for Literal
[src]

[src]

[src]

impl ToTokens for TokenTree
[src]

[src]

[src]

impl ToTokens for TokenStream
[src]

[src]

[src]

Implementors