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
)Expand description
Types that can be interpolated inside a quote!
invocation.
Required Methods§
Sourcefn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (proc_macro_totokens
)
fn to_tokens(&self, tokens: &mut TokenStream)
proc_macro_totokens
)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§
Sourcefn to_token_stream(&self) -> TokenStream
🔬This is a nightly-only experimental API. (proc_macro_totokens
)
fn to_token_stream(&self) -> TokenStream
proc_macro_totokens
)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.
Sourcefn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
🔬This is a nightly-only experimental API. (proc_macro_totokens
)
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
proc_macro_totokens
)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
impl ToTokens for bool
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for char
impl ToTokens for char
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for f32
impl ToTokens for f32
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for f64
impl ToTokens for f64
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for i8
impl ToTokens for i8
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for i16
impl ToTokens for i16
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for i32
impl ToTokens for i32
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for i64
impl ToTokens for i64
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for i128
impl ToTokens for i128
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for isize
impl ToTokens for isize
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for str
impl ToTokens for str
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for u8
impl ToTokens for u8
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for u16
impl ToTokens for u16
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for u32
impl ToTokens for u32
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for u64
impl ToTokens for u64
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for u128
impl ToTokens for u128
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for usize
impl ToTokens for usize
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for CString
impl ToTokens for CString
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for String
impl ToTokens for String
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl ToTokens for CStr
impl ToTokens for CStr
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl<T> ToTokens for Cow<'_, T>
impl<T> ToTokens for Cow<'_, T>
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl<T> ToTokens for Option<T>where
T: ToTokens,
impl<T> ToTokens for Option<T>where
T: ToTokens,
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl<T> ToTokens for &T
impl<T> ToTokens for &T
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl<T> ToTokens for &mut T
impl<T> ToTokens for &mut T
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl<T> ToTokens for Box<T>
impl<T> ToTokens for Box<T>
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)Source§impl<T> ToTokens for Rc<T>
impl<T> ToTokens for Rc<T>
Source§fn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
🔬This is a nightly-only experimental API. (
proc_macro_totokens
)