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
#130977)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
#130977)
fn to_tokens(&self, tokens: &mut TokenStream)
proc_macro_totokens
#130977)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
#130977)
fn to_token_stream(&self) -> TokenStream
proc_macro_totokens
#130977)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
#130977)
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
proc_macro_totokens
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)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
#130977)Source§impl<T: ToTokens + ToOwned + ?Sized> ToTokens for Cow<'_, T>
impl<T: ToTokens + ToOwned + ?Sized> 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
#130977)Source§impl<T: ToTokens + ?Sized> ToTokens for &T
impl<T: ToTokens + ?Sized> 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
#130977)Source§impl<T: ToTokens + ?Sized> ToTokens for &mut T
impl<T: ToTokens + ?Sized> 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
#130977)Source§impl<T: ToTokens + ?Sized> ToTokens for Box<T>
impl<T: ToTokens + ?Sized> 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
#130977)Source§impl<T: ToTokens + ?Sized> ToTokens for Rc<T>
impl<T: ToTokens + ?Sized> 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
#130977)Source§impl<T: ToTokens> ToTokens for Option<T>
impl<T: ToTokens> ToTokens for Option<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
#130977)