ToTokens in proc_macro - Rust (original) (raw)
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.
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
Write self
to the given TokenStream
.
§Example
Example implementation for a struct representing Rust paths likestd::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);
}
}
}
🔬This is a nightly-only experimental API. (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.
🔬This is a nightly-only experimental API. (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.
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)
🔬This is a nightly-only experimental API. (proc_macro_totokens
#130977)