pub trait ToTokens {
// Required method
fn to_tokens(&self, tokens: &mut TokenStream);
// Provided methods
fn to_token_iter(&self) -> TokenIter ⓘ { ... }
fn into_token_iter(self) -> TokenIter ⓘ
where Self: Sized { ... }
fn to_token_stream(&self) -> TokenStream { ... }
fn into_token_stream(self) -> TokenStream
where Self: Sized { ... }
fn tokens_to_string(&self) -> String { ... }
}Expand description
unsynn defines its own ToTokens trait to be able to implement it for std container types.
This is similar to the ToTokens from the quote crate but adds some extra methods and is
implemented for more types. Moreover the to_token_iter() method is the main entry point
for crating an iterator that can be used for parsing.
§Using with the unsynn! macro
The unsynn! macro provides convenient syntax sugar for customizing token emission via the
to_tokens clause. See the macro documentation
for details.
unsynn! {
struct BoolKeyword(bool);
to_tokens |s, tokens| {
let keyword = if s.0 { "true" } else { "false" };
Ident::new(keyword, Span::call_site()).to_tokens(tokens);
};
}Required Methods§
Sourcefn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
Write &self to the given TokenStream.
This is the core method that needs to be implemented. All other methods in this trait have default implementations based on this method.
§Using with the unsynn! macro
The unsynn! macro’s to_tokens clause provides syntax sugar for implementing this
method. See unsynn! documentation.
Provided Methods§
Sourcefn to_token_iter(&self) -> TokenIter ⓘ
fn to_token_iter(&self) -> TokenIter ⓘ
Convert &self into a TokenIter object.
Sourcefn into_token_iter(self) -> TokenIter ⓘwhere
Self: Sized,
fn into_token_iter(self) -> TokenIter ⓘwhere
Self: Sized,
Convert self into a TokenIter object.
Sourcefn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
Convert &self into a TokenStream object.
Sourcefn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
fn into_token_stream(self) -> TokenStreamwhere
Self: Sized,
Convert self into a TokenStream object.
Sourcefn tokens_to_string(&self) -> String
fn tokens_to_string(&self) -> String
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl ToTokens for &String
Available on crate feature proc_macro2 only.
impl ToTokens for &String
proc_macro2 only.fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &i8
Emit a literal i8 with negative sign and without suffix
impl ToTokens for &i8
Emit a literal i8 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &i16
Emit a literal i16 with negative sign and without suffix
impl ToTokens for &i16
Emit a literal i16 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &i32
Emit a literal i32 with negative sign and without suffix
impl ToTokens for &i32
Emit a literal i32 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &i64
Emit a literal i64 with negative sign and without suffix
impl ToTokens for &i64
Emit a literal i64 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &i128
Emit a literal i128 with negative sign and without suffix
impl ToTokens for &i128
Emit a literal i128 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &isize
Emit a literal isize with negative sign and without suffix
impl ToTokens for &isize
Emit a literal isize with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &str
Available on crate feature proc_macro2 only.Tokenizes a &str. Panics if the input string does not tokenize.
impl ToTokens for &str
proc_macro2 only.Tokenizes a &str. Panics if the input string does not tokenize.
§Example
let mut tokens = "foo -> {1,2,3}".to_token_stream();
assert_tokens_eq!(
tokens,
"foo -> { 1 , 2 , 3 }"
);fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &u8
Emit a literal u8 without sign and suffix
impl ToTokens for &u8
Emit a literal u8 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &u16
Emit a literal u16 without sign and suffix
impl ToTokens for &u16
Emit a literal u16 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &u32
Emit a literal u32 without sign and suffix
impl ToTokens for &u32
Emit a literal u32 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &u64
Emit a literal u64 without sign and suffix
impl ToTokens for &u64
Emit a literal u64 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &u128
Emit a literal u128 without sign and suffix
impl ToTokens for &u128
Emit a literal u128 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for &usize
Emit a literal usize without sign and suffix
impl ToTokens for &usize
Emit a literal usize without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for i8
Emit a literal i8 with negative sign and without suffix
impl ToTokens for i8
Emit a literal i8 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for i16
Emit a literal i16 with negative sign and without suffix
impl ToTokens for i16
Emit a literal i16 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for i32
Emit a literal i32 with negative sign and without suffix
impl ToTokens for i32
Emit a literal i32 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for i64
Emit a literal i64 with negative sign and without suffix
impl ToTokens for i64
Emit a literal i64 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for i128
Emit a literal i128 with negative sign and without suffix
impl ToTokens for i128
Emit a literal i128 with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for isize
Emit a literal isize with negative sign and without suffix
impl ToTokens for isize
Emit a literal isize with negative sign and without suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for str
Available on crate feature proc_macro2 only.
impl ToTokens for str
proc_macro2 only.fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for u8
Emit a literal u8 without sign and suffix
impl ToTokens for u8
Emit a literal u8 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for u16
Emit a literal u16 without sign and suffix
impl ToTokens for u16
Emit a literal u16 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for u32
Emit a literal u32 without sign and suffix
impl ToTokens for u32
Emit a literal u32 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for u64
Emit a literal u64 without sign and suffix
impl ToTokens for u64
Emit a literal u64 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for u128
Emit a literal u128 without sign and suffix
impl ToTokens for u128
Emit a literal u128 without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl ToTokens for usize
Emit a literal usize without sign and suffix
impl ToTokens for usize
Emit a literal usize without sign and suffix
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<A: ToTokens, B: ToTokens, C: ToTokens, D: ToTokens> ToTokens for (A, B, C, D)
Emit tokens for a 4-element tuple by emitting each element in sequence.
impl<A: ToTokens, B: ToTokens, C: ToTokens, D: ToTokens> ToTokens for (A, B, C, D)
Emit tokens for a 4-element tuple by emitting each element in sequence.
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<A: ToTokens, B: ToTokens, C: ToTokens> ToTokens for (A, B, C)
Emit tokens for a 3-element tuple by emitting each element in sequence.
impl<A: ToTokens, B: ToTokens, C: ToTokens> ToTokens for (A, B, C)
Emit tokens for a 3-element tuple by emitting each element in sequence.
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<A: ToTokens, B: ToTokens> ToTokens for (A, B)
Emit tokens for a 2-element tuple by emitting each element in sequence.
impl<A: ToTokens, B: ToTokens> ToTokens for (A, B)
Emit tokens for a 2-element tuple by emitting each element in sequence.
§Example
let mut tokens = TokenStream::new();
(42u8, true).to_tokens(&mut tokens);
assert_eq!(tokens.to_string(), "42 true");fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T: ToTokens> ToTokens for RefCell<T>
impl<T: ToTokens> ToTokens for RefCell<T>
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T: ToTokens> ToTokens for [T]
ToTokens for arrays and slices
impl<T: ToTokens> ToTokens for [T]
ToTokens for arrays and slices
§Example
use unsynn::*;
let arr: [Ident; 3] = [
Ident::new("a", Span::call_site()),
Ident::new("b", Span::call_site()),
Ident::new("c", Span::call_site())
];
let mut tokens = TokenStream::new();
arr.to_tokens(&mut tokens);
assert_eq!(tokens.to_string(), "a b c");fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T> ToTokens for PhantomData<T>
impl<T> ToTokens for PhantomData<T>
fn to_tokens(&self, _tokens: &mut TokenStream)
Implementors§
impl ToTokens for BraceGroup
impl ToTokens for BracketGroup
impl ToTokens for Disable
impl ToTokens for Enable
impl ToTokens for EndOfStream
impl ToTokens for Group
impl ToTokens for Ident
impl ToTokens for Invalid
impl ToTokens for Literal
impl ToTokens for LiteralCharacter
impl ToTokens for LiteralInteger
impl ToTokens for LiteralString
impl ToTokens for NonEmptyTokenStream
impl ToTokens for NonParseable
nonparseable only.impl ToTokens for NoneGroup
impl ToTokens for Nothing
impl ToTokens for ParenthesisGroup
impl ToTokens for Punct
impl ToTokens for TokenIter
impl ToTokens for TokenStream
impl ToTokens for TokenTree
impl ToTokens for TokensRemain
impl<A, B, C, D> ToTokens for Either<A, B, C, D>
impl<A: PredicateOp, B: PredicateOp, C: PredicateOp, D: PredicateOp> ToTokens for AllOf<A, B, C, D>
impl<A: PredicateOp, B: PredicateOp, C: PredicateOp, D: PredicateOp> ToTokens for AnyOf<A, B, C, D>
impl<A: PredicateOp, B: PredicateOp, C: PredicateOp, D: PredicateOp> ToTokens for OneOf<A, B, C, D>
impl<A: PredicateOp, B: PredicateOp, Same: PredicateOp, Different: PredicateOp> ToTokens for PredicateCmp<A, B, Same, Different>
impl<A: ToTokens, B: ToTokens, C: ToTokens, D: ToTokens> ToTokens for Cons<A, B, C, D>
impl<A: ToTokens, B: ToTokens> ToTokens for Swap<A, B>
impl<C: ToTokens> ToTokens for BraceGroupContaining<C>
impl<C: ToTokens> ToTokens for BracketGroupContaining<C>
impl<C: ToTokens> ToTokens for GroupContaining<C>
impl<C: ToTokens> ToTokens for NoneGroupContaining<C>
impl<C: ToTokens> ToTokens for ParenthesisGroupContaining<C>
impl<Operand, Operator, const MAX: usize> ToTokens for LeftAssocExpr<Operand, Operator, MAX>
impl<Operand, Operator, const MAX: usize> ToTokens for PostfixExpr<Operand, Operator, MAX>
impl<Operand, Operator, const MAX: usize> ToTokens for RightAssocExpr<Operand, Operator, MAX>
impl<Operator, Operand, const MAX: usize> ToTokens for PrefixExpr<Operator, Operand, MAX>
impl<T: Default> ToTokens for HiddenState<T>
impl<T: Parse + ToTokens> ToTokens for Cached<T>
impl<T: PredicateOp> ToTokens for Not<T>
impl<T: ToTokens, D: ToTokens, P, const MIN: usize, const MAX: usize> ToTokens for DelimitedVec<T, D, P, MIN, MAX>
impl<T: ToTokens, D: ToTokens> ToTokens for Delimited<T, D>
impl<T: ToTokens, S: ToTokens> ToTokens for LazyVec<T, S>
impl<T: ToTokens, S: ToTokens> ToTokens for LazyVecUntil<T, S>
impl<T: ToTokens> ToTokens for Insert<T>
impl<T: ToTokens> ToTokens for IntoIdent<T>
proc_macro2 only.