Skip to main content

ToTokens

Trait ToTokens 

Source
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§

Source

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§

Source

fn to_token_iter(&self) -> TokenIter

Convert &self into a TokenIter object.

Source

fn into_token_iter(self) -> TokenIter
where Self: Sized,

Convert self into a TokenIter object.

Source

fn to_token_stream(&self) -> TokenStream

Convert &self into a TokenStream object.

Source

fn into_token_stream(self) -> TokenStream
where Self: Sized,

Convert self into a TokenStream object.

Source

fn tokens_to_string(&self) -> String

Convert &self into a String object. This is mostly used in the test suite to compare the outputs. When the input is a &str then this parses it and returns a normalized String.

Trait Implementations§

Source§

impl Display for dyn ToTokens

implement Display using ToTokens::tokens_to_string() for all types that implement ToTokens

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Implementations on Foreign Types§

Source§

impl ToTokens for &i8

Emit a literal i8 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &i16

Emit a literal i16 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &i32

Emit a literal i32 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &i64

Emit a literal i64 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &i128

Emit a literal i128 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &isize

Emit a literal isize with negative sign and without suffix

Source§

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.

§Example

let mut tokens = "foo -> {1,2,3}".to_token_stream();

assert_tokens_eq!(
    tokens,
    "foo -> { 1 , 2 , 3 }"
);
Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &u8

Emit a literal u8 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &u16

Emit a literal u16 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &u32

Emit a literal u32 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &u64

Emit a literal u64 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &u128

Emit a literal u128 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &usize

Emit a literal usize without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for &String

Available on crate feature proc_macro2 only.
Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for TokenTree

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for bool

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for char

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for i8

Emit a literal i8 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for i16

Emit a literal i16 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for i32

Emit a literal i32 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for i64

Emit a literal i64 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for i128

Emit a literal i128 with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for isize

Emit a literal isize with negative sign and without suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for str

Available on crate feature proc_macro2 only.
Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for u8

Emit a literal u8 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for u16

Emit a literal u16 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for u32

Emit a literal u32 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for u64

Emit a literal u64 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for u128

Emit a literal u128 without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for usize

Emit a literal usize without sign and suffix

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for Group

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for Ident

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for Literal

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for Punct

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl ToTokens for TokenStream

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<A, B> ToTokens for (A, B)
where A: ToTokens, B: ToTokens,

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");
Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<A, B, C> ToTokens for (A, B, C)
where A: ToTokens, B: ToTokens, C: ToTokens,

Emit tokens for a 3-element tuple by emitting each element in sequence.

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<A, B, C, D> ToTokens for (A, B, C, D)
where A: ToTokens, B: ToTokens, C: ToTokens, D: ToTokens,

Emit tokens for a 4-element tuple by emitting each element in sequence.

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<T> ToTokens for Option<T>
where T: ToTokens,

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<T> ToTokens for [T]
where T: ToTokens,

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");
Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<T> ToTokens for Box<T>
where T: ToTokens,

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<T> ToTokens for Rc<T>
where T: ToTokens,

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<T> ToTokens for Vec<T>
where T: ToTokens,

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<T> ToTokens for RefCell<T>
where T: ToTokens,

Source§

fn to_tokens(&self, tokens: &mut TokenStream)

Source§

impl<T> ToTokens for PhantomData<T>

Source§

fn to_tokens(&self, _tokens: &mut TokenStream)

Implementors§

Source§

impl ToTokens for GenericArgument

Source§

impl ToTokens for Type

Source§

impl ToTokens for Visibility

Source§

impl ToTokens for EndOfStream

Source§

impl ToTokens for Invalid

Source§

impl ToTokens for NonEmptyTokenStream

Source§

impl ToTokens for NonParseable

Available on crate feature nonparseable only.
Source§

impl ToTokens for Nothing

Source§

impl ToTokens for BraceGroup

Source§

impl ToTokens for BracketGroup

Source§

impl ToTokens for NoneGroup

Source§

impl ToTokens for ParenthesisGroup

Source§

impl ToTokens for LiteralCharacter

Source§

impl ToTokens for LiteralInteger

Source§

impl ToTokens for LiteralString

Source§

impl ToTokens for Disable

Source§

impl ToTokens for Enable

Source§

impl ToTokens for TokensRemain

Source§

impl ToTokens for TokenIter

Source§

impl ToTokens for AngleTokenTree

Source§

impl ToTokens for DocAttribute

Source§

impl ToTokens for GenericParams

Source§

impl ToTokens for KAsync

Source§

impl ToTokens for KDoc

Source§

impl ToTokens for KFn

Source§

impl ToTokens for KMut

Source§

impl ToTokens for KPub

Source§

impl ToTokens for KSelfKw

Source§

impl ToTokens for KTrait

Source§

impl ToTokens for KWhere

Source§

impl ToTokens for Lifetime

Source§

impl ToTokens for MethodParam

Source§

impl ToTokens for MethodParams

Source§

impl ToTokens for PathWithGenerics

Source§

impl ToTokens for RawAttribute

Source§

impl ToTokens for RefSelf

Source§

impl ToTokens for ReturnType

Source§

impl ToTokens for ServiceMethod

Source§

impl ToTokens for ServiceTrait

Source§

impl ToTokens for TypePath

Source§

impl ToTokens for TypeRef

Source§

impl ToTokens for TypeTuple

Source§

impl ToTokens for WhereClause

Source§

impl<A, B> ToTokens for Swap<A, B>
where A: ToTokens, B: ToTokens,

Source§

impl<A, B, C, D> ToTokens for Either<A, B, C, D>
where A: ToTokens, B: ToTokens, C: ToTokens, D: ToTokens,

Source§

impl<A, B, C, D> ToTokens for Cons<A, B, C, D>
where A: ToTokens, B: ToTokens, C: ToTokens, D: ToTokens,

Source§

impl<A, B, C, D> ToTokens for AllOf<A, B, C, D>

Source§

impl<A, B, C, D> ToTokens for AnyOf<A, B, C, D>

Source§

impl<A, B, C, D> ToTokens for OneOf<A, B, C, D>

Source§

impl<A, B, Same, Different> ToTokens for PredicateCmp<A, B, Same, Different>
where A: PredicateOp, B: PredicateOp, Same: PredicateOp, Different: PredicateOp,

Source§

impl<C> ToTokens for BraceGroupContaining<C>
where C: ToTokens,

Source§

impl<C> ToTokens for BracketGroupContaining<C>
where C: ToTokens,

Source§

impl<C> ToTokens for GroupContaining<C>
where C: ToTokens,

Source§

impl<C> ToTokens for NoneGroupContaining<C>
where C: ToTokens,

Source§

impl<C> ToTokens for ParenthesisGroupContaining<C>
where C: ToTokens,

Source§

impl<Operand, Operator, const MAX: usize> ToTokens for LeftAssocExpr<Operand, Operator, MAX>
where Operand: ToTokens, Operator: ToTokens,

Source§

impl<Operand, Operator, const MAX: usize> ToTokens for PostfixExpr<Operand, Operator, MAX>
where Operand: ToTokens, Operator: ToTokens,

Source§

impl<Operand, Operator, const MAX: usize> ToTokens for RightAssocExpr<Operand, Operator, MAX>
where Operand: ToTokens, Operator: ToTokens,

Source§

impl<Operator, Operand, const MAX: usize> ToTokens for PrefixExpr<Operator, Operand, MAX>
where Operator: ToTokens, Operand: ToTokens,

Source§

impl<T> ToTokens for NonEmptyOption<T>
where T: ToTokens,

Source§

impl<T> ToTokens for DynNode<T>

Source§

impl<T> ToTokens for Cached<T>
where T: Parse + ToTokens,

Source§

impl<T> ToTokens for Except<T>

Source§

impl<T> ToTokens for Expect<T>

Source§

impl<T> ToTokens for HiddenState<T>
where T: Default,

Source§

impl<T> ToTokens for Not<T>
where T: PredicateOp,

Source§

impl<T> ToTokens for Discard<T>

Source§

impl<T> ToTokens for Insert<T>
where T: ToTokens,

Source§

impl<T> ToTokens for IntoIdent<T>
where T: ToTokens,

Available on crate feature proc_macro2 only.
Source§

impl<T> ToTokens for IntoLiteralString<T>

Source§

impl<T> ToTokens for IntoTokenStream<T>
where T: ToTokens,

Source§

impl<T> ToTokens for Skip<T>

Source§

impl<T, D> ToTokens for Delimited<T, D>
where T: ToTokens, D: ToTokens,

Source§

impl<T, D, P, const MIN: usize, const MAX: usize> ToTokens for DelimitedVec<T, D, P, MIN, MAX>
where T: ToTokens, D: ToTokens,

Source§

impl<T, S> ToTokens for LazyVec<T, S>
where T: ToTokens, S: ToTokens,

Source§

impl<T, S> ToTokens for LazyVecUntil<T, S>
where T: ToTokens, S: ToTokens,

Source§

impl<T, const N: usize> ToTokens for StderrLog<T, N>

Source§

impl<const C1: char, const C2: char, const C3: char, const C4: char> ToTokens for Operator<C1, C2, C3, C4>

Source§

impl<const C: char> ToTokens for PunctAlone<C>

Source§

impl<const C: char> ToTokens for PunctAny<C>

Source§

impl<const C: char> ToTokens for PunctJoint<C>

Source§

impl<const V: char> ToTokens for ConstCharacter<V>

Source§

impl<const V: u128> ToTokens for ConstInteger<V>