[][src]Trait proc_quote::ToTokens

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

    default fn into_token_stream(self) -> TokenStream { ... }
}

Types that can be interpolated inside a quote! invocation.

Required methods

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:

This code runs with edition 2018
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);
        }
    }
}
Loading content...

Provided methods

default fn into_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.

Loading content...

Implementations on Foreign Types

impl ToTokens for char[src]

default fn into_token_stream(self) -> TokenStream[src]

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

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for f64[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for u32[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for u8[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for Literal[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for i32[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for bool[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for u64[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for usize[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for TokenStream[src]

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

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for f32[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for str[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for i8[src]

default fn into_token_stream(self) -> TokenStream[src]

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

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for Ident[src]

default fn into_token_stream(self) -> TokenStream[src]

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

default fn into_token_stream(self) -> TokenStream[src]

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

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for Group[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for i64[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for TokenTree[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for u16[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for String[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for isize[src]

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for Punct[src]

default fn into_token_stream(self) -> TokenStream[src]

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

default fn into_token_stream(self) -> TokenStream[src]

impl ToTokens for i16[src]

default fn into_token_stream(self) -> TokenStream[src]

Loading content...

Implementors

Loading content...