#![allow(missing_docs)]
use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use quote::{ToTokens, TokenStreamExt as _};
pub use crate::Punctuated;
#[non_exhaustive]
#[derive(Clone, Debug)]
pub enum Item {
Struct(Struct),
Enum(Enum),
Union(Union),
Module(Module),
Trait(Trait),
Impl(Impl),
TypeAlias(TypeAlias),
Function(Function),
Constant(Constant),
UseDeclaration(UseDeclaration),
Macro(Macro),
ExternBlock(ExternBlock),
ExternCrate(ExternCrate),
}
#[derive(Clone, Debug)]
pub struct Struct {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_struct: Ident,
pub name: Ident,
pub generic_params: Option<GenericParamList>,
pub where_clause: Option<WhereClause>,
pub fields: Fields,
pub tk_semicolon: Option<Punct>,
}
#[derive(Clone, Debug)]
pub enum Fields {
Unit,
Tuple(TupleFields),
Named(NamedFields),
}
#[derive(Clone)]
pub struct TupleFields {
pub fields: Punctuated<TupleField>,
pub tk_parens: GroupSpan,
}
#[derive(Clone)]
pub struct NamedFields {
pub fields: Punctuated<NamedField>,
pub tk_braces: GroupSpan,
}
#[derive(Clone)]
pub struct Enum {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_enum: Ident,
pub name: Ident,
pub generic_params: Option<GenericParamList>,
pub where_clause: Option<WhereClause>,
pub tk_braces: GroupSpan,
pub variants: Punctuated<EnumVariant>,
}
#[derive(Clone, Debug)]
pub struct EnumVariant {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub name: Ident,
pub fields: Fields,
pub value: Option<EnumVariantValue>,
}
#[derive(Clone, Debug)]
pub struct Module {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_unsafe: Option<Ident>,
pub tk_mod: Ident,
pub name: Ident,
pub tk_semicolon: Option<Punct>,
pub tk_braces: Option<GroupSpan>,
pub inner_attributes: Vec<Attribute>,
pub members: Vec<Item>,
}
#[derive(Clone)]
pub struct Union {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_union: Ident,
pub name: Ident,
pub generic_params: Option<GenericParamList>,
pub where_clause: Option<WhereClause>,
pub fields: NamedFields,
}
#[derive(Clone, Debug)]
pub struct Trait {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_unsafe: Option<Ident>,
pub tk_trait: Ident,
pub name: Ident,
pub generic_params: Option<GenericParamList>,
pub bound: Option<GenericBound>,
pub where_clause: Option<WhereClause>,
pub tk_braces: GroupSpan,
pub inner_attributes: Vec<Attribute>,
pub body_items: Vec<TraitMember>,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum TraitMember {
AssocFunction(Function),
AssocConstant(Constant),
AssocType(TypeAlias),
Macro(Macro),
}
#[derive(Clone, Debug)]
pub struct Impl {
pub attributes: Vec<Attribute>,
pub tk_unsafe: Option<Ident>,
pub tk_impl: Ident,
pub impl_generic_params: Option<GenericParamList>,
pub trait_ty: Option<TypeExpr>,
pub tk_for: Option<Ident>,
pub self_ty: TypeExpr,
pub where_clause: Option<WhereClause>,
pub tk_braces: GroupSpan,
pub inner_attributes: Vec<Attribute>,
pub body_items: Vec<ImplMember>,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ImplMember {
AssocFunction(Function),
AssocConstant(Constant),
AssocType(TypeAlias),
Macro(Macro),
}
#[derive(Clone, Debug)]
pub struct Constant {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_const_or_static: Ident,
pub tk_mut: Option<Ident>,
pub name: Ident,
pub tk_colon: Punct,
pub ty: TypeExpr,
pub tk_equals: Option<Punct>,
pub initializer: Option<ValueExpr>,
pub tk_semicolon: Punct,
}
impl Constant {
pub fn is_static(&self) -> bool {
self.tk_const_or_static == "static"
}
}
#[derive(Clone, Debug)]
pub struct TypeAlias {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_type: Ident,
pub name: Ident,
pub bound: Option<GenericBound>,
pub tk_equals: Option<Punct>,
pub initializer_ty: Option<TypeExpr>,
pub tk_semicolon: Punct,
}
#[derive(Clone, Debug)]
pub struct Function {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub qualifiers: FnQualifiers,
pub tk_fn_keyword: Ident,
pub name: Ident,
pub generic_params: Option<GenericParamList>,
pub tk_params_parens: GroupSpan,
pub params: Punctuated<FnParam>,
pub where_clause: Option<WhereClause>,
pub tk_return_arrow: Option<[Punct; 2]>,
pub return_ty: Option<TypeExpr>,
pub tk_semicolon: Option<Punct>,
pub body: Option<Group>,
}
#[derive(Clone, Debug, Default)]
pub struct FnQualifiers {
pub tk_default: Option<Ident>,
pub tk_const: Option<Ident>,
pub tk_async: Option<Ident>,
pub tk_unsafe: Option<Ident>,
pub tk_extern: Option<Ident>,
pub extern_abi: Option<Literal>,
}
#[derive(Clone, Debug)]
pub enum FnParam {
Receiver(FnReceiverParam),
Typed(FnTypedParam),
}
#[derive(Clone, Debug)]
pub struct FnReceiverParam {
pub attributes: Vec<Attribute>,
pub tk_ref: Option<Punct>,
pub lifetime: Option<Lifetime>,
pub tk_mut: Option<Ident>,
pub tk_self: Ident,
}
#[derive(Clone, Debug)]
pub struct FnTypedParam {
pub attributes: Vec<Attribute>,
pub tk_mut: Option<Ident>,
pub name: Ident,
pub tk_colon: Punct,
pub ty: TypeExpr,
}
#[derive(Clone, Debug)]
pub struct TupleField {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub ty: TypeExpr,
}
#[derive(Clone, Debug)]
pub struct NamedField {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub name: Ident,
pub tk_colon: Punct,
pub ty: TypeExpr,
}
#[derive(Clone)]
pub struct Attribute {
pub tk_hash: Punct,
pub tk_bang: Option<Punct>,
pub tk_brackets: GroupSpan,
pub path: Vec<TokenTree>,
pub value: AttributeValue,
}
#[derive(Clone)]
pub enum AttributeValue {
Group(GroupSpan, Vec<TokenTree>),
Equals(Punct, Vec<TokenTree>),
Empty,
}
#[derive(Clone)]
pub struct VisMarker {
pub tk_token1: TokenTree,
pub tk_token2: Option<TokenTree>,
}
#[derive(Clone)]
pub struct GenericParamList {
pub tk_l_bracket: Punct,
pub params: Punctuated<GenericParam>,
pub tk_r_bracket: Punct,
}
#[derive(Clone)]
pub struct GenericParam {
pub tk_prefix: Option<TokenTree>,
pub name: Ident,
pub bound: Option<GenericBound>,
}
#[derive(Clone)]
pub struct GenericBound {
pub tk_colon: Punct,
pub tokens: Vec<TokenTree>,
}
#[derive(Clone)]
pub struct GenericArgList {
pub tk_turbofish_colons: Option<[Punct; 2]>,
pub tk_l_bracket: Punct,
pub args: Punctuated<GenericArg>,
pub tk_r_bracket: Punct,
}
#[derive(Clone, Debug)]
pub enum GenericArg {
Lifetime { lifetime: Lifetime },
Binding {
ident: Ident,
tk_equals: Punct,
ty: TypeExpr,
},
TypeOrConst { expr: TypeExpr },
}
pub struct InlineGenericArgs<'a>(pub(crate) &'a GenericParamList);
#[derive(Clone, Debug)]
pub struct Lifetime {
pub tk_apostrophe: Punct,
pub name: Ident,
}
#[derive(Clone)]
pub struct WhereClause {
pub tk_where: Ident,
pub items: Punctuated<WhereClausePredicate>,
}
#[derive(Clone)]
pub struct WhereClausePredicate {
pub left_side: Vec<TokenTree>,
pub bound: GenericBound,
}
#[derive(Clone)]
pub struct TypeExpr {
pub tokens: Vec<TokenTree>,
}
#[derive(Clone)]
pub struct ValueExpr {
pub tokens: Vec<TokenTree>,
}
#[derive(Clone, Debug)]
pub struct Path {
pub segments: Vec<PathSegment>,
}
#[derive(Clone)]
pub struct PathSegment {
pub tk_separator_colons: Option<[Punct; 2]>,
pub ident: Ident,
pub generic_args: Option<GenericArgList>,
}
#[derive(Clone, Debug)]
pub struct UseDeclaration {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_use: Ident,
pub import_tree: TypeExpr,
pub tk_semicolon: Punct,
}
#[derive(Clone, Debug)]
pub struct EnumVariantValue {
pub tk_equal: Punct,
pub value: TokenTree,
}
#[derive(Clone, Debug)]
pub struct Macro {
pub attributes: Vec<Attribute>,
pub name: Ident,
pub tk_bang: Punct,
pub tk_declared_name: Option<Ident>,
pub tk_braces_or_parens: GroupSpan,
pub inner_tokens: Vec<TokenTree>,
pub tk_semicolon: Option<Punct>,
}
#[derive(Clone, Debug)]
pub struct ExternBlock {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_unsafe: Option<Ident>,
pub tk_extern: Ident,
pub extern_abi: Option<Literal>,
pub tk_braces: GroupSpan,
pub inner_attributes: Vec<Attribute>,
pub body_items: Vec<ImplMember>,
}
#[derive(Clone, Debug)]
pub struct ExternCrate {
pub attributes: Vec<Attribute>,
pub vis_marker: Option<VisMarker>,
pub tk_extern: Ident,
pub tk_crate: Ident,
pub name: Ident,
pub tk_as: Option<Ident>,
pub alias: Option<Ident>,
pub tk_underscore: Option<Ident>,
pub tk_semicolon: Punct,
}
#[derive(Clone)]
pub struct GroupSpan {
pub delimiter: Delimiter,
pub span: Span,
}
struct TokenRef<'a>(&'a TokenTree);
pub(crate) fn format_debug_tokens(
f: &mut std::fmt::Formatter<'_>,
tokens: &[TokenTree],
) -> std::fmt::Result {
let mut list = f.debug_list();
for token in tokens {
list.entry(&TokenRef(token));
}
list.finish()
}
impl std::fmt::Debug for TokenRef<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
TokenTree::Group(_group) => self.0.fmt(f),
TokenTree::Ident(_ident) => f.write_str(&self.0.to_string()),
TokenTree::Punct(_punct) => write!(f, "\"{}\"", &self.0.to_string()),
TokenTree::Literal(_literal) => f.write_str(&self.0.to_string()),
}
}
}
impl std::fmt::Debug for TupleFields {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fields.fmt(f)
}
}
impl std::fmt::Debug for NamedFields {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fields.fmt(f)
}
}
impl std::fmt::Debug for Enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Enum")
.field("attributes", &self.attributes)
.field("vis_marker", &self.vis_marker)
.field("tk_enum", &self.tk_enum)
.field("name", &self.name)
.field("generic_params", &self.generic_params)
.field("where_clauses", &self.where_clause)
.field("variants", &self.variants)
.finish()
}
}
impl std::fmt::Debug for Union {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Union")
.field("attributes", &self.attributes)
.field("vis_marker", &self.vis_marker)
.field("tk_union", &self.tk_union)
.field("name", &self.name)
.field("generic_params", &self.generic_params)
.field("where_clauses", &self.where_clause)
.field("fields", &self.fields)
.finish()
}
}
impl std::fmt::Debug for Attribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut f = f.debug_struct("Attribute");
f.field("tk_hash", &self.tk_hash);
if let Some(tk_bang) = self.tk_bang.as_ref() {
f.field("tk_bang", tk_bang);
}
f.field("tk_brackets", &self.tk_brackets);
let path_token_refs: Vec<_> = self.path.iter().map(TokenRef).collect();
f.field("path", &path_token_refs);
f.field("value", &self.value);
f.finish()
}
}
impl std::fmt::Debug for AttributeValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AttributeValue::Group(tk_group, value) => {
let mut f = f.debug_tuple("Group");
let value_token_refs: Vec<_> = value.iter().map(TokenRef).collect();
f.field(&value_token_refs);
f.field(&tk_group);
f.finish()
}
AttributeValue::Equals(tk_equals, value) => {
let mut f = f.debug_tuple("Equals");
let value_token_refs: Vec<_> = value.iter().map(TokenRef).collect();
f.field(&value_token_refs);
f.field(&tk_equals);
f.finish()
}
AttributeValue::Empty => f.write_str("Empty"),
}
}
}
impl std::fmt::Debug for VisMarker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.tk_token2 {
None => f.write_str(&self.tk_token1.to_string()),
Some(TokenTree::Group(group)) => {
let mut list = f.debug_tuple(&self.tk_token1.to_string());
for token in group.stream() {
list.field(&TokenRef(&token));
}
list.finish()
}
_ => unreachable!(),
}
}
}
impl std::fmt::Debug for PathSegment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut f = f.debug_struct("PathSegment");
if self.tk_separator_colons.is_some() {
f.field("tk_separator_colons", &"::");
}
f.field("ident", &self.ident);
if let Some(args) = self.generic_args.as_ref() {
f.field("generic_args", args);
}
f.finish()
}
}
impl std::fmt::Debug for GenericParamList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.params.fmt(f)
}
}
impl std::fmt::Debug for GenericParam {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut f = f.debug_struct("GenericParam");
if let Some(prefix) = self.tk_prefix.as_ref() {
f.field("tk_prefix", &prefix.to_string());
}
f.field("name", &self.name.to_string());
f.field("bound", &self.bound);
f.finish()
}
}
impl std::fmt::Debug for GenericBound {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format_debug_tokens(f, &self.tokens)
}
}
impl std::fmt::Debug for GenericArgList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut f = f.debug_struct("GenericArgList");
if self.tk_turbofish_colons.is_some() {
f.field("tk_turbofish_colons", &"::");
}
f.field("args", &self.args);
f.finish()
}
}
impl std::fmt::Debug for WhereClause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.items.fmt(f)
}
}
impl std::fmt::Debug for WhereClausePredicate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut list = f.debug_list();
for token in quote::quote!(#self) {
list.entry(&TokenRef(&token));
}
list.finish()
}
}
impl std::fmt::Debug for TypeExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format_debug_tokens(f, &self.tokens)
}
}
impl std::fmt::Debug for ValueExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format_debug_tokens(f, &self.tokens)
}
}
impl std::fmt::Debug for GroupSpan {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.delimiter {
Delimiter::Parenthesis => f.write_str("()"),
Delimiter::Brace => f.write_str("{}"),
Delimiter::Bracket => f.write_str("[]"),
Delimiter::None => f.write_str("Ø"),
}
}
}
impl GroupSpan {
fn quote_with(&self, tokens: &mut TokenStream, f: impl FnOnce(&mut TokenStream)) {
let mut inner = TokenStream::new();
f(&mut inner);
let mut g = Group::new(self.delimiter, inner);
g.set_span(self.span);
tokens.append(g);
}
}
impl ToTokens for Item {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Item::Struct(struct_decl) => struct_decl.to_tokens(tokens),
Item::Enum(enum_decl) => enum_decl.to_tokens(tokens),
Item::Union(union_decl) => union_decl.to_tokens(tokens),
Item::Module(mod_decl) => mod_decl.to_tokens(tokens),
Item::Trait(trait_decl) => trait_decl.to_tokens(tokens),
Item::Impl(impl_decl) => impl_decl.to_tokens(tokens),
Item::TypeAlias(ty_decl) => ty_decl.to_tokens(tokens),
Item::Function(function_decl) => function_decl.to_tokens(tokens),
Item::Constant(const_decl) => const_decl.to_tokens(tokens),
Item::UseDeclaration(use_decl) => use_decl.to_tokens(tokens),
Item::Macro(macro_decl) => macro_decl.to_tokens(tokens),
Item::ExternBlock(block_decl) => block_decl.to_tokens(tokens),
Item::ExternCrate(crate_decl) => crate_decl.to_tokens(tokens),
}
}
}
impl ToTokens for Struct {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_struct.to_tokens(tokens);
self.name.to_tokens(tokens);
self.generic_params.to_tokens(tokens);
if matches!(&self.fields, Fields::Named(_)) {
self.where_clause.to_tokens(tokens);
self.fields.to_tokens(tokens);
} else {
self.fields.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
self.tk_semicolon.to_tokens(tokens);
}
}
}
impl ToTokens for Fields {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Fields::Unit => (),
Fields::Tuple(fields) => fields.to_tokens(tokens),
Fields::Named(fields) => fields.to_tokens(tokens),
}
}
}
impl ToTokens for TupleFields {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_parens.quote_with(tokens, |tokens| {
self.fields.to_tokens(tokens);
});
}
}
impl ToTokens for NamedFields {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_braces.quote_with(tokens, |tokens| {
self.fields.to_tokens(tokens);
});
}
}
impl ToTokens for Enum {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_enum.to_tokens(tokens);
self.name.to_tokens(tokens);
self.generic_params.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
self.tk_braces.quote_with(tokens, |tokens| {
self.variants.to_tokens(tokens);
});
}
}
impl ToTokens for EnumVariant {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.name.to_tokens(tokens);
self.fields.to_tokens(tokens);
self.value.to_tokens(tokens);
}
}
impl ToTokens for Constant {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_const_or_static.to_tokens(tokens);
self.tk_mut.to_tokens(tokens);
self.name.to_tokens(tokens);
self.tk_colon.to_tokens(tokens);
self.ty.to_tokens(tokens);
self.tk_equals.to_tokens(tokens);
self.initializer.to_tokens(tokens);
self.tk_semicolon.to_tokens(tokens);
}
}
impl ToTokens for TypeAlias {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_type.to_tokens(tokens);
self.name.to_tokens(tokens);
self.bound.to_tokens(tokens);
self.tk_equals.to_tokens(tokens);
self.initializer_ty.to_tokens(tokens);
self.tk_semicolon.to_tokens(tokens);
}
}
impl ToTokens for Union {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_union.to_tokens(tokens);
self.name.to_tokens(tokens);
self.generic_params.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
self.fields.to_tokens(tokens);
}
}
impl ToTokens for Module {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_unsafe.to_tokens(tokens);
self.tk_mod.to_tokens(tokens);
self.name.to_tokens(tokens);
if let Some(tk_semicolon) = self.tk_semicolon.as_ref() {
tk_semicolon.to_tokens(tokens);
} else if let Some(tk_braces) = self.tk_braces.as_ref() {
tk_braces.quote_with(tokens, |tokens| {
for attribute in &self.inner_attributes {
attribute.to_tokens(tokens);
}
for token in &self.members {
token.to_tokens(tokens);
}
});
}
}
}
impl ToTokens for Trait {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_unsafe.to_tokens(tokens);
self.tk_trait.to_tokens(tokens);
self.name.to_tokens(tokens);
self.generic_params.to_tokens(tokens);
self.bound.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
self.tk_braces.quote_with(tokens, |tokens| {
for attribute in &self.inner_attributes {
attribute.to_tokens(tokens);
}
for item in self.body_items.iter() {
item.to_tokens(tokens)
}
});
}
}
impl ToTokens for Impl {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.tk_unsafe.to_tokens(tokens);
self.tk_impl.to_tokens(tokens);
self.impl_generic_params.to_tokens(tokens);
self.trait_ty.to_tokens(tokens);
self.tk_for.to_tokens(tokens);
self.self_ty.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
self.tk_braces.quote_with(tokens, |tokens| {
for attribute in &self.inner_attributes {
attribute.to_tokens(tokens);
}
for item in self.body_items.iter() {
item.to_tokens(tokens)
}
});
}
}
impl ToTokens for ImplMember {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
ImplMember::AssocFunction(function) => function.to_tokens(tokens),
ImplMember::AssocConstant(constant) => constant.to_tokens(tokens),
ImplMember::AssocType(assoc_ty) => assoc_ty.to_tokens(tokens),
ImplMember::Macro(macro_) => macro_.to_tokens(tokens),
}
}
}
impl ToTokens for TraitMember {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
TraitMember::AssocFunction(function) => function.to_tokens(tokens),
TraitMember::AssocConstant(constant) => constant.to_tokens(tokens),
TraitMember::AssocType(assoc_ty) => assoc_ty.to_tokens(tokens),
TraitMember::Macro(macro_) => macro_.to_tokens(tokens),
}
}
}
impl ToTokens for Function {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.qualifiers.to_tokens(tokens);
self.tk_fn_keyword.to_tokens(tokens);
self.name.to_tokens(tokens);
self.generic_params.to_tokens(tokens);
self.tk_params_parens.quote_with(tokens, |tokens| {
self.params.to_tokens(tokens);
});
if let Some([dash, tip]) = self.tk_return_arrow.as_ref() {
dash.to_tokens(tokens);
tip.to_tokens(tokens);
}
self.return_ty.to_tokens(tokens);
self.where_clause.to_tokens(tokens);
if let Some(body) = self.body.as_ref() {
body.to_tokens(tokens);
}
if let Some(semicolon) = self.tk_semicolon.as_ref() {
semicolon.to_tokens(tokens);
}
}
}
impl ToTokens for FnQualifiers {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_default.to_tokens(tokens);
self.tk_const.to_tokens(tokens);
self.tk_async.to_tokens(tokens);
self.tk_unsafe.to_tokens(tokens);
self.tk_extern.to_tokens(tokens);
self.extern_abi.to_tokens(tokens);
}
}
impl ToTokens for FnParam {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
FnParam::Receiver(param) => param.to_tokens(tokens),
FnParam::Typed(param) => param.to_tokens(tokens),
}
}
}
impl ToTokens for FnReceiverParam {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.tk_ref.to_tokens(tokens);
self.lifetime.to_tokens(tokens);
self.tk_mut.to_tokens(tokens);
self.tk_self.to_tokens(tokens);
}
}
impl ToTokens for FnTypedParam {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.tk_mut.to_tokens(tokens);
self.name.to_tokens(tokens);
self.tk_colon.to_tokens(tokens);
self.ty.to_tokens(tokens);
}
}
impl ToTokens for TupleField {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.ty.to_tokens(tokens);
}
}
impl ToTokens for NamedField {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.name.to_tokens(tokens);
self.tk_colon.to_tokens(tokens);
self.ty.to_tokens(tokens);
}
}
impl ToTokens for Attribute {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_hash.to_tokens(tokens);
if let Some(tk_bang) = self.tk_bang.as_ref() {
tk_bang.to_tokens(tokens);
}
self.tk_brackets.quote_with(tokens, |tokens| {
for token in &self.path {
token.to_tokens(tokens);
}
self.value.to_tokens(tokens);
});
}
}
impl ToTokens for AttributeValue {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
AttributeValue::Group(tk_group, value) => {
tk_group.quote_with(tokens, |tokens| {
for token in value {
token.to_tokens(tokens);
}
});
}
AttributeValue::Equals(tk_equals, value) => {
tk_equals.to_tokens(tokens);
for token in value {
token.to_tokens(tokens);
}
}
AttributeValue::Empty => (),
}
}
}
impl ToTokens for VisMarker {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_token1.to_tokens(tokens);
self.tk_token2.to_tokens(tokens);
}
}
impl ToTokens for Path {
fn to_tokens(&self, tokens: &mut TokenStream) {
for segment in &self.segments {
segment.to_tokens(tokens);
}
}
}
impl ToTokens for PathSegment {
fn to_tokens(&self, tokens: &mut TokenStream) {
if let Some(colons) = &self.tk_separator_colons {
tokens.append(colons[0].clone());
tokens.append(colons[1].clone());
}
self.ident.to_tokens(tokens);
self.generic_args.to_tokens(tokens);
}
}
impl ToTokens for GenericParamList {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.tk_l_bracket.clone());
self.params.to_tokens(tokens);
tokens.append(self.tk_r_bracket.clone());
}
}
impl ToTokens for GenericParam {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_prefix.to_tokens(tokens);
self.name.to_tokens(tokens);
self.bound.to_tokens(tokens);
}
}
impl ToTokens for GenericBound {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_colon.to_tokens(tokens);
for token in &self.tokens {
tokens.append(token.clone());
}
}
}
impl ToTokens for GenericArgList {
fn to_tokens(&self, tokens: &mut TokenStream) {
if let Some(colons) = &self.tk_turbofish_colons {
tokens.append(colons[0].clone());
tokens.append(colons[1].clone());
}
tokens.append(self.tk_l_bracket.clone());
self.args.to_tokens(tokens);
tokens.append(self.tk_r_bracket.clone());
}
}
impl ToTokens for GenericArg {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
GenericArg::Lifetime { lifetime } => {
lifetime.to_tokens(tokens);
}
GenericArg::Binding {
ident,
tk_equals,
ty,
} => {
ident.to_tokens(tokens);
tk_equals.to_tokens(tokens);
ty.to_tokens(tokens);
}
GenericArg::TypeOrConst { expr } => {
expr.to_tokens(tokens);
}
}
}
}
impl ToTokens for InlineGenericArgs<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(Punct::new('<', Spacing::Alone));
for param in &self.0.params.inner {
if param.0.is_lifetime() {
param.0.tk_prefix.to_tokens(tokens);
}
tokens.append(param.0.name.clone());
tokens.append(Punct::new(',', Spacing::Alone));
}
tokens.append(Punct::new('>', Spacing::Alone));
}
}
impl ToTokens for Lifetime {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.tk_apostrophe.clone());
tokens.append(self.name.clone());
}
}
impl ToTokens for WhereClause {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(self.tk_where.clone());
self.items.to_tokens(tokens);
}
}
impl ToTokens for WhereClausePredicate {
fn to_tokens(&self, tokens: &mut TokenStream) {
for token in &self.left_side {
tokens.append(token.clone());
}
self.bound.to_tokens(tokens);
}
}
impl ToTokens for TypeExpr {
fn to_tokens(&self, tokens: &mut TokenStream) {
for token in &self.tokens {
tokens.append(token.clone());
}
}
}
impl ToTokens for ValueExpr {
fn to_tokens(&self, tokens: &mut TokenStream) {
for token in &self.tokens {
tokens.append(token.clone());
}
}
}
impl ToTokens for UseDeclaration {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_use.to_tokens(tokens);
self.import_tree.to_tokens(tokens);
self.tk_semicolon.to_tokens(tokens);
}
}
impl ToTokens for EnumVariantValue {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.tk_equal.to_tokens(tokens);
self.value.to_tokens(tokens);
}
}
impl ToTokens for Macro {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.name.to_tokens(tokens);
self.tk_bang.to_tokens(tokens);
self.tk_declared_name.to_tokens(tokens);
self.tk_braces_or_parens.quote_with(tokens, |tokens| {
tokens.extend(self.inner_tokens.iter().cloned());
});
self.tk_semicolon.to_tokens(tokens);
}
}
impl ToTokens for ExternBlock {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_unsafe.to_tokens(tokens);
self.tk_extern.to_tokens(tokens);
self.extern_abi.to_tokens(tokens);
self.tk_braces.quote_with(tokens, |tokens| {
for attribute in &self.inner_attributes {
attribute.to_tokens(tokens);
}
for item in &self.body_items {
item.to_tokens(tokens);
}
});
}
}
impl ToTokens for ExternCrate {
fn to_tokens(&self, tokens: &mut TokenStream) {
for attribute in &self.attributes {
attribute.to_tokens(tokens);
}
self.vis_marker.to_tokens(tokens);
self.tk_extern.to_tokens(tokens);
self.tk_crate.to_tokens(tokens);
self.name.to_tokens(tokens);
self.tk_as.to_tokens(tokens);
self.alias.to_tokens(tokens);
self.tk_underscore.to_tokens(tokens);
self.tk_semicolon.to_tokens(tokens);
}
}
impl Default for GenericParamList {
fn default() -> Self {
Self {
tk_l_bracket: Punct::new('<', Spacing::Alone),
params: Punctuated::new(),
tk_r_bracket: Punct::new('>', Spacing::Alone),
}
}
}
impl Default for WhereClause {
fn default() -> Self {
Self {
tk_where: Ident::new("where", Span::call_site()),
items: Punctuated::new(),
}
}
}