1 // Copyright 2018 Syn Developers
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 use super::*;
10 use derive::{Data, DeriveInput};
11 use proc_macro2::TokenStream;
12 use punctuated::Punctuated;
13 use token::{Brace, Paren};
14 
15 #[cfg(feature = "extra-traits")]
16 use std::hash::{Hash, Hasher};
17 #[cfg(feature = "extra-traits")]
18 use tt::TokenStreamHelper;
19 
20 ast_enum_of_structs! {
21     /// Things that can appear directly inside of a module or scope.
22     ///
23     /// *This type is available if Syn is built with the `"full"` feature.*
24     ///
25     /// # Syntax tree enum
26     ///
27     /// This type is a [syntax tree enum].
28     ///
29     /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
30     pub enum Item {
31         /// An `extern crate` item: `extern crate serde`.
32         ///
33         /// *This type is available if Syn is built with the `"full"` feature.*
34         pub ExternCrate(ItemExternCrate {
35             pub attrs: Vec<Attribute>,
36             pub vis: Visibility,
37             pub extern_token: Token![extern],
38             pub crate_token: Token![crate],
39             pub ident: Ident,
40             pub rename: Option<(Token![as], Ident)>,
41             pub semi_token: Token![;],
42         }),
43 
44         /// A use declaration: `use std::collections::HashMap`.
45         ///
46         /// *This type is available if Syn is built with the `"full"` feature.*
47         pub Use(ItemUse {
48             pub attrs: Vec<Attribute>,
49             pub vis: Visibility,
50             pub use_token: Token![use],
51             pub leading_colon: Option<Token![::]>,
52             pub tree: UseTree,
53             pub semi_token: Token![;],
54         }),
55 
56         /// A static item: `static BIKE: Shed = Shed(42)`.
57         ///
58         /// *This type is available if Syn is built with the `"full"` feature.*
59         pub Static(ItemStatic {
60             pub attrs: Vec<Attribute>,
61             pub vis: Visibility,
62             pub static_token: Token![static],
63             pub mutability: Option<Token![mut]>,
64             pub ident: Ident,
65             pub colon_token: Token![:],
66             pub ty: Box<Type>,
67             pub eq_token: Token![=],
68             pub expr: Box<Expr>,
69             pub semi_token: Token![;],
70         }),
71 
72         /// A constant item: `const MAX: u16 = 65535`.
73         ///
74         /// *This type is available if Syn is built with the `"full"` feature.*
75         pub Const(ItemConst {
76             pub attrs: Vec<Attribute>,
77             pub vis: Visibility,
78             pub const_token: Token![const],
79             pub ident: Ident,
80             pub colon_token: Token![:],
81             pub ty: Box<Type>,
82             pub eq_token: Token![=],
83             pub expr: Box<Expr>,
84             pub semi_token: Token![;],
85         }),
86 
87         /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
88         /// }`.
89         ///
90         /// *This type is available if Syn is built with the `"full"` feature.*
91         pub Fn(ItemFn {
92             pub attrs: Vec<Attribute>,
93             pub vis: Visibility,
94             pub constness: Option<Token![const]>,
95             pub unsafety: Option<Token![unsafe]>,
96             pub abi: Option<Abi>,
97             pub ident: Ident,
98             pub decl: Box<FnDecl>,
99             pub block: Box<Block>,
100         }),
101 
102         /// A module or module declaration: `mod m` or `mod m { ... }`.
103         ///
104         /// *This type is available if Syn is built with the `"full"` feature.*
105         pub Mod(ItemMod {
106             pub attrs: Vec<Attribute>,
107             pub vis: Visibility,
108             pub mod_token: Token![mod],
109             pub ident: Ident,
110             pub content: Option<(token::Brace, Vec<Item>)>,
111             pub semi: Option<Token![;]>,
112         }),
113 
114         /// A block of foreign items: `extern "C" { ... }`.
115         ///
116         /// *This type is available if Syn is built with the `"full"` feature.*
117         pub ForeignMod(ItemForeignMod {
118             pub attrs: Vec<Attribute>,
119             pub abi: Abi,
120             pub brace_token: token::Brace,
121             pub items: Vec<ForeignItem>,
122         }),
123 
124         /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
125         ///
126         /// *This type is available if Syn is built with the `"full"` feature.*
127         pub Type(ItemType {
128             pub attrs: Vec<Attribute>,
129             pub vis: Visibility,
130             pub type_token: Token![type],
131             pub ident: Ident,
132             pub generics: Generics,
133             pub eq_token: Token![=],
134             pub ty: Box<Type>,
135             pub semi_token: Token![;],
136         }),
137 
138         /// A struct definition: `struct Foo<A> { x: A }`.
139         ///
140         /// *This type is available if Syn is built with the `"full"` feature.*
141         pub Struct(ItemStruct {
142             pub attrs: Vec<Attribute>,
143             pub vis: Visibility,
144             pub struct_token: Token![struct],
145             pub ident: Ident,
146             pub generics: Generics,
147             pub fields: Fields,
148             pub semi_token: Option<Token![;]>,
149         }),
150 
151         /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
152         ///
153         /// *This type is available if Syn is built with the `"full"` feature.*
154         pub Enum(ItemEnum {
155             pub attrs: Vec<Attribute>,
156             pub vis: Visibility,
157             pub enum_token: Token![enum],
158             pub ident: Ident,
159             pub generics: Generics,
160             pub brace_token: token::Brace,
161             pub variants: Punctuated<Variant, Token![,]>,
162         }),
163 
164         /// A union definition: `union Foo<A, B> { x: A, y: B }`.
165         ///
166         /// *This type is available if Syn is built with the `"full"` feature.*
167         pub Union(ItemUnion {
168             pub attrs: Vec<Attribute>,
169             pub vis: Visibility,
170             pub union_token: Token![union],
171             pub ident: Ident,
172             pub generics: Generics,
173             pub fields: FieldsNamed,
174         }),
175 
176         /// A trait definition: `pub trait Iterator { ... }`.
177         ///
178         /// *This type is available if Syn is built with the `"full"` feature.*
179         pub Trait(ItemTrait {
180             pub attrs: Vec<Attribute>,
181             pub vis: Visibility,
182             pub unsafety: Option<Token![unsafe]>,
183             pub auto_token: Option<Token![auto]>,
184             pub trait_token: Token![trait],
185             pub ident: Ident,
186             pub generics: Generics,
187             pub colon_token: Option<Token![:]>,
188             pub supertraits: Punctuated<TypeParamBound, Token![+]>,
189             pub brace_token: token::Brace,
190             pub items: Vec<TraitItem>,
191         }),
192 
193         /// An impl block providing trait or associated items: `impl<A> Trait
194         /// for Data<A> { ... }`.
195         ///
196         /// *This type is available if Syn is built with the `"full"` feature.*
197         pub Impl(ItemImpl {
198             pub attrs: Vec<Attribute>,
199             pub defaultness: Option<Token![default]>,
200             pub unsafety: Option<Token![unsafe]>,
201             pub impl_token: Token![impl],
202             pub generics: Generics,
203             /// Trait this impl implements.
204             pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
205             /// The Self type of the impl.
206             pub self_ty: Box<Type>,
207             pub brace_token: token::Brace,
208             pub items: Vec<ImplItem>,
209         }),
210 
211         /// A macro invocation, which includes `macro_rules!` definitions.
212         ///
213         /// *This type is available if Syn is built with the `"full"` feature.*
214         pub Macro(ItemMacro {
215             pub attrs: Vec<Attribute>,
216             /// The `example` in `macro_rules! example { ... }`.
217             pub ident: Option<Ident>,
218             pub mac: Macro,
219             pub semi_token: Option<Token![;]>,
220         }),
221 
222         /// A 2.0-style declarative macro introduced by the `macro` keyword.
223         ///
224         /// *This type is available if Syn is built with the `"full"` feature.*
225         pub Macro2(ItemMacro2 #manual_extra_traits {
226             pub attrs: Vec<Attribute>,
227             pub vis: Visibility,
228             pub macro_token: Token![macro],
229             pub ident: Ident,
230             pub paren_token: Paren,
231             pub args: TokenStream,
232             pub brace_token: Brace,
233             pub body: TokenStream,
234         }),
235 
236         /// Tokens forming an item not interpreted by Syn.
237         ///
238         /// *This type is available if Syn is built with the `"full"` feature.*
239         pub Verbatim(ItemVerbatim #manual_extra_traits {
240             pub tts: TokenStream,
241         }),
242     }
243 }
244 
245 #[cfg(feature = "extra-traits")]
246 impl Eq for ItemMacro2 {}
247 
248 #[cfg(feature = "extra-traits")]
249 impl PartialEq for ItemMacro2 {
eq(&self, other: &Self) -> bool250     fn eq(&self, other: &Self) -> bool {
251         self.attrs == other.attrs
252             && self.vis == other.vis
253             && self.macro_token == other.macro_token
254             && self.ident == other.ident
255             && self.paren_token == other.paren_token
256             && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
257             && self.brace_token == other.brace_token
258             && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
259     }
260 }
261 
262 #[cfg(feature = "extra-traits")]
263 impl Hash for ItemMacro2 {
hash<H>(&self, state: &mut H) where H: Hasher,264     fn hash<H>(&self, state: &mut H)
265     where
266         H: Hasher,
267     {
268         self.attrs.hash(state);
269         self.vis.hash(state);
270         self.macro_token.hash(state);
271         self.ident.hash(state);
272         self.paren_token.hash(state);
273         TokenStreamHelper(&self.args).hash(state);
274         self.brace_token.hash(state);
275         TokenStreamHelper(&self.body).hash(state);
276     }
277 }
278 
279 #[cfg(feature = "extra-traits")]
280 impl Eq for ItemVerbatim {}
281 
282 #[cfg(feature = "extra-traits")]
283 impl PartialEq for ItemVerbatim {
eq(&self, other: &Self) -> bool284     fn eq(&self, other: &Self) -> bool {
285         TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
286     }
287 }
288 
289 #[cfg(feature = "extra-traits")]
290 impl Hash for ItemVerbatim {
hash<H>(&self, state: &mut H) where H: Hasher,291     fn hash<H>(&self, state: &mut H)
292     where
293         H: Hasher,
294     {
295         TokenStreamHelper(&self.tts).hash(state);
296     }
297 }
298 
299 impl From<DeriveInput> for Item {
from(input: DeriveInput) -> Item300     fn from(input: DeriveInput) -> Item {
301         match input.data {
302             Data::Struct(data) => Item::Struct(ItemStruct {
303                 attrs: input.attrs,
304                 vis: input.vis,
305                 struct_token: data.struct_token,
306                 ident: input.ident,
307                 generics: input.generics,
308                 fields: data.fields,
309                 semi_token: data.semi_token,
310             }),
311             Data::Enum(data) => Item::Enum(ItemEnum {
312                 attrs: input.attrs,
313                 vis: input.vis,
314                 enum_token: data.enum_token,
315                 ident: input.ident,
316                 generics: input.generics,
317                 brace_token: data.brace_token,
318                 variants: data.variants,
319             }),
320             Data::Union(data) => Item::Union(ItemUnion {
321                 attrs: input.attrs,
322                 vis: input.vis,
323                 union_token: data.union_token,
324                 ident: input.ident,
325                 generics: input.generics,
326                 fields: data.fields,
327             }),
328         }
329     }
330 }
331 
332 ast_enum_of_structs! {
333     /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
334     ///
335     /// *This type is available if Syn is built with the `"full"` feature.*
336     ///
337     /// # Syntax tree enum
338     ///
339     /// This type is a [syntax tree enum].
340     ///
341     /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
342     pub enum UseTree {
343         /// A path prefix of imports in a `use` item: `std::...`.
344         ///
345         /// *This type is available if Syn is built with the `"full"` feature.*
346         pub Path(UsePath {
347             pub ident: Ident,
348             pub colon2_token: Token![::],
349             pub tree: Box<UseTree>,
350         }),
351 
352         /// An identifier imported by a `use` item: `HashMap`.
353         ///
354         /// *This type is available if Syn is built with the `"full"` feature.*
355         pub Name(UseName {
356             pub ident: Ident,
357         }),
358 
359         /// An renamed identifier imported by a `use` item: `HashMap as Map`.
360         ///
361         /// *This type is available if Syn is built with the `"full"` feature.*
362         pub Rename(UseRename {
363             pub ident: Ident,
364             pub as_token: Token![as],
365             pub rename: Ident,
366         }),
367 
368         /// A glob import in a `use` item: `*`.
369         ///
370         /// *This type is available if Syn is built with the `"full"` feature.*
371         pub Glob(UseGlob {
372             pub star_token: Token![*],
373         }),
374 
375         /// A braced group of imports in a `use` item: `{A, B, C}`.
376         ///
377         /// *This type is available if Syn is built with the `"full"` feature.*
378         pub Group(UseGroup {
379             pub brace_token: token::Brace,
380             pub items: Punctuated<UseTree, Token![,]>,
381         }),
382     }
383 }
384 
385 ast_enum_of_structs! {
386     /// An item within an `extern` block.
387     ///
388     /// *This type is available if Syn is built with the `"full"` feature.*
389     ///
390     /// # Syntax tree enum
391     ///
392     /// This type is a [syntax tree enum].
393     ///
394     /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
395     pub enum ForeignItem {
396         /// A foreign function in an `extern` block.
397         ///
398         /// *This type is available if Syn is built with the `"full"` feature.*
399         pub Fn(ForeignItemFn {
400             pub attrs: Vec<Attribute>,
401             pub vis: Visibility,
402             pub ident: Ident,
403             pub decl: Box<FnDecl>,
404             pub semi_token: Token![;],
405         }),
406 
407         /// A foreign static item in an `extern` block: `static ext: u8`.
408         ///
409         /// *This type is available if Syn is built with the `"full"` feature.*
410         pub Static(ForeignItemStatic {
411             pub attrs: Vec<Attribute>,
412             pub vis: Visibility,
413             pub static_token: Token![static],
414             pub mutability: Option<Token![mut]>,
415             pub ident: Ident,
416             pub colon_token: Token![:],
417             pub ty: Box<Type>,
418             pub semi_token: Token![;],
419         }),
420 
421         /// A foreign type in an `extern` block: `type void`.
422         ///
423         /// *This type is available if Syn is built with the `"full"` feature.*
424         pub Type(ForeignItemType {
425             pub attrs: Vec<Attribute>,
426             pub vis: Visibility,
427             pub type_token: Token![type],
428             pub ident: Ident,
429             pub semi_token: Token![;],
430         }),
431 
432         /// Tokens in an `extern` block not interpreted by Syn.
433         ///
434         /// *This type is available if Syn is built with the `"full"` feature.*
435         pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
436             pub tts: TokenStream,
437         }),
438     }
439 }
440 
441 #[cfg(feature = "extra-traits")]
442 impl Eq for ForeignItemVerbatim {}
443 
444 #[cfg(feature = "extra-traits")]
445 impl PartialEq for ForeignItemVerbatim {
eq(&self, other: &Self) -> bool446     fn eq(&self, other: &Self) -> bool {
447         TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
448     }
449 }
450 
451 #[cfg(feature = "extra-traits")]
452 impl Hash for ForeignItemVerbatim {
hash<H>(&self, state: &mut H) where H: Hasher,453     fn hash<H>(&self, state: &mut H)
454     where
455         H: Hasher,
456     {
457         TokenStreamHelper(&self.tts).hash(state);
458     }
459 }
460 
461 ast_enum_of_structs! {
462     /// An item declaration within the definition of a trait.
463     ///
464     /// *This type is available if Syn is built with the `"full"` feature.*
465     ///
466     /// # Syntax tree enum
467     ///
468     /// This type is a [syntax tree enum].
469     ///
470     /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
471     pub enum TraitItem {
472         /// An associated constant within the definition of a trait.
473         ///
474         /// *This type is available if Syn is built with the `"full"` feature.*
475         pub Const(TraitItemConst {
476             pub attrs: Vec<Attribute>,
477             pub const_token: Token![const],
478             pub ident: Ident,
479             pub colon_token: Token![:],
480             pub ty: Type,
481             pub default: Option<(Token![=], Expr)>,
482             pub semi_token: Token![;],
483         }),
484 
485         /// A trait method within the definition of a trait.
486         ///
487         /// *This type is available if Syn is built with the `"full"` feature.*
488         pub Method(TraitItemMethod {
489             pub attrs: Vec<Attribute>,
490             pub sig: MethodSig,
491             pub default: Option<Block>,
492             pub semi_token: Option<Token![;]>,
493         }),
494 
495         /// An associated type within the definition of a trait.
496         ///
497         /// *This type is available if Syn is built with the `"full"` feature.*
498         pub Type(TraitItemType {
499             pub attrs: Vec<Attribute>,
500             pub type_token: Token![type],
501             pub ident: Ident,
502             pub generics: Generics,
503             pub colon_token: Option<Token![:]>,
504             pub bounds: Punctuated<TypeParamBound, Token![+]>,
505             pub default: Option<(Token![=], Type)>,
506             pub semi_token: Token![;],
507         }),
508 
509         /// A macro invocation within the definition of a trait.
510         ///
511         /// *This type is available if Syn is built with the `"full"` feature.*
512         pub Macro(TraitItemMacro {
513             pub attrs: Vec<Attribute>,
514             pub mac: Macro,
515             pub semi_token: Option<Token![;]>,
516         }),
517 
518         /// Tokens within the definition of a trait not interpreted by Syn.
519         ///
520         /// *This type is available if Syn is built with the `"full"` feature.*
521         pub Verbatim(TraitItemVerbatim #manual_extra_traits {
522             pub tts: TokenStream,
523         }),
524     }
525 }
526 
527 #[cfg(feature = "extra-traits")]
528 impl Eq for TraitItemVerbatim {}
529 
530 #[cfg(feature = "extra-traits")]
531 impl PartialEq for TraitItemVerbatim {
eq(&self, other: &Self) -> bool532     fn eq(&self, other: &Self) -> bool {
533         TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
534     }
535 }
536 
537 #[cfg(feature = "extra-traits")]
538 impl Hash for TraitItemVerbatim {
hash<H>(&self, state: &mut H) where H: Hasher,539     fn hash<H>(&self, state: &mut H)
540     where
541         H: Hasher,
542     {
543         TokenStreamHelper(&self.tts).hash(state);
544     }
545 }
546 
547 ast_enum_of_structs! {
548     /// An item within an impl block.
549     ///
550     /// *This type is available if Syn is built with the `"full"` feature.*
551     ///
552     /// # Syntax tree enum
553     ///
554     /// This type is a [syntax tree enum].
555     ///
556     /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
557     pub enum ImplItem {
558         /// An associated constant within an impl block.
559         ///
560         /// *This type is available if Syn is built with the `"full"` feature.*
561         pub Const(ImplItemConst {
562             pub attrs: Vec<Attribute>,
563             pub vis: Visibility,
564             pub defaultness: Option<Token![default]>,
565             pub const_token: Token![const],
566             pub ident: Ident,
567             pub colon_token: Token![:],
568             pub ty: Type,
569             pub eq_token: Token![=],
570             pub expr: Expr,
571             pub semi_token: Token![;],
572         }),
573 
574         /// A method within an impl block.
575         ///
576         /// *This type is available if Syn is built with the `"full"` feature.*
577         pub Method(ImplItemMethod {
578             pub attrs: Vec<Attribute>,
579             pub vis: Visibility,
580             pub defaultness: Option<Token![default]>,
581             pub sig: MethodSig,
582             pub block: Block,
583         }),
584 
585         /// An associated type within an impl block.
586         ///
587         /// *This type is available if Syn is built with the `"full"` feature.*
588         pub Type(ImplItemType {
589             pub attrs: Vec<Attribute>,
590             pub vis: Visibility,
591             pub defaultness: Option<Token![default]>,
592             pub type_token: Token![type],
593             pub ident: Ident,
594             pub generics: Generics,
595             pub eq_token: Token![=],
596             pub ty: Type,
597             pub semi_token: Token![;],
598         }),
599 
600         /// A macro invocation within an impl block.
601         ///
602         /// *This type is available if Syn is built with the `"full"` feature.*
603         pub Macro(ImplItemMacro {
604             pub attrs: Vec<Attribute>,
605             pub mac: Macro,
606             pub semi_token: Option<Token![;]>,
607         }),
608 
609         /// Tokens within an impl block not interpreted by Syn.
610         ///
611         /// *This type is available if Syn is built with the `"full"` feature.*
612         pub Verbatim(ImplItemVerbatim #manual_extra_traits {
613             pub tts: TokenStream,
614         }),
615     }
616 }
617 
618 #[cfg(feature = "extra-traits")]
619 impl Eq for ImplItemVerbatim {}
620 
621 #[cfg(feature = "extra-traits")]
622 impl PartialEq for ImplItemVerbatim {
eq(&self, other: &Self) -> bool623     fn eq(&self, other: &Self) -> bool {
624         TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
625     }
626 }
627 
628 #[cfg(feature = "extra-traits")]
629 impl Hash for ImplItemVerbatim {
hash<H>(&self, state: &mut H) where H: Hasher,630     fn hash<H>(&self, state: &mut H)
631     where
632         H: Hasher,
633     {
634         TokenStreamHelper(&self.tts).hash(state);
635     }
636 }
637 
638 ast_struct! {
639     /// A method's signature in a trait or implementation: `unsafe fn
640     /// initialize(&self)`.
641     ///
642     /// *This type is available if Syn is built with the `"full"` feature.*
643     pub struct MethodSig {
644         pub constness: Option<Token![const]>,
645         pub unsafety: Option<Token![unsafe]>,
646         pub abi: Option<Abi>,
647         pub ident: Ident,
648         pub decl: FnDecl,
649     }
650 }
651 
652 ast_struct! {
653     /// Header of a function declaration, without including the body.
654     ///
655     /// *This type is available if Syn is built with the `"full"` feature.*
656     pub struct FnDecl {
657         pub fn_token: Token![fn],
658         pub generics: Generics,
659         pub paren_token: token::Paren,
660         pub inputs: Punctuated<FnArg, Token![,]>,
661         pub variadic: Option<Token![...]>,
662         pub output: ReturnType,
663     }
664 }
665 
666 ast_enum_of_structs! {
667     /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
668     ///
669     /// *This type is available if Syn is built with the `"full"` feature.*
670     ///
671     /// # Syntax tree enum
672     ///
673     /// This type is a [syntax tree enum].
674     ///
675     /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
676     pub enum FnArg {
677         /// Self captured by reference in a function signature: `&self` or `&mut
678         /// self`.
679         ///
680         /// *This type is available if Syn is built with the `"full"` feature.*
681         pub SelfRef(ArgSelfRef {
682             pub and_token: Token![&],
683             pub lifetime: Option<Lifetime>,
684             pub mutability: Option<Token![mut]>,
685             pub self_token: Token![self],
686         }),
687 
688         /// Self captured by value in a function signature: `self` or `mut
689         /// self`.
690         ///
691         /// *This type is available if Syn is built with the `"full"` feature.*
692         pub SelfValue(ArgSelf {
693             pub mutability: Option<Token![mut]>,
694             pub self_token: Token![self],
695         }),
696 
697         /// An explicitly typed pattern captured by a function signature.
698         ///
699         /// *This type is available if Syn is built with the `"full"` feature.*
700         pub Captured(ArgCaptured {
701             pub pat: Pat,
702             pub colon_token: Token![:],
703             pub ty: Type,
704         }),
705 
706         /// A pattern whose type is inferred captured by a function signature.
707         pub Inferred(Pat),
708         /// A type not bound to any pattern in a function signature.
709         pub Ignored(Type),
710     }
711 }
712 
713 #[cfg(feature = "parsing")]
714 pub mod parsing {
715     use super::*;
716 
717     use synom::Synom;
718 
719     impl_synom!(Item "item" alt!(
720         syn!(ItemExternCrate) => { Item::ExternCrate }
721         |
722         syn!(ItemUse) => { Item::Use }
723         |
724         syn!(ItemStatic) => { Item::Static }
725         |
726         syn!(ItemConst) => { Item::Const }
727         |
728         syn!(ItemFn) => { Item::Fn }
729         |
730         call!(unstable_async_fn) => { Item::Verbatim }
731         |
732         syn!(ItemMod) => { Item::Mod }
733         |
734         syn!(ItemForeignMod) => { Item::ForeignMod }
735         |
736         syn!(ItemType) => { Item::Type }
737         |
738         call!(unstable_existential_type) => { Item::Verbatim }
739         |
740         syn!(ItemStruct) => { Item::Struct }
741         |
742         syn!(ItemEnum) => { Item::Enum }
743         |
744         syn!(ItemUnion) => { Item::Union }
745         |
746         syn!(ItemTrait) => { Item::Trait }
747         |
748         syn!(ItemImpl) => { Item::Impl }
749         |
750         syn!(ItemMacro) => { Item::Macro }
751         |
752         syn!(ItemMacro2) => { Item::Macro2 }
753     ));
754 
755     impl_synom!(ItemMacro "macro item" do_parse!(
756         attrs: many0!(Attribute::parse_outer) >>
757         what: call!(Path::parse_mod_style) >>
758         bang: punct!(!) >>
759         ident: option!(syn!(Ident)) >>
760         body: call!(tt::delimited) >>
761         semi: cond!(!is_brace(&body.0), punct!(;)) >>
762         (ItemMacro {
763             attrs: attrs,
764             ident: ident,
765             mac: Macro {
766                 path: what,
767                 bang_token: bang,
768                 delimiter: body.0,
769                 tts: body.1,
770             },
771             semi_token: semi,
772         })
773     ));
774 
775     // TODO: figure out the actual grammar; is body required to be braced?
776     impl_synom!(ItemMacro2 "macro2 item" do_parse!(
777         attrs: many0!(Attribute::parse_outer) >>
778         vis: syn!(Visibility) >>
779         macro_: keyword!(macro) >>
780         ident: syn!(Ident) >>
781         args: call!(tt::parenthesized) >>
782         body: call!(tt::braced) >>
783         (ItemMacro2 {
784             attrs: attrs,
785             vis: vis,
786             macro_token: macro_,
787             ident: ident,
788             paren_token: args.0,
789             args: args.1,
790             brace_token: body.0,
791             body: body.1,
792         })
793     ));
794 
795     impl_synom!(ItemExternCrate "extern crate item" do_parse!(
796         attrs: many0!(Attribute::parse_outer) >>
797         vis: syn!(Visibility) >>
798         extern_: keyword!(extern) >>
799         crate_: keyword!(crate) >>
800         ident: syn!(Ident) >>
801         rename: option!(tuple!(keyword!(as), syn!(Ident))) >>
802         semi: punct!(;) >>
803         (ItemExternCrate {
804             attrs: attrs,
805             vis: vis,
806             extern_token: extern_,
807             crate_token: crate_,
808             ident: ident,
809             rename: rename,
810             semi_token: semi,
811         })
812     ));
813 
814     impl_synom!(ItemUse "use item" do_parse!(
815         attrs: many0!(Attribute::parse_outer) >>
816         vis: syn!(Visibility) >>
817         use_: keyword!(use) >>
818         leading_colon: option!(punct!(::)) >>
819         tree: syn!(UseTree) >>
820         semi: punct!(;) >>
821         (ItemUse {
822             attrs: attrs,
823             vis: vis,
824             use_token: use_,
825             leading_colon: leading_colon,
826             tree: tree,
827             semi_token: semi,
828         })
829     ));
830 
831     named!(use_element -> Ident, alt!(
832         syn!(Ident)
833         |
834         keyword!(self) => { Into::into }
835         |
836         keyword!(super) => { Into::into }
837         |
838         keyword!(crate) => { Into::into }
839         |
840         keyword!(extern) => { Into::into }
841     ));
842 
843     impl_synom!(UseTree "use tree" alt!(
844         syn!(UseRename) => { UseTree::Rename }
845         |
846         syn!(UsePath) => { UseTree::Path }
847         |
848         syn!(UseName) => { UseTree::Name }
849         |
850         syn!(UseGlob) => { UseTree::Glob }
851         |
852         syn!(UseGroup) => { UseTree::Group }
853     ));
854 
855     impl_synom!(UsePath "use path" do_parse!(
856         ident: call!(use_element) >>
857         colon2_token: punct!(::) >>
858         tree: syn!(UseTree) >>
859         (UsePath {
860             ident: ident,
861             colon2_token: colon2_token,
862             tree: Box::new(tree),
863         })
864     ));
865 
866     impl_synom!(UseName "use name" do_parse!(
867         ident: call!(use_element) >>
868         (UseName {
869             ident: ident,
870         })
871     ));
872 
873     impl_synom!(UseRename "use rename" do_parse!(
874         ident: call!(use_element) >>
875         as_token: keyword!(as) >>
876         rename: syn!(Ident) >>
877         (UseRename {
878             ident: ident,
879             as_token: as_token,
880             rename: rename,
881         })
882     ));
883 
884     impl_synom!(UseGlob "use glob" do_parse!(
885         star: punct!(*) >>
886         (UseGlob {
887             star_token: star,
888         })
889     ));
890 
891     impl_synom!(UseGroup "use group" do_parse!(
892         list: braces!(Punctuated::parse_terminated) >>
893         (UseGroup {
894             brace_token: list.0,
895             items: list.1,
896         })
897     ));
898 
899     impl_synom!(ItemStatic "static item" do_parse!(
900         attrs: many0!(Attribute::parse_outer) >>
901         vis: syn!(Visibility) >>
902         static_: keyword!(static) >>
903         mutability: option!(keyword!(mut)) >>
904         ident: syn!(Ident) >>
905         colon: punct!(:) >>
906         ty: syn!(Type) >>
907         eq: punct!(=) >>
908         value: syn!(Expr) >>
909         semi: punct!(;) >>
910         (ItemStatic {
911             attrs: attrs,
912             vis: vis,
913             static_token: static_,
914             mutability: mutability,
915             ident: ident,
916             colon_token: colon,
917             ty: Box::new(ty),
918             eq_token: eq,
919             expr: Box::new(value),
920             semi_token: semi,
921         })
922     ));
923 
924     impl_synom!(ItemConst "const item" do_parse!(
925         attrs: many0!(Attribute::parse_outer) >>
926         vis: syn!(Visibility) >>
927         const_: keyword!(const) >>
928         ident: syn!(Ident) >>
929         colon: punct!(:) >>
930         ty: syn!(Type) >>
931         eq: punct!(=) >>
932         value: syn!(Expr) >>
933         semi: punct!(;) >>
934         (ItemConst {
935             attrs: attrs,
936             vis: vis,
937             const_token: const_,
938             ident: ident,
939             colon_token: colon,
940             ty: Box::new(ty),
941             eq_token: eq,
942             expr: Box::new(value),
943             semi_token: semi,
944         })
945     ));
946 
947     impl_synom!(ItemFn "fn item" do_parse!(
948         outer_attrs: many0!(Attribute::parse_outer) >>
949         vis: syn!(Visibility) >>
950         constness: option!(keyword!(const)) >>
951         unsafety: option!(keyword!(unsafe)) >>
952         abi: option!(syn!(Abi)) >>
953         fn_: keyword!(fn) >>
954         ident: syn!(Ident) >>
955         generics: syn!(Generics) >>
956         inputs: parens!(Punctuated::parse_terminated) >>
957         ret: syn!(ReturnType) >>
958         where_clause: option!(syn!(WhereClause)) >>
959         inner_attrs_stmts: braces!(tuple!(
960             many0!(Attribute::parse_inner),
961             call!(Block::parse_within),
962         )) >>
963         (ItemFn {
964             attrs: {
965                 let mut attrs = outer_attrs;
966                 attrs.extend((inner_attrs_stmts.1).0);
967                 attrs
968             },
969             vis: vis,
970             constness: constness,
971             unsafety: unsafety,
972             abi: abi,
973             decl: Box::new(FnDecl {
974                 fn_token: fn_,
975                 paren_token: inputs.0,
976                 inputs: inputs.1,
977                 output: ret,
978                 variadic: None,
979                 generics: Generics {
980                     where_clause: where_clause,
981                     ..generics
982                 },
983             }),
984             ident: ident,
985             block: Box::new(Block {
986                 brace_token: inner_attrs_stmts.0,
987                 stmts: (inner_attrs_stmts.1).1,
988             }),
989         })
990     ));
991 
992     named!(unstable_async_fn -> ItemVerbatim, do_parse!(
993         begin: call!(verbatim::grab_cursor) >>
994         many0!(Attribute::parse_outer) >>
995         syn!(Visibility) >>
996         option!(keyword!(const)) >>
997         option!(keyword!(unsafe)) >>
998         keyword!(async) >>
999         option!(syn!(Abi)) >>
1000         keyword!(fn) >>
1001         syn!(Ident) >>
1002         syn!(Generics) >>
1003         parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1004         syn!(ReturnType) >>
1005         option!(syn!(WhereClause)) >>
1006         braces!(tuple!(
1007             many0!(Attribute::parse_inner),
1008             call!(Block::parse_within),
1009         )) >>
1010         end: call!(verbatim::grab_cursor) >>
1011         (ItemVerbatim {
1012             tts: verbatim::token_range(begin..end),
1013         })
1014     ));
1015 
1016     impl Synom for FnArg {
1017         named!(parse -> Self, alt!(
1018             do_parse!(
1019                 and: punct!(&) >>
1020                 lt: option!(syn!(Lifetime)) >>
1021                 mutability: option!(keyword!(mut)) >>
1022                 self_: keyword!(self) >>
1023                 not!(punct!(:)) >>
1024                 (ArgSelfRef {
1025                     lifetime: lt,
1026                     mutability: mutability,
1027                     and_token: and,
1028                     self_token: self_,
1029                 }.into())
1030             )
1031             |
1032             do_parse!(
1033                 mutability: option!(keyword!(mut)) >>
1034                 self_: keyword!(self) >>
1035                 not!(punct!(:)) >>
1036                 (ArgSelf {
1037                     mutability: mutability,
1038                     self_token: self_,
1039                 }.into())
1040             )
1041             |
1042             do_parse!(
1043                 pat: syn!(Pat) >>
1044                 colon: punct!(:) >>
1045                 ty: syn!(Type) >>
1046                 (ArgCaptured {
1047                     pat: pat,
1048                     ty: ty,
1049                     colon_token: colon,
1050                 }.into())
1051             )
1052             |
1053             syn!(Type) => { FnArg::Ignored }
1054         ));
1055 
description() -> Option<&'static str>1056         fn description() -> Option<&'static str> {
1057             Some("function argument")
1058         }
1059     }
1060 
1061     impl_synom!(ItemMod "mod item" do_parse!(
1062         outer_attrs: many0!(Attribute::parse_outer) >>
1063         vis: syn!(Visibility) >>
1064         mod_: keyword!(mod) >>
1065         ident: syn!(Ident) >>
1066         content_semi: alt!(
1067             punct!(;) => {|semi| (
1068                 Vec::new(),
1069                 None,
1070                 Some(semi),
1071             )}
1072             |
1073             braces!(
1074                 tuple!(
1075                     many0!(Attribute::parse_inner),
1076                     many0!(Item::parse),
1077                 )
1078             ) => {|(brace, (inner_attrs, items))| (
1079                 inner_attrs,
1080                 Some((brace, items)),
1081                 None,
1082             )}
1083         ) >>
1084         (ItemMod {
1085             attrs: {
1086                 let mut attrs = outer_attrs;
1087                 attrs.extend(content_semi.0);
1088                 attrs
1089             },
1090             vis: vis,
1091             mod_token: mod_,
1092             ident: ident,
1093             content: content_semi.1,
1094             semi: content_semi.2,
1095         })
1096     ));
1097 
1098     impl_synom!(ItemForeignMod "foreign mod item" do_parse!(
1099         outer_attrs: many0!(Attribute::parse_outer) >>
1100         abi: syn!(Abi) >>
1101         braced_content: braces!(tuple!(
1102             many0!(Attribute::parse_inner),
1103             many0!(ForeignItem::parse),
1104         )) >>
1105         (ItemForeignMod {
1106             attrs: {
1107                 let mut attrs = outer_attrs;
1108                 attrs.extend((braced_content.1).0);
1109                 attrs
1110             },
1111             abi: abi,
1112             brace_token: braced_content.0,
1113             items: (braced_content.1).1,
1114         })
1115     ));
1116 
1117     impl_synom!(ForeignItem "foreign item" alt!(
1118         syn!(ForeignItemFn) => { ForeignItem::Fn }
1119         |
1120         syn!(ForeignItemStatic) => { ForeignItem::Static }
1121         |
1122         syn!(ForeignItemType) => { ForeignItem::Type }
1123         |
1124         call!(foreign_item_macro) => { ForeignItem::Verbatim }
1125     ));
1126 
1127     impl_synom!(ForeignItemFn "foreign function" do_parse!(
1128         attrs: many0!(Attribute::parse_outer) >>
1129         vis: syn!(Visibility) >>
1130         fn_: keyword!(fn) >>
1131         ident: syn!(Ident) >>
1132         generics: syn!(Generics) >>
1133         inputs: parens!(do_parse!(
1134             args: call!(Punctuated::parse_terminated) >>
1135             variadic: option!(cond_reduce!(args.empty_or_trailing(), punct!(...))) >>
1136             (args, variadic)
1137         )) >>
1138         ret: syn!(ReturnType) >>
1139         where_clause: option!(syn!(WhereClause)) >>
1140         semi: punct!(;) >>
1141         ({
1142             let (parens, (inputs, variadic)) = inputs;
1143             ForeignItemFn {
1144                 ident: ident,
1145                 attrs: attrs,
1146                 semi_token: semi,
1147                 decl: Box::new(FnDecl {
1148                     fn_token: fn_,
1149                     paren_token: parens,
1150                     inputs: inputs,
1151                     variadic: variadic,
1152                     output: ret,
1153                     generics: Generics {
1154                         where_clause: where_clause,
1155                         ..generics
1156                     },
1157                 }),
1158                 vis: vis,
1159             }
1160         })
1161     ));
1162 
1163     impl_synom!(ForeignItemStatic "foreign static" do_parse!(
1164         attrs: many0!(Attribute::parse_outer) >>
1165         vis: syn!(Visibility) >>
1166         static_: keyword!(static) >>
1167         mutability: option!(keyword!(mut)) >>
1168         ident: syn!(Ident) >>
1169         colon: punct!(:) >>
1170         ty: syn!(Type) >>
1171         semi: punct!(;) >>
1172         (ForeignItemStatic {
1173             ident: ident,
1174             attrs: attrs,
1175             semi_token: semi,
1176             ty: Box::new(ty),
1177             mutability: mutability,
1178             static_token: static_,
1179             colon_token: colon,
1180             vis: vis,
1181         })
1182     ));
1183 
1184     impl_synom!(ForeignItemType "foreign type" do_parse!(
1185         attrs: many0!(Attribute::parse_outer) >>
1186         vis: syn!(Visibility) >>
1187         type_: keyword!(type) >>
1188         ident: syn!(Ident) >>
1189         semi: punct!(;) >>
1190         (ForeignItemType {
1191             attrs: attrs,
1192             vis: vis,
1193             type_token: type_,
1194             ident: ident,
1195             semi_token: semi,
1196         })
1197     ));
1198 
1199     named!(foreign_item_macro -> ForeignItemVerbatim, do_parse!(
1200         begin: call!(verbatim::grab_cursor) >>
1201         many0!(Attribute::parse_outer) >>
1202         mac: syn!(Macro) >>
1203         cond!(!is_brace(&mac.delimiter), punct!(;)) >>
1204         end: call!(verbatim::grab_cursor) >>
1205         (ForeignItemVerbatim {
1206             tts: verbatim::token_range(begin..end),
1207         })
1208     ));
1209 
1210     impl_synom!(ItemType "type item" do_parse!(
1211         attrs: many0!(Attribute::parse_outer) >>
1212         vis: syn!(Visibility) >>
1213         type_: keyword!(type) >>
1214         ident: syn!(Ident) >>
1215         generics: syn!(Generics) >>
1216         where_clause: option!(syn!(WhereClause)) >>
1217         eq: punct!(=) >>
1218         ty: syn!(Type) >>
1219         semi: punct!(;) >>
1220         (ItemType {
1221             attrs: attrs,
1222             vis: vis,
1223             type_token: type_,
1224             ident: ident,
1225             generics: Generics {
1226                 where_clause: where_clause,
1227                 ..generics
1228             },
1229             eq_token: eq,
1230             ty: Box::new(ty),
1231             semi_token: semi,
1232         })
1233     ));
1234 
1235     named!(existential_type_helper(vis: bool) -> TokenStream, do_parse!(
1236         begin: call!(verbatim::grab_cursor) >>
1237         many0!(Attribute::parse_outer) >>
1238         cond_reduce!(vis, syn!(Visibility)) >>
1239         custom_keyword!(existential) >>
1240         keyword!(type) >>
1241         syn!(Ident) >>
1242         syn!(Generics) >>
1243         option!(syn!(WhereClause)) >>
1244         colon: option!(punct!(:)) >>
1245         cond!(
1246             colon.is_some(),
1247             Punctuated::<TypeParamBound, Token![+]>::parse_separated_nonempty
1248         ) >>
1249         punct!(;) >>
1250         end: call!(verbatim::grab_cursor) >>
1251         (verbatim::token_range(begin..end))
1252     ));
1253 
1254     named!(unstable_existential_type -> ItemVerbatim, map!(
1255         call!(existential_type_helper, true),
1256         |tts| ItemVerbatim { tts: tts }
1257     ));
1258 
1259     impl_synom!(ItemStruct "struct item" switch!(
1260         map!(syn!(DeriveInput), Into::into),
1261         Item::Struct(item) => value!(item)
1262         |
1263         _ => reject!()
1264     ));
1265 
1266     impl_synom!(ItemEnum "enum item" switch!(
1267         map!(syn!(DeriveInput), Into::into),
1268         Item::Enum(item) => value!(item)
1269         |
1270         _ => reject!()
1271     ));
1272 
1273     impl_synom!(ItemUnion "union item" do_parse!(
1274         attrs: many0!(Attribute::parse_outer) >>
1275         vis: syn!(Visibility) >>
1276         union_: keyword!(union) >>
1277         ident: syn!(Ident) >>
1278         generics: syn!(Generics) >>
1279         where_clause: option!(syn!(WhereClause)) >>
1280         fields: syn!(FieldsNamed) >>
1281         (ItemUnion {
1282             attrs: attrs,
1283             vis: vis,
1284             union_token: union_,
1285             ident: ident,
1286             generics: Generics {
1287                 where_clause: where_clause,
1288                 ..generics
1289             },
1290             fields: fields,
1291         })
1292     ));
1293 
1294     impl_synom!(ItemTrait "trait item" do_parse!(
1295         attrs: many0!(Attribute::parse_outer) >>
1296         vis: syn!(Visibility) >>
1297         unsafety: option!(keyword!(unsafe)) >>
1298         auto_: option!(keyword!(auto)) >>
1299         trait_: keyword!(trait) >>
1300         ident: syn!(Ident) >>
1301         generics: syn!(Generics) >>
1302         colon: option!(punct!(:)) >>
1303         bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
1304         where_clause: option!(syn!(WhereClause)) >>
1305         body: braces!(many0!(TraitItem::parse)) >>
1306         (ItemTrait {
1307             attrs: attrs,
1308             vis: vis,
1309             unsafety: unsafety,
1310             auto_token: auto_,
1311             trait_token: trait_,
1312             ident: ident,
1313             generics: Generics {
1314                 where_clause: where_clause,
1315                 ..generics
1316             },
1317             colon_token: colon,
1318             supertraits: bounds.unwrap_or_default(),
1319             brace_token: body.0,
1320             items: body.1,
1321         })
1322     ));
1323 
1324     impl_synom!(TraitItem "trait item" alt!(
1325         syn!(TraitItemConst) => { TraitItem::Const }
1326         |
1327         syn!(TraitItemMethod) => { TraitItem::Method }
1328         |
1329         syn!(TraitItemType) => { TraitItem::Type }
1330         |
1331         call!(unstable_trait_existential_type) => { TraitItem::Verbatim }
1332         |
1333         syn!(TraitItemMacro) => { TraitItem::Macro }
1334     ));
1335 
1336     impl_synom!(TraitItemConst "const trait item" do_parse!(
1337         attrs: many0!(Attribute::parse_outer) >>
1338         const_: keyword!(const) >>
1339         ident: syn!(Ident) >>
1340         colon: punct!(:) >>
1341         ty: syn!(Type) >>
1342         default: option!(tuple!(punct!(=), syn!(Expr))) >>
1343         semi: punct!(;) >>
1344         (TraitItemConst {
1345             attrs: attrs,
1346             const_token: const_,
1347             ident: ident,
1348             colon_token: colon,
1349             ty: ty,
1350             default: default,
1351             semi_token: semi,
1352         })
1353     ));
1354 
1355     impl_synom!(TraitItemMethod "method trait item" do_parse!(
1356         outer_attrs: many0!(Attribute::parse_outer) >>
1357         constness: option!(keyword!(const)) >>
1358         unsafety: option!(keyword!(unsafe)) >>
1359         abi: option!(syn!(Abi)) >>
1360         fn_: keyword!(fn) >>
1361         ident: syn!(Ident) >>
1362         generics: syn!(Generics) >>
1363         inputs: parens!(Punctuated::parse_terminated) >>
1364         ret: syn!(ReturnType) >>
1365         where_clause: option!(syn!(WhereClause)) >>
1366         body: option!(braces!(tuple!(
1367             many0!(Attribute::parse_inner),
1368             call!(Block::parse_within),
1369         ))) >>
1370         semi: cond!(body.is_none(), punct!(;)) >>
1371         ({
1372             let (inner_attrs, stmts) = match body {
1373                 Some((b, (inner_attrs, stmts))) => (inner_attrs, Some((stmts, b))),
1374                 None => (Vec::new(), None),
1375             };
1376             TraitItemMethod {
1377                 attrs: {
1378                     let mut attrs = outer_attrs;
1379                     attrs.extend(inner_attrs);
1380                     attrs
1381                 },
1382                 sig: MethodSig {
1383                     constness: constness,
1384                     unsafety: unsafety,
1385                     abi: abi,
1386                     ident: ident,
1387                     decl: FnDecl {
1388                         inputs: inputs.1,
1389                         output: ret,
1390                         fn_token: fn_,
1391                         paren_token: inputs.0,
1392                         variadic: None,
1393                         generics: Generics {
1394                             where_clause: where_clause,
1395                             ..generics
1396                         },
1397                     },
1398                 },
1399                 default: stmts.map(|stmts| {
1400                     Block {
1401                         stmts: stmts.0,
1402                         brace_token: stmts.1,
1403                     }
1404                 }),
1405                 semi_token: semi,
1406             }
1407         })
1408     ));
1409 
1410     impl_synom!(TraitItemType "trait item type" do_parse!(
1411         attrs: many0!(Attribute::parse_outer) >>
1412         type_: keyword!(type) >>
1413         ident: syn!(Ident) >>
1414         generics: syn!(Generics) >>
1415         colon: option!(punct!(:)) >>
1416         bounds: cond!(colon.is_some(), Punctuated::parse_separated_nonempty) >>
1417         where_clause: option!(syn!(WhereClause)) >>
1418         default: option!(tuple!(punct!(=), syn!(Type))) >>
1419         semi: punct!(;) >>
1420         (TraitItemType {
1421             attrs: attrs,
1422             type_token: type_,
1423             ident: ident,
1424             generics: Generics {
1425                 where_clause: where_clause,
1426                 ..generics
1427             },
1428             colon_token: colon,
1429             bounds: bounds.unwrap_or_default(),
1430             default: default,
1431             semi_token: semi,
1432         })
1433     ));
1434 
1435     named!(unstable_trait_existential_type -> TraitItemVerbatim, map!(
1436         call!(existential_type_helper, false),
1437         |tts| TraitItemVerbatim { tts: tts }
1438     ));
1439 
1440     impl_synom!(TraitItemMacro "trait item macro" do_parse!(
1441         attrs: many0!(Attribute::parse_outer) >>
1442         mac: syn!(Macro) >>
1443         semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
1444         (TraitItemMacro {
1445             attrs: attrs,
1446             mac: mac,
1447             semi_token: semi,
1448         })
1449     ));
1450 
1451     impl_synom!(ItemImpl "impl item" do_parse!(
1452         outer_attrs: many0!(Attribute::parse_outer) >>
1453         defaultness: option!(keyword!(default)) >>
1454         unsafety: option!(keyword!(unsafe)) >>
1455         impl_: keyword!(impl) >>
1456         generics: syn!(Generics) >>
1457         polarity_path: alt!(
1458             do_parse!(
1459                 polarity: option!(punct!(!)) >>
1460                 path: syn!(Path) >>
1461                 for_: keyword!(for) >>
1462                 (Some((polarity, path, for_)))
1463             )
1464             |
1465             epsilon!() => { |_| None }
1466         ) >>
1467         self_ty: syn!(Type) >>
1468         where_clause: option!(syn!(WhereClause)) >>
1469         inner: braces!(tuple!(
1470             many0!(Attribute::parse_inner),
1471             many0!(ImplItem::parse),
1472         )) >>
1473         (ItemImpl {
1474             attrs: {
1475                 let mut attrs = outer_attrs;
1476                 attrs.extend((inner.1).0);
1477                 attrs
1478             },
1479             defaultness: defaultness,
1480             unsafety: unsafety,
1481             impl_token: impl_,
1482             generics: Generics {
1483                 where_clause: where_clause,
1484                 ..generics
1485             },
1486             trait_: polarity_path,
1487             self_ty: Box::new(self_ty),
1488             brace_token: inner.0,
1489             items: (inner.1).1,
1490         })
1491     ));
1492 
1493     impl_synom!(ImplItem "item in impl block" alt!(
1494         syn!(ImplItemConst) => { ImplItem::Const }
1495         |
1496         syn!(ImplItemMethod) => { ImplItem::Method }
1497         |
1498         call!(unstable_async_method) => { ImplItem::Verbatim }
1499         |
1500         syn!(ImplItemType) => { ImplItem::Type }
1501         |
1502         call!(unstable_impl_existential_type) => { ImplItem::Verbatim }
1503         |
1504         syn!(ImplItemMacro) => { ImplItem::Macro }
1505     ));
1506 
1507     impl_synom!(ImplItemConst "const item in impl block" do_parse!(
1508         attrs: many0!(Attribute::parse_outer) >>
1509         vis: syn!(Visibility) >>
1510         defaultness: option!(keyword!(default)) >>
1511         const_: keyword!(const) >>
1512         ident: syn!(Ident) >>
1513         colon: punct!(:) >>
1514         ty: syn!(Type) >>
1515         eq: punct!(=) >>
1516         value: syn!(Expr) >>
1517         semi: punct!(;) >>
1518         (ImplItemConst {
1519             attrs: attrs,
1520             vis: vis,
1521             defaultness: defaultness,
1522             const_token: const_,
1523             ident: ident,
1524             colon_token: colon,
1525             ty: ty,
1526             eq_token: eq,
1527             expr: value,
1528             semi_token: semi,
1529         })
1530     ));
1531 
1532     impl_synom!(ImplItemMethod "method in impl block" do_parse!(
1533         outer_attrs: many0!(Attribute::parse_outer) >>
1534         vis: syn!(Visibility) >>
1535         defaultness: option!(keyword!(default)) >>
1536         constness: option!(keyword!(const)) >>
1537         unsafety: option!(keyword!(unsafe)) >>
1538         abi: option!(syn!(Abi)) >>
1539         fn_: keyword!(fn) >>
1540         ident: syn!(Ident) >>
1541         generics: syn!(Generics) >>
1542         inputs: parens!(Punctuated::parse_terminated) >>
1543         ret: syn!(ReturnType) >>
1544         where_clause: option!(syn!(WhereClause)) >>
1545         inner_attrs_stmts: braces!(tuple!(
1546             many0!(Attribute::parse_inner),
1547             call!(Block::parse_within),
1548         )) >>
1549         (ImplItemMethod {
1550             attrs: {
1551                 let mut attrs = outer_attrs;
1552                 attrs.extend((inner_attrs_stmts.1).0);
1553                 attrs
1554             },
1555             vis: vis,
1556             defaultness: defaultness,
1557             sig: MethodSig {
1558                 constness: constness,
1559                 unsafety: unsafety,
1560                 abi: abi,
1561                 ident: ident,
1562                 decl: FnDecl {
1563                     fn_token: fn_,
1564                     paren_token: inputs.0,
1565                     inputs: inputs.1,
1566                     output: ret,
1567                     generics: Generics {
1568                         where_clause: where_clause,
1569                         ..generics
1570                     },
1571                     variadic: None,
1572                 },
1573             },
1574             block: Block {
1575                 brace_token: inner_attrs_stmts.0,
1576                 stmts: (inner_attrs_stmts.1).1,
1577             },
1578         })
1579     ));
1580 
1581     named!(unstable_async_method -> ImplItemVerbatim, do_parse!(
1582         begin: call!(verbatim::grab_cursor) >>
1583         many0!(Attribute::parse_outer) >>
1584         syn!(Visibility) >>
1585         option!(keyword!(default)) >>
1586         option!(keyword!(const)) >>
1587         option!(keyword!(unsafe)) >>
1588         keyword!(async) >>
1589         option!(syn!(Abi)) >>
1590         keyword!(fn) >>
1591         syn!(Ident) >>
1592         syn!(Generics) >>
1593         parens!(Punctuated::<FnArg, Token![,]>::parse_terminated) >>
1594         syn!(ReturnType) >>
1595         option!(syn!(WhereClause)) >>
1596         braces!(tuple!(
1597             many0!(Attribute::parse_inner),
1598             call!(Block::parse_within),
1599         )) >>
1600         end: call!(verbatim::grab_cursor) >>
1601         (ImplItemVerbatim {
1602             tts: verbatim::token_range(begin..end),
1603         })
1604     ));
1605 
1606     impl_synom!(ImplItemType "type in impl block" do_parse!(
1607         attrs: many0!(Attribute::parse_outer) >>
1608         vis: syn!(Visibility) >>
1609         defaultness: option!(keyword!(default)) >>
1610         type_: keyword!(type) >>
1611         ident: syn!(Ident) >>
1612         generics: syn!(Generics) >>
1613         where_clause: option!(syn!(WhereClause)) >>
1614         eq: punct!(=) >>
1615         ty: syn!(Type) >>
1616         semi: punct!(;) >>
1617         (ImplItemType {
1618             attrs: attrs,
1619             vis: vis,
1620             defaultness: defaultness,
1621             type_token: type_,
1622             ident: ident,
1623             generics: Generics {
1624                 where_clause: where_clause,
1625                 ..generics
1626             },
1627             eq_token: eq,
1628             ty: ty,
1629             semi_token: semi,
1630         })
1631     ));
1632 
1633     named!(unstable_impl_existential_type -> ImplItemVerbatim, map!(
1634         call!(existential_type_helper, true),
1635         |tts| ImplItemVerbatim { tts: tts }
1636     ));
1637 
1638     impl_synom!(ImplItemMacro "macro in impl block" do_parse!(
1639         attrs: many0!(Attribute::parse_outer) >>
1640         mac: syn!(Macro) >>
1641         semi: cond!(!is_brace(&mac.delimiter), punct!(;)) >>
1642         (ImplItemMacro {
1643             attrs: attrs,
1644             mac: mac,
1645             semi_token: semi,
1646         })
1647     ));
1648 
is_brace(delimiter: &MacroDelimiter) -> bool1649     fn is_brace(delimiter: &MacroDelimiter) -> bool {
1650         match *delimiter {
1651             MacroDelimiter::Brace(_) => true,
1652             MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
1653         }
1654     }
1655 }
1656 
1657 #[cfg(feature = "printing")]
1658 mod printing {
1659     use super::*;
1660     use attr::FilterAttrs;
1661     use proc_macro2::TokenStream;
1662     use quote::{ToTokens, TokenStreamExt};
1663 
1664     impl ToTokens for ItemExternCrate {
to_tokens(&self, tokens: &mut TokenStream)1665         fn to_tokens(&self, tokens: &mut TokenStream) {
1666             tokens.append_all(self.attrs.outer());
1667             self.vis.to_tokens(tokens);
1668             self.extern_token.to_tokens(tokens);
1669             self.crate_token.to_tokens(tokens);
1670             self.ident.to_tokens(tokens);
1671             if let Some((ref as_token, ref rename)) = self.rename {
1672                 as_token.to_tokens(tokens);
1673                 rename.to_tokens(tokens);
1674             }
1675             self.semi_token.to_tokens(tokens);
1676         }
1677     }
1678 
1679     impl ToTokens for ItemUse {
to_tokens(&self, tokens: &mut TokenStream)1680         fn to_tokens(&self, tokens: &mut TokenStream) {
1681             tokens.append_all(self.attrs.outer());
1682             self.vis.to_tokens(tokens);
1683             self.use_token.to_tokens(tokens);
1684             self.leading_colon.to_tokens(tokens);
1685             self.tree.to_tokens(tokens);
1686             self.semi_token.to_tokens(tokens);
1687         }
1688     }
1689 
1690     impl ToTokens for ItemStatic {
to_tokens(&self, tokens: &mut TokenStream)1691         fn to_tokens(&self, tokens: &mut TokenStream) {
1692             tokens.append_all(self.attrs.outer());
1693             self.vis.to_tokens(tokens);
1694             self.static_token.to_tokens(tokens);
1695             self.mutability.to_tokens(tokens);
1696             self.ident.to_tokens(tokens);
1697             self.colon_token.to_tokens(tokens);
1698             self.ty.to_tokens(tokens);
1699             self.eq_token.to_tokens(tokens);
1700             self.expr.to_tokens(tokens);
1701             self.semi_token.to_tokens(tokens);
1702         }
1703     }
1704 
1705     impl ToTokens for ItemConst {
to_tokens(&self, tokens: &mut TokenStream)1706         fn to_tokens(&self, tokens: &mut TokenStream) {
1707             tokens.append_all(self.attrs.outer());
1708             self.vis.to_tokens(tokens);
1709             self.const_token.to_tokens(tokens);
1710             self.ident.to_tokens(tokens);
1711             self.colon_token.to_tokens(tokens);
1712             self.ty.to_tokens(tokens);
1713             self.eq_token.to_tokens(tokens);
1714             self.expr.to_tokens(tokens);
1715             self.semi_token.to_tokens(tokens);
1716         }
1717     }
1718 
1719     impl ToTokens for ItemFn {
to_tokens(&self, tokens: &mut TokenStream)1720         fn to_tokens(&self, tokens: &mut TokenStream) {
1721             tokens.append_all(self.attrs.outer());
1722             self.vis.to_tokens(tokens);
1723             self.constness.to_tokens(tokens);
1724             self.unsafety.to_tokens(tokens);
1725             self.abi.to_tokens(tokens);
1726             NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
1727             self.block.brace_token.surround(tokens, |tokens| {
1728                 tokens.append_all(self.attrs.inner());
1729                 tokens.append_all(&self.block.stmts);
1730             });
1731         }
1732     }
1733 
1734     impl ToTokens for ItemMod {
to_tokens(&self, tokens: &mut TokenStream)1735         fn to_tokens(&self, tokens: &mut TokenStream) {
1736             tokens.append_all(self.attrs.outer());
1737             self.vis.to_tokens(tokens);
1738             self.mod_token.to_tokens(tokens);
1739             self.ident.to_tokens(tokens);
1740             if let Some((ref brace, ref items)) = self.content {
1741                 brace.surround(tokens, |tokens| {
1742                     tokens.append_all(self.attrs.inner());
1743                     tokens.append_all(items);
1744                 });
1745             } else {
1746                 TokensOrDefault(&self.semi).to_tokens(tokens);
1747             }
1748         }
1749     }
1750 
1751     impl ToTokens for ItemForeignMod {
to_tokens(&self, tokens: &mut TokenStream)1752         fn to_tokens(&self, tokens: &mut TokenStream) {
1753             tokens.append_all(self.attrs.outer());
1754             self.abi.to_tokens(tokens);
1755             self.brace_token.surround(tokens, |tokens| {
1756                 tokens.append_all(self.attrs.inner());
1757                 tokens.append_all(&self.items);
1758             });
1759         }
1760     }
1761 
1762     impl ToTokens for ItemType {
to_tokens(&self, tokens: &mut TokenStream)1763         fn to_tokens(&self, tokens: &mut TokenStream) {
1764             tokens.append_all(self.attrs.outer());
1765             self.vis.to_tokens(tokens);
1766             self.type_token.to_tokens(tokens);
1767             self.ident.to_tokens(tokens);
1768             self.generics.to_tokens(tokens);
1769             self.generics.where_clause.to_tokens(tokens);
1770             self.eq_token.to_tokens(tokens);
1771             self.ty.to_tokens(tokens);
1772             self.semi_token.to_tokens(tokens);
1773         }
1774     }
1775 
1776     impl ToTokens for ItemEnum {
to_tokens(&self, tokens: &mut TokenStream)1777         fn to_tokens(&self, tokens: &mut TokenStream) {
1778             tokens.append_all(self.attrs.outer());
1779             self.vis.to_tokens(tokens);
1780             self.enum_token.to_tokens(tokens);
1781             self.ident.to_tokens(tokens);
1782             self.generics.to_tokens(tokens);
1783             self.generics.where_clause.to_tokens(tokens);
1784             self.brace_token.surround(tokens, |tokens| {
1785                 self.variants.to_tokens(tokens);
1786             });
1787         }
1788     }
1789 
1790     impl ToTokens for ItemStruct {
to_tokens(&self, tokens: &mut TokenStream)1791         fn to_tokens(&self, tokens: &mut TokenStream) {
1792             tokens.append_all(self.attrs.outer());
1793             self.vis.to_tokens(tokens);
1794             self.struct_token.to_tokens(tokens);
1795             self.ident.to_tokens(tokens);
1796             self.generics.to_tokens(tokens);
1797             match self.fields {
1798                 Fields::Named(ref fields) => {
1799                     self.generics.where_clause.to_tokens(tokens);
1800                     fields.to_tokens(tokens);
1801                 }
1802                 Fields::Unnamed(ref fields) => {
1803                     fields.to_tokens(tokens);
1804                     self.generics.where_clause.to_tokens(tokens);
1805                     TokensOrDefault(&self.semi_token).to_tokens(tokens);
1806                 }
1807                 Fields::Unit => {
1808                     self.generics.where_clause.to_tokens(tokens);
1809                     TokensOrDefault(&self.semi_token).to_tokens(tokens);
1810                 }
1811             }
1812         }
1813     }
1814 
1815     impl ToTokens for ItemUnion {
to_tokens(&self, tokens: &mut TokenStream)1816         fn to_tokens(&self, tokens: &mut TokenStream) {
1817             tokens.append_all(self.attrs.outer());
1818             self.vis.to_tokens(tokens);
1819             self.union_token.to_tokens(tokens);
1820             self.ident.to_tokens(tokens);
1821             self.generics.to_tokens(tokens);
1822             self.generics.where_clause.to_tokens(tokens);
1823             self.fields.to_tokens(tokens);
1824         }
1825     }
1826 
1827     impl ToTokens for ItemTrait {
to_tokens(&self, tokens: &mut TokenStream)1828         fn to_tokens(&self, tokens: &mut TokenStream) {
1829             tokens.append_all(self.attrs.outer());
1830             self.vis.to_tokens(tokens);
1831             self.unsafety.to_tokens(tokens);
1832             self.auto_token.to_tokens(tokens);
1833             self.trait_token.to_tokens(tokens);
1834             self.ident.to_tokens(tokens);
1835             self.generics.to_tokens(tokens);
1836             if !self.supertraits.is_empty() {
1837                 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1838                 self.supertraits.to_tokens(tokens);
1839             }
1840             self.generics.where_clause.to_tokens(tokens);
1841             self.brace_token.surround(tokens, |tokens| {
1842                 tokens.append_all(&self.items);
1843             });
1844         }
1845     }
1846 
1847     impl ToTokens for ItemImpl {
to_tokens(&self, tokens: &mut TokenStream)1848         fn to_tokens(&self, tokens: &mut TokenStream) {
1849             tokens.append_all(self.attrs.outer());
1850             self.defaultness.to_tokens(tokens);
1851             self.unsafety.to_tokens(tokens);
1852             self.impl_token.to_tokens(tokens);
1853             self.generics.to_tokens(tokens);
1854             if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
1855                 polarity.to_tokens(tokens);
1856                 path.to_tokens(tokens);
1857                 for_token.to_tokens(tokens);
1858             }
1859             self.self_ty.to_tokens(tokens);
1860             self.generics.where_clause.to_tokens(tokens);
1861             self.brace_token.surround(tokens, |tokens| {
1862                 tokens.append_all(self.attrs.inner());
1863                 tokens.append_all(&self.items);
1864             });
1865         }
1866     }
1867 
1868     impl ToTokens for ItemMacro {
to_tokens(&self, tokens: &mut TokenStream)1869         fn to_tokens(&self, tokens: &mut TokenStream) {
1870             tokens.append_all(self.attrs.outer());
1871             self.mac.path.to_tokens(tokens);
1872             self.mac.bang_token.to_tokens(tokens);
1873             self.ident.to_tokens(tokens);
1874             match self.mac.delimiter {
1875                 MacroDelimiter::Paren(ref paren) => {
1876                     paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1877                 }
1878                 MacroDelimiter::Brace(ref brace) => {
1879                     brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1880                 }
1881                 MacroDelimiter::Bracket(ref bracket) => {
1882                     bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
1883                 }
1884             }
1885             self.semi_token.to_tokens(tokens);
1886         }
1887     }
1888 
1889     impl ToTokens for ItemMacro2 {
to_tokens(&self, tokens: &mut TokenStream)1890         fn to_tokens(&self, tokens: &mut TokenStream) {
1891             tokens.append_all(self.attrs.outer());
1892             self.vis.to_tokens(tokens);
1893             self.macro_token.to_tokens(tokens);
1894             self.ident.to_tokens(tokens);
1895             self.paren_token.surround(tokens, |tokens| {
1896                 self.args.to_tokens(tokens);
1897             });
1898             self.brace_token.surround(tokens, |tokens| {
1899                 self.body.to_tokens(tokens);
1900             });
1901         }
1902     }
1903 
1904     impl ToTokens for ItemVerbatim {
to_tokens(&self, tokens: &mut TokenStream)1905         fn to_tokens(&self, tokens: &mut TokenStream) {
1906             self.tts.to_tokens(tokens);
1907         }
1908     }
1909 
1910     impl ToTokens for UsePath {
to_tokens(&self, tokens: &mut TokenStream)1911         fn to_tokens(&self, tokens: &mut TokenStream) {
1912             self.ident.to_tokens(tokens);
1913             self.colon2_token.to_tokens(tokens);
1914             self.tree.to_tokens(tokens);
1915         }
1916     }
1917 
1918     impl ToTokens for UseName {
to_tokens(&self, tokens: &mut TokenStream)1919         fn to_tokens(&self, tokens: &mut TokenStream) {
1920             self.ident.to_tokens(tokens);
1921         }
1922     }
1923 
1924     impl ToTokens for UseRename {
to_tokens(&self, tokens: &mut TokenStream)1925         fn to_tokens(&self, tokens: &mut TokenStream) {
1926             self.ident.to_tokens(tokens);
1927             self.as_token.to_tokens(tokens);
1928             self.rename.to_tokens(tokens);
1929         }
1930     }
1931 
1932     impl ToTokens for UseGlob {
to_tokens(&self, tokens: &mut TokenStream)1933         fn to_tokens(&self, tokens: &mut TokenStream) {
1934             self.star_token.to_tokens(tokens);
1935         }
1936     }
1937 
1938     impl ToTokens for UseGroup {
to_tokens(&self, tokens: &mut TokenStream)1939         fn to_tokens(&self, tokens: &mut TokenStream) {
1940             self.brace_token.surround(tokens, |tokens| {
1941                 self.items.to_tokens(tokens);
1942             });
1943         }
1944     }
1945 
1946     impl ToTokens for TraitItemConst {
to_tokens(&self, tokens: &mut TokenStream)1947         fn to_tokens(&self, tokens: &mut TokenStream) {
1948             tokens.append_all(self.attrs.outer());
1949             self.const_token.to_tokens(tokens);
1950             self.ident.to_tokens(tokens);
1951             self.colon_token.to_tokens(tokens);
1952             self.ty.to_tokens(tokens);
1953             if let Some((ref eq_token, ref default)) = self.default {
1954                 eq_token.to_tokens(tokens);
1955                 default.to_tokens(tokens);
1956             }
1957             self.semi_token.to_tokens(tokens);
1958         }
1959     }
1960 
1961     impl ToTokens for TraitItemMethod {
to_tokens(&self, tokens: &mut TokenStream)1962         fn to_tokens(&self, tokens: &mut TokenStream) {
1963             tokens.append_all(self.attrs.outer());
1964             self.sig.to_tokens(tokens);
1965             match self.default {
1966                 Some(ref block) => {
1967                     block.brace_token.surround(tokens, |tokens| {
1968                         tokens.append_all(self.attrs.inner());
1969                         tokens.append_all(&block.stmts);
1970                     });
1971                 }
1972                 None => {
1973                     TokensOrDefault(&self.semi_token).to_tokens(tokens);
1974                 }
1975             }
1976         }
1977     }
1978 
1979     impl ToTokens for TraitItemType {
to_tokens(&self, tokens: &mut TokenStream)1980         fn to_tokens(&self, tokens: &mut TokenStream) {
1981             tokens.append_all(self.attrs.outer());
1982             self.type_token.to_tokens(tokens);
1983             self.ident.to_tokens(tokens);
1984             self.generics.to_tokens(tokens);
1985             if !self.bounds.is_empty() {
1986                 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1987                 self.bounds.to_tokens(tokens);
1988             }
1989             self.generics.where_clause.to_tokens(tokens);
1990             if let Some((ref eq_token, ref default)) = self.default {
1991                 eq_token.to_tokens(tokens);
1992                 default.to_tokens(tokens);
1993             }
1994             self.semi_token.to_tokens(tokens);
1995         }
1996     }
1997 
1998     impl ToTokens for TraitItemMacro {
to_tokens(&self, tokens: &mut TokenStream)1999         fn to_tokens(&self, tokens: &mut TokenStream) {
2000             tokens.append_all(self.attrs.outer());
2001             self.mac.to_tokens(tokens);
2002             self.semi_token.to_tokens(tokens);
2003         }
2004     }
2005 
2006     impl ToTokens for TraitItemVerbatim {
to_tokens(&self, tokens: &mut TokenStream)2007         fn to_tokens(&self, tokens: &mut TokenStream) {
2008             self.tts.to_tokens(tokens);
2009         }
2010     }
2011 
2012     impl ToTokens for ImplItemConst {
to_tokens(&self, tokens: &mut TokenStream)2013         fn to_tokens(&self, tokens: &mut TokenStream) {
2014             tokens.append_all(self.attrs.outer());
2015             self.vis.to_tokens(tokens);
2016             self.defaultness.to_tokens(tokens);
2017             self.const_token.to_tokens(tokens);
2018             self.ident.to_tokens(tokens);
2019             self.colon_token.to_tokens(tokens);
2020             self.ty.to_tokens(tokens);
2021             self.eq_token.to_tokens(tokens);
2022             self.expr.to_tokens(tokens);
2023             self.semi_token.to_tokens(tokens);
2024         }
2025     }
2026 
2027     impl ToTokens for ImplItemMethod {
to_tokens(&self, tokens: &mut TokenStream)2028         fn to_tokens(&self, tokens: &mut TokenStream) {
2029             tokens.append_all(self.attrs.outer());
2030             self.vis.to_tokens(tokens);
2031             self.defaultness.to_tokens(tokens);
2032             self.sig.to_tokens(tokens);
2033             self.block.brace_token.surround(tokens, |tokens| {
2034                 tokens.append_all(self.attrs.inner());
2035                 tokens.append_all(&self.block.stmts);
2036             });
2037         }
2038     }
2039 
2040     impl ToTokens for ImplItemType {
to_tokens(&self, tokens: &mut TokenStream)2041         fn to_tokens(&self, tokens: &mut TokenStream) {
2042             tokens.append_all(self.attrs.outer());
2043             self.vis.to_tokens(tokens);
2044             self.defaultness.to_tokens(tokens);
2045             self.type_token.to_tokens(tokens);
2046             self.ident.to_tokens(tokens);
2047             self.generics.to_tokens(tokens);
2048             self.generics.where_clause.to_tokens(tokens);
2049             self.eq_token.to_tokens(tokens);
2050             self.ty.to_tokens(tokens);
2051             self.semi_token.to_tokens(tokens);
2052         }
2053     }
2054 
2055     impl ToTokens for ImplItemMacro {
to_tokens(&self, tokens: &mut TokenStream)2056         fn to_tokens(&self, tokens: &mut TokenStream) {
2057             tokens.append_all(self.attrs.outer());
2058             self.mac.to_tokens(tokens);
2059             self.semi_token.to_tokens(tokens);
2060         }
2061     }
2062 
2063     impl ToTokens for ImplItemVerbatim {
to_tokens(&self, tokens: &mut TokenStream)2064         fn to_tokens(&self, tokens: &mut TokenStream) {
2065             self.tts.to_tokens(tokens);
2066         }
2067     }
2068 
2069     impl ToTokens for ForeignItemFn {
to_tokens(&self, tokens: &mut TokenStream)2070         fn to_tokens(&self, tokens: &mut TokenStream) {
2071             tokens.append_all(self.attrs.outer());
2072             self.vis.to_tokens(tokens);
2073             NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2074             self.semi_token.to_tokens(tokens);
2075         }
2076     }
2077 
2078     impl ToTokens for ForeignItemStatic {
to_tokens(&self, tokens: &mut TokenStream)2079         fn to_tokens(&self, tokens: &mut TokenStream) {
2080             tokens.append_all(self.attrs.outer());
2081             self.vis.to_tokens(tokens);
2082             self.static_token.to_tokens(tokens);
2083             self.mutability.to_tokens(tokens);
2084             self.ident.to_tokens(tokens);
2085             self.colon_token.to_tokens(tokens);
2086             self.ty.to_tokens(tokens);
2087             self.semi_token.to_tokens(tokens);
2088         }
2089     }
2090 
2091     impl ToTokens for ForeignItemType {
to_tokens(&self, tokens: &mut TokenStream)2092         fn to_tokens(&self, tokens: &mut TokenStream) {
2093             tokens.append_all(self.attrs.outer());
2094             self.vis.to_tokens(tokens);
2095             self.type_token.to_tokens(tokens);
2096             self.ident.to_tokens(tokens);
2097             self.semi_token.to_tokens(tokens);
2098         }
2099     }
2100 
2101     impl ToTokens for ForeignItemVerbatim {
to_tokens(&self, tokens: &mut TokenStream)2102         fn to_tokens(&self, tokens: &mut TokenStream) {
2103             self.tts.to_tokens(tokens);
2104         }
2105     }
2106 
2107     impl ToTokens for MethodSig {
to_tokens(&self, tokens: &mut TokenStream)2108         fn to_tokens(&self, tokens: &mut TokenStream) {
2109             self.constness.to_tokens(tokens);
2110             self.unsafety.to_tokens(tokens);
2111             self.abi.to_tokens(tokens);
2112             NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2113         }
2114     }
2115 
2116     struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
2117 
2118     impl<'a> ToTokens for NamedDecl<'a> {
to_tokens(&self, tokens: &mut TokenStream)2119         fn to_tokens(&self, tokens: &mut TokenStream) {
2120             self.0.fn_token.to_tokens(tokens);
2121             self.1.to_tokens(tokens);
2122             self.0.generics.to_tokens(tokens);
2123             self.0.paren_token.surround(tokens, |tokens| {
2124                 self.0.inputs.to_tokens(tokens);
2125                 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2126                     <Token![,]>::default().to_tokens(tokens);
2127                 }
2128                 self.0.variadic.to_tokens(tokens);
2129             });
2130             self.0.output.to_tokens(tokens);
2131             self.0.generics.where_clause.to_tokens(tokens);
2132         }
2133     }
2134 
2135     impl ToTokens for ArgSelfRef {
to_tokens(&self, tokens: &mut TokenStream)2136         fn to_tokens(&self, tokens: &mut TokenStream) {
2137             self.and_token.to_tokens(tokens);
2138             self.lifetime.to_tokens(tokens);
2139             self.mutability.to_tokens(tokens);
2140             self.self_token.to_tokens(tokens);
2141         }
2142     }
2143 
2144     impl ToTokens for ArgSelf {
to_tokens(&self, tokens: &mut TokenStream)2145         fn to_tokens(&self, tokens: &mut TokenStream) {
2146             self.mutability.to_tokens(tokens);
2147             self.self_token.to_tokens(tokens);
2148         }
2149     }
2150 
2151     impl ToTokens for ArgCaptured {
to_tokens(&self, tokens: &mut TokenStream)2152         fn to_tokens(&self, tokens: &mut TokenStream) {
2153             self.pat.to_tokens(tokens);
2154             self.colon_token.to_tokens(tokens);
2155             self.ty.to_tokens(tokens);
2156         }
2157     }
2158 }
2159