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§
Implementations on Foreign Types§
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 &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.
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, B> ToTokens for (A, B)
Emit tokens for a 2-element tuple by emitting each element in sequence.
impl<A, B> 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<A, B, C> ToTokens for (A, B, C)
Emit tokens for a 3-element tuple by emitting each element in sequence.
impl<A, B, C> 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, B, C, D> ToTokens for (A, B, C, D)
Emit tokens for a 4-element tuple by emitting each element in sequence.
impl<A, B, C, D> 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<T> ToTokens for Option<T>where
T: ToTokens,
impl<T> ToTokens for Option<T>where
T: ToTokens,
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T> ToTokens for [T]where
T: ToTokens,
ToTokens for arrays and slices
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");fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T> ToTokens for Box<T>where
T: ToTokens,
impl<T> ToTokens for Box<T>where
T: ToTokens,
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T> ToTokens for Rc<T>where
T: ToTokens,
impl<T> ToTokens for Rc<T>where
T: ToTokens,
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T> ToTokens for Vec<T>where
T: ToTokens,
impl<T> ToTokens for Vec<T>where
T: ToTokens,
fn to_tokens(&self, tokens: &mut TokenStream)
Source§impl<T> ToTokens for RefCell<T>where
T: ToTokens,
impl<T> ToTokens for RefCell<T>where
T: ToTokens,
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 AdtDecl
impl ToTokens for AttributeInner
impl ToTokens for ConstOrMut
impl ToTokens for EnumVariantData
impl ToTokens for Expr
impl ToTokens for FacetInner
impl ToTokens for GenericParam
impl ToTokens for LifetimeOrTt
impl ToTokens for StructKind
impl ToTokens for TokenTree
impl ToTokens for Vis
impl ToTokens for AngleTokenTree
impl ToTokens for Attribute
impl ToTokens for BraceGroup
impl ToTokens for BracketGroup
impl ToTokens for ChildInner
impl ToTokens for DefaultEqualsInner
impl ToTokens for DeserializeWithInner
impl ToTokens for Disable
impl ToTokens for DocInner
impl ToTokens for Enable
impl ToTokens for EndOfStream
impl ToTokens for Enum
impl ToTokens for EnumVariantLike
impl ToTokens for FacetAttr
impl ToTokens for FlattenInner
impl ToTokens for GenericParams
impl ToTokens for Group
impl ToTokens for Ident
impl ToTokens for Invalid
impl ToTokens for InvariantInner
impl ToTokens for KChild
impl ToTokens for KConst
impl ToTokens for KCrate
impl ToTokens for KDefault
impl ToTokens for KDenyUnknownFields
impl ToTokens for KDeserializeWith
impl ToTokens for KDoc
impl ToTokens for KEnum
impl ToTokens for KFacet
impl ToTokens for KFlatten
impl ToTokens for KIn
impl ToTokens for KInvariants
impl ToTokens for KMut
impl ToTokens for KOpaque
impl ToTokens for KPub
impl ToTokens for KRename
impl ToTokens for KRenameAll
impl ToTokens for KRepr
impl ToTokens for KSensitive
impl ToTokens for KSerializeWith
impl ToTokens for KSkipSerializing
impl ToTokens for KSkipSerializingIf
impl ToTokens for KStruct
impl ToTokens for KTransparent
impl ToTokens for KTypeTag
impl ToTokens for KWhere
impl ToTokens for Lifetime
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 RenameAllInner
impl ToTokens for RenameInner
impl ToTokens for ReprInner
impl ToTokens for SerializeWithInner
impl ToTokens for SkipSerializingIfInner
impl ToTokens for SkipSerializingInner
impl ToTokens for Struct
impl ToTokens for StructEnumVariant
impl ToTokens for StructField
impl ToTokens for TokenIter
impl ToTokens for TokenStream
impl ToTokens for TokensRemain
impl ToTokens for TupleField
impl ToTokens for TupleVariant
impl ToTokens for TypeTagInner
impl ToTokens for UnitVariant
impl ToTokens for WhereClause
impl ToTokens for WhereClauses
impl<A, B> ToTokens for Swap<A, B>
impl<A, B, C, D> ToTokens for Either<A, B, C, D>
impl<A, B, C, D> ToTokens for AllOf<A, B, C, D>
impl<A, B, C, D> ToTokens for AnyOf<A, B, C, D>
impl<A, B, C, D> ToTokens for Cons<A, B, C, D>
impl<A, B, C, D> ToTokens for OneOf<A, B, C, D>
impl<A, B, Same, Different> ToTokens for PredicateCmp<A, B, Same, Different>
impl<C> ToTokens for BraceGroupContaining<C>where
C: ToTokens,
impl<C> ToTokens for BracketGroupContaining<C>where
C: ToTokens,
impl<C> ToTokens for GroupContaining<C>where
C: ToTokens,
impl<C> ToTokens for NoneGroupContaining<C>where
C: ToTokens,
impl<C> ToTokens for ParenthesisGroupContaining<C>where
C: ToTokens,
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> ToTokens for Cached<T>
impl<T> ToTokens for Discard<T>
impl<T> ToTokens for DynNode<T>
impl<T> ToTokens for Except<T>
impl<T> ToTokens for Expect<T>
impl<T> ToTokens for HiddenState<T>where
T: Default,
impl<T> ToTokens for Insert<T>where
T: ToTokens,
impl<T> ToTokens for IntoIdent<T>where
T: ToTokens,
proc_macro2 only.