]> git.proxmox.com Git - cargo.git/blob - vendor/syn/src/item.rs
New upstream version 0.33.0
[cargo.git] / vendor / syn / src / item.rs
1 use super::*;
2 use derive::{Data, DeriveInput};
3 use proc_macro2::TokenStream;
4 use punctuated::Punctuated;
5 use token::{Brace, Paren};
6
7 #[cfg(feature = "extra-traits")]
8 use std::hash::{Hash, Hasher};
9 #[cfg(feature = "extra-traits")]
10 use tt::TokenStreamHelper;
11
12 ast_enum_of_structs! {
13 /// Things that can appear directly inside of a module or scope.
14 ///
15 /// *This type is available if Syn is built with the `"full"` feature.*
16 ///
17 /// # Syntax tree enum
18 ///
19 /// This type is a [syntax tree enum].
20 ///
21 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
22 pub enum Item {
23 /// An `extern crate` item: `extern crate serde`.
24 ///
25 /// *This type is available if Syn is built with the `"full"` feature.*
26 pub ExternCrate(ItemExternCrate {
27 pub attrs: Vec<Attribute>,
28 pub vis: Visibility,
29 pub extern_token: Token![extern],
30 pub crate_token: Token![crate],
31 pub ident: Ident,
32 pub rename: Option<(Token![as], Ident)>,
33 pub semi_token: Token![;],
34 }),
35
36 /// A use declaration: `use std::collections::HashMap`.
37 ///
38 /// *This type is available if Syn is built with the `"full"` feature.*
39 pub Use(ItemUse {
40 pub attrs: Vec<Attribute>,
41 pub vis: Visibility,
42 pub use_token: Token![use],
43 pub leading_colon: Option<Token![::]>,
44 pub tree: UseTree,
45 pub semi_token: Token![;],
46 }),
47
48 /// A static item: `static BIKE: Shed = Shed(42)`.
49 ///
50 /// *This type is available if Syn is built with the `"full"` feature.*
51 pub Static(ItemStatic {
52 pub attrs: Vec<Attribute>,
53 pub vis: Visibility,
54 pub static_token: Token![static],
55 pub mutability: Option<Token![mut]>,
56 pub ident: Ident,
57 pub colon_token: Token![:],
58 pub ty: Box<Type>,
59 pub eq_token: Token![=],
60 pub expr: Box<Expr>,
61 pub semi_token: Token![;],
62 }),
63
64 /// A constant item: `const MAX: u16 = 65535`.
65 ///
66 /// *This type is available if Syn is built with the `"full"` feature.*
67 pub Const(ItemConst {
68 pub attrs: Vec<Attribute>,
69 pub vis: Visibility,
70 pub const_token: Token![const],
71 pub ident: Ident,
72 pub colon_token: Token![:],
73 pub ty: Box<Type>,
74 pub eq_token: Token![=],
75 pub expr: Box<Expr>,
76 pub semi_token: Token![;],
77 }),
78
79 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
80 /// }`.
81 ///
82 /// *This type is available if Syn is built with the `"full"` feature.*
83 pub Fn(ItemFn {
84 pub attrs: Vec<Attribute>,
85 pub vis: Visibility,
86 pub constness: Option<Token![const]>,
87 pub unsafety: Option<Token![unsafe]>,
88 pub asyncness: Option<Token![async]>,
89 pub abi: Option<Abi>,
90 pub ident: Ident,
91 pub decl: Box<FnDecl>,
92 pub block: Box<Block>,
93 }),
94
95 /// A module or module declaration: `mod m` or `mod m { ... }`.
96 ///
97 /// *This type is available if Syn is built with the `"full"` feature.*
98 pub Mod(ItemMod {
99 pub attrs: Vec<Attribute>,
100 pub vis: Visibility,
101 pub mod_token: Token![mod],
102 pub ident: Ident,
103 pub content: Option<(token::Brace, Vec<Item>)>,
104 pub semi: Option<Token![;]>,
105 }),
106
107 /// A block of foreign items: `extern "C" { ... }`.
108 ///
109 /// *This type is available if Syn is built with the `"full"` feature.*
110 pub ForeignMod(ItemForeignMod {
111 pub attrs: Vec<Attribute>,
112 pub abi: Abi,
113 pub brace_token: token::Brace,
114 pub items: Vec<ForeignItem>,
115 }),
116
117 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
118 ///
119 /// *This type is available if Syn is built with the `"full"` feature.*
120 pub Type(ItemType {
121 pub attrs: Vec<Attribute>,
122 pub vis: Visibility,
123 pub type_token: Token![type],
124 pub ident: Ident,
125 pub generics: Generics,
126 pub eq_token: Token![=],
127 pub ty: Box<Type>,
128 pub semi_token: Token![;],
129 }),
130
131 /// An existential type: `existential type Iter: Iterator<Item = u8>`.
132 ///
133 /// *This type is available if Syn is built with the `"full"` feature.*
134 pub Existential(ItemExistential {
135 pub attrs: Vec<Attribute>,
136 pub vis: Visibility,
137 pub existential_token: Token![existential],
138 pub type_token: Token![type],
139 pub ident: Ident,
140 pub generics: Generics,
141 pub colon_token: Option<Token![:]>,
142 pub bounds: Punctuated<TypeParamBound, Token![+]>,
143 pub semi_token: Token![;],
144 }),
145
146 /// A struct definition: `struct Foo<A> { x: A }`.
147 ///
148 /// *This type is available if Syn is built with the `"full"` feature.*
149 pub Struct(ItemStruct {
150 pub attrs: Vec<Attribute>,
151 pub vis: Visibility,
152 pub struct_token: Token![struct],
153 pub ident: Ident,
154 pub generics: Generics,
155 pub fields: Fields,
156 pub semi_token: Option<Token![;]>,
157 }),
158
159 /// An enum definition: `enum Foo<A, B> { C<A>, D<B> }`.
160 ///
161 /// *This type is available if Syn is built with the `"full"` feature.*
162 pub Enum(ItemEnum {
163 pub attrs: Vec<Attribute>,
164 pub vis: Visibility,
165 pub enum_token: Token![enum],
166 pub ident: Ident,
167 pub generics: Generics,
168 pub brace_token: token::Brace,
169 pub variants: Punctuated<Variant, Token![,]>,
170 }),
171
172 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
173 ///
174 /// *This type is available if Syn is built with the `"full"` feature.*
175 pub Union(ItemUnion {
176 pub attrs: Vec<Attribute>,
177 pub vis: Visibility,
178 pub union_token: Token![union],
179 pub ident: Ident,
180 pub generics: Generics,
181 pub fields: FieldsNamed,
182 }),
183
184 /// A trait definition: `pub trait Iterator { ... }`.
185 ///
186 /// *This type is available if Syn is built with the `"full"` feature.*
187 pub Trait(ItemTrait {
188 pub attrs: Vec<Attribute>,
189 pub vis: Visibility,
190 pub unsafety: Option<Token![unsafe]>,
191 pub auto_token: Option<Token![auto]>,
192 pub trait_token: Token![trait],
193 pub ident: Ident,
194 pub generics: Generics,
195 pub colon_token: Option<Token![:]>,
196 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
197 pub brace_token: token::Brace,
198 pub items: Vec<TraitItem>,
199 }),
200
201 /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.
202 ///
203 /// *This type is available if Syn is built with the `"full"` feature.*
204 pub TraitAlias(ItemTraitAlias {
205 pub attrs: Vec<Attribute>,
206 pub vis: Visibility,
207 pub trait_token: Token![trait],
208 pub ident: Ident,
209 pub generics: Generics,
210 pub eq_token: Token![=],
211 pub bounds: Punctuated<TypeParamBound, Token![+]>,
212 pub semi_token: Token![;],
213 }),
214
215 /// An impl block providing trait or associated items: `impl<A> Trait
216 /// for Data<A> { ... }`.
217 ///
218 /// *This type is available if Syn is built with the `"full"` feature.*
219 pub Impl(ItemImpl {
220 pub attrs: Vec<Attribute>,
221 pub defaultness: Option<Token![default]>,
222 pub unsafety: Option<Token![unsafe]>,
223 pub impl_token: Token![impl],
224 pub generics: Generics,
225 /// Trait this impl implements.
226 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
227 /// The Self type of the impl.
228 pub self_ty: Box<Type>,
229 pub brace_token: token::Brace,
230 pub items: Vec<ImplItem>,
231 }),
232
233 /// A macro invocation, which includes `macro_rules!` definitions.
234 ///
235 /// *This type is available if Syn is built with the `"full"` feature.*
236 pub Macro(ItemMacro {
237 pub attrs: Vec<Attribute>,
238 /// The `example` in `macro_rules! example { ... }`.
239 pub ident: Option<Ident>,
240 pub mac: Macro,
241 pub semi_token: Option<Token![;]>,
242 }),
243
244 /// A 2.0-style declarative macro introduced by the `macro` keyword.
245 ///
246 /// *This type is available if Syn is built with the `"full"` feature.*
247 pub Macro2(ItemMacro2 #manual_extra_traits {
248 pub attrs: Vec<Attribute>,
249 pub vis: Visibility,
250 pub macro_token: Token![macro],
251 pub ident: Ident,
252 pub paren_token: Paren,
253 pub args: TokenStream,
254 pub brace_token: Brace,
255 pub body: TokenStream,
256 }),
257
258 /// Tokens forming an item not interpreted by Syn.
259 ///
260 /// *This type is available if Syn is built with the `"full"` feature.*
261 pub Verbatim(ItemVerbatim #manual_extra_traits {
262 pub tts: TokenStream,
263 }),
264 }
265 }
266
267 #[cfg(feature = "extra-traits")]
268 impl Eq for ItemMacro2 {}
269
270 #[cfg(feature = "extra-traits")]
271 impl PartialEq for ItemMacro2 {
272 fn eq(&self, other: &Self) -> bool {
273 self.attrs == other.attrs
274 && self.vis == other.vis
275 && self.macro_token == other.macro_token
276 && self.ident == other.ident
277 && self.paren_token == other.paren_token
278 && TokenStreamHelper(&self.args) == TokenStreamHelper(&other.args)
279 && self.brace_token == other.brace_token
280 && TokenStreamHelper(&self.body) == TokenStreamHelper(&other.body)
281 }
282 }
283
284 #[cfg(feature = "extra-traits")]
285 impl Hash for ItemMacro2 {
286 fn hash<H>(&self, state: &mut H)
287 where
288 H: Hasher,
289 {
290 self.attrs.hash(state);
291 self.vis.hash(state);
292 self.macro_token.hash(state);
293 self.ident.hash(state);
294 self.paren_token.hash(state);
295 TokenStreamHelper(&self.args).hash(state);
296 self.brace_token.hash(state);
297 TokenStreamHelper(&self.body).hash(state);
298 }
299 }
300
301 #[cfg(feature = "extra-traits")]
302 impl Eq for ItemVerbatim {}
303
304 #[cfg(feature = "extra-traits")]
305 impl PartialEq for ItemVerbatim {
306 fn eq(&self, other: &Self) -> bool {
307 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
308 }
309 }
310
311 #[cfg(feature = "extra-traits")]
312 impl Hash for ItemVerbatim {
313 fn hash<H>(&self, state: &mut H)
314 where
315 H: Hasher,
316 {
317 TokenStreamHelper(&self.tts).hash(state);
318 }
319 }
320
321 impl From<DeriveInput> for Item {
322 fn from(input: DeriveInput) -> Item {
323 match input.data {
324 Data::Struct(data) => Item::Struct(ItemStruct {
325 attrs: input.attrs,
326 vis: input.vis,
327 struct_token: data.struct_token,
328 ident: input.ident,
329 generics: input.generics,
330 fields: data.fields,
331 semi_token: data.semi_token,
332 }),
333 Data::Enum(data) => Item::Enum(ItemEnum {
334 attrs: input.attrs,
335 vis: input.vis,
336 enum_token: data.enum_token,
337 ident: input.ident,
338 generics: input.generics,
339 brace_token: data.brace_token,
340 variants: data.variants,
341 }),
342 Data::Union(data) => Item::Union(ItemUnion {
343 attrs: input.attrs,
344 vis: input.vis,
345 union_token: data.union_token,
346 ident: input.ident,
347 generics: input.generics,
348 fields: data.fields,
349 }),
350 }
351 }
352 }
353
354 ast_enum_of_structs! {
355 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
356 ///
357 /// *This type is available if Syn is built with the `"full"` feature.*
358 ///
359 /// # Syntax tree enum
360 ///
361 /// This type is a [syntax tree enum].
362 ///
363 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
364 pub enum UseTree {
365 /// A path prefix of imports in a `use` item: `std::...`.
366 ///
367 /// *This type is available if Syn is built with the `"full"` feature.*
368 pub Path(UsePath {
369 pub ident: Ident,
370 pub colon2_token: Token![::],
371 pub tree: Box<UseTree>,
372 }),
373
374 /// An identifier imported by a `use` item: `HashMap`.
375 ///
376 /// *This type is available if Syn is built with the `"full"` feature.*
377 pub Name(UseName {
378 pub ident: Ident,
379 }),
380
381 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
382 ///
383 /// *This type is available if Syn is built with the `"full"` feature.*
384 pub Rename(UseRename {
385 pub ident: Ident,
386 pub as_token: Token![as],
387 pub rename: Ident,
388 }),
389
390 /// A glob import in a `use` item: `*`.
391 ///
392 /// *This type is available if Syn is built with the `"full"` feature.*
393 pub Glob(UseGlob {
394 pub star_token: Token![*],
395 }),
396
397 /// A braced group of imports in a `use` item: `{A, B, C}`.
398 ///
399 /// *This type is available if Syn is built with the `"full"` feature.*
400 pub Group(UseGroup {
401 pub brace_token: token::Brace,
402 pub items: Punctuated<UseTree, Token![,]>,
403 }),
404 }
405 }
406
407 ast_enum_of_structs! {
408 /// An item within an `extern` block.
409 ///
410 /// *This type is available if Syn is built with the `"full"` feature.*
411 ///
412 /// # Syntax tree enum
413 ///
414 /// This type is a [syntax tree enum].
415 ///
416 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
417 pub enum ForeignItem {
418 /// A foreign function in an `extern` block.
419 ///
420 /// *This type is available if Syn is built with the `"full"` feature.*
421 pub Fn(ForeignItemFn {
422 pub attrs: Vec<Attribute>,
423 pub vis: Visibility,
424 pub ident: Ident,
425 pub decl: Box<FnDecl>,
426 pub semi_token: Token![;],
427 }),
428
429 /// A foreign static item in an `extern` block: `static ext: u8`.
430 ///
431 /// *This type is available if Syn is built with the `"full"` feature.*
432 pub Static(ForeignItemStatic {
433 pub attrs: Vec<Attribute>,
434 pub vis: Visibility,
435 pub static_token: Token![static],
436 pub mutability: Option<Token![mut]>,
437 pub ident: Ident,
438 pub colon_token: Token![:],
439 pub ty: Box<Type>,
440 pub semi_token: Token![;],
441 }),
442
443 /// A foreign type in an `extern` block: `type void`.
444 ///
445 /// *This type is available if Syn is built with the `"full"` feature.*
446 pub Type(ForeignItemType {
447 pub attrs: Vec<Attribute>,
448 pub vis: Visibility,
449 pub type_token: Token![type],
450 pub ident: Ident,
451 pub semi_token: Token![;],
452 }),
453
454 /// A macro invocation within an extern block.
455 ///
456 /// *This type is available if Syn is built with the `"full"` feature.*
457 pub Macro(ForeignItemMacro {
458 pub attrs: Vec<Attribute>,
459 pub mac: Macro,
460 pub semi_token: Option<Token![;]>,
461 }),
462
463 /// Tokens in an `extern` block not interpreted by Syn.
464 ///
465 /// *This type is available if Syn is built with the `"full"` feature.*
466 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
467 pub tts: TokenStream,
468 }),
469 }
470 }
471
472 #[cfg(feature = "extra-traits")]
473 impl Eq for ForeignItemVerbatim {}
474
475 #[cfg(feature = "extra-traits")]
476 impl PartialEq for ForeignItemVerbatim {
477 fn eq(&self, other: &Self) -> bool {
478 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
479 }
480 }
481
482 #[cfg(feature = "extra-traits")]
483 impl Hash for ForeignItemVerbatim {
484 fn hash<H>(&self, state: &mut H)
485 where
486 H: Hasher,
487 {
488 TokenStreamHelper(&self.tts).hash(state);
489 }
490 }
491
492 ast_enum_of_structs! {
493 /// An item declaration within the definition of a trait.
494 ///
495 /// *This type is available if Syn is built with the `"full"` feature.*
496 ///
497 /// # Syntax tree enum
498 ///
499 /// This type is a [syntax tree enum].
500 ///
501 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
502 pub enum TraitItem {
503 /// An associated constant within the definition of a trait.
504 ///
505 /// *This type is available if Syn is built with the `"full"` feature.*
506 pub Const(TraitItemConst {
507 pub attrs: Vec<Attribute>,
508 pub const_token: Token![const],
509 pub ident: Ident,
510 pub colon_token: Token![:],
511 pub ty: Type,
512 pub default: Option<(Token![=], Expr)>,
513 pub semi_token: Token![;],
514 }),
515
516 /// A trait method within the definition of a trait.
517 ///
518 /// *This type is available if Syn is built with the `"full"` feature.*
519 pub Method(TraitItemMethod {
520 pub attrs: Vec<Attribute>,
521 pub sig: MethodSig,
522 pub default: Option<Block>,
523 pub semi_token: Option<Token![;]>,
524 }),
525
526 /// An associated type within the definition of a trait.
527 ///
528 /// *This type is available if Syn is built with the `"full"` feature.*
529 pub Type(TraitItemType {
530 pub attrs: Vec<Attribute>,
531 pub type_token: Token![type],
532 pub ident: Ident,
533 pub generics: Generics,
534 pub colon_token: Option<Token![:]>,
535 pub bounds: Punctuated<TypeParamBound, Token![+]>,
536 pub default: Option<(Token![=], Type)>,
537 pub semi_token: Token![;],
538 }),
539
540 /// A macro invocation within the definition of a trait.
541 ///
542 /// *This type is available if Syn is built with the `"full"` feature.*
543 pub Macro(TraitItemMacro {
544 pub attrs: Vec<Attribute>,
545 pub mac: Macro,
546 pub semi_token: Option<Token![;]>,
547 }),
548
549 /// Tokens within the definition of a trait not interpreted by Syn.
550 ///
551 /// *This type is available if Syn is built with the `"full"` feature.*
552 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
553 pub tts: TokenStream,
554 }),
555 }
556 }
557
558 #[cfg(feature = "extra-traits")]
559 impl Eq for TraitItemVerbatim {}
560
561 #[cfg(feature = "extra-traits")]
562 impl PartialEq for TraitItemVerbatim {
563 fn eq(&self, other: &Self) -> bool {
564 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
565 }
566 }
567
568 #[cfg(feature = "extra-traits")]
569 impl Hash for TraitItemVerbatim {
570 fn hash<H>(&self, state: &mut H)
571 where
572 H: Hasher,
573 {
574 TokenStreamHelper(&self.tts).hash(state);
575 }
576 }
577
578 ast_enum_of_structs! {
579 /// An item within an impl block.
580 ///
581 /// *This type is available if Syn is built with the `"full"` feature.*
582 ///
583 /// # Syntax tree enum
584 ///
585 /// This type is a [syntax tree enum].
586 ///
587 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
588 pub enum ImplItem {
589 /// An associated constant within an impl block.
590 ///
591 /// *This type is available if Syn is built with the `"full"` feature.*
592 pub Const(ImplItemConst {
593 pub attrs: Vec<Attribute>,
594 pub vis: Visibility,
595 pub defaultness: Option<Token![default]>,
596 pub const_token: Token![const],
597 pub ident: Ident,
598 pub colon_token: Token![:],
599 pub ty: Type,
600 pub eq_token: Token![=],
601 pub expr: Expr,
602 pub semi_token: Token![;],
603 }),
604
605 /// A method within an impl block.
606 ///
607 /// *This type is available if Syn is built with the `"full"` feature.*
608 pub Method(ImplItemMethod {
609 pub attrs: Vec<Attribute>,
610 pub vis: Visibility,
611 pub defaultness: Option<Token![default]>,
612 pub sig: MethodSig,
613 pub block: Block,
614 }),
615
616 /// An associated type within an impl block.
617 ///
618 /// *This type is available if Syn is built with the `"full"` feature.*
619 pub Type(ImplItemType {
620 pub attrs: Vec<Attribute>,
621 pub vis: Visibility,
622 pub defaultness: Option<Token![default]>,
623 pub type_token: Token![type],
624 pub ident: Ident,
625 pub generics: Generics,
626 pub eq_token: Token![=],
627 pub ty: Type,
628 pub semi_token: Token![;],
629 }),
630
631 /// An existential type within an impl block.
632 ///
633 /// *This type is available if Syn is built with the `"full"` feature.*
634 pub Existential(ImplItemExistential {
635 pub attrs: Vec<Attribute>,
636 pub existential_token: Token![existential],
637 pub type_token: Token![type],
638 pub ident: Ident,
639 pub generics: Generics,
640 pub colon_token: Option<Token![:]>,
641 pub bounds: Punctuated<TypeParamBound, Token![+]>,
642 pub semi_token: Token![;],
643 }),
644
645 /// A macro invocation within an impl block.
646 ///
647 /// *This type is available if Syn is built with the `"full"` feature.*
648 pub Macro(ImplItemMacro {
649 pub attrs: Vec<Attribute>,
650 pub mac: Macro,
651 pub semi_token: Option<Token![;]>,
652 }),
653
654 /// Tokens within an impl block not interpreted by Syn.
655 ///
656 /// *This type is available if Syn is built with the `"full"` feature.*
657 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
658 pub tts: TokenStream,
659 }),
660 }
661 }
662
663 #[cfg(feature = "extra-traits")]
664 impl Eq for ImplItemVerbatim {}
665
666 #[cfg(feature = "extra-traits")]
667 impl PartialEq for ImplItemVerbatim {
668 fn eq(&self, other: &Self) -> bool {
669 TokenStreamHelper(&self.tts) == TokenStreamHelper(&other.tts)
670 }
671 }
672
673 #[cfg(feature = "extra-traits")]
674 impl Hash for ImplItemVerbatim {
675 fn hash<H>(&self, state: &mut H)
676 where
677 H: Hasher,
678 {
679 TokenStreamHelper(&self.tts).hash(state);
680 }
681 }
682
683 ast_struct! {
684 /// A method's signature in a trait or implementation: `unsafe fn
685 /// initialize(&self)`.
686 ///
687 /// *This type is available if Syn is built with the `"full"` feature.*
688 pub struct MethodSig {
689 pub constness: Option<Token![const]>,
690 pub unsafety: Option<Token![unsafe]>,
691 pub asyncness: Option<Token![async]>,
692 pub abi: Option<Abi>,
693 pub ident: Ident,
694 pub decl: FnDecl,
695 }
696 }
697
698 ast_struct! {
699 /// Header of a function declaration, without including the body.
700 ///
701 /// *This type is available if Syn is built with the `"full"` feature.*
702 pub struct FnDecl {
703 pub fn_token: Token![fn],
704 pub generics: Generics,
705 pub paren_token: token::Paren,
706 pub inputs: Punctuated<FnArg, Token![,]>,
707 pub variadic: Option<Token![...]>,
708 pub output: ReturnType,
709 }
710 }
711
712 ast_enum_of_structs! {
713 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
714 ///
715 /// *This type is available if Syn is built with the `"full"` feature.*
716 ///
717 /// # Syntax tree enum
718 ///
719 /// This type is a [syntax tree enum].
720 ///
721 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
722 pub enum FnArg {
723 /// Self captured by reference in a function signature: `&self` or `&mut
724 /// self`.
725 ///
726 /// *This type is available if Syn is built with the `"full"` feature.*
727 pub SelfRef(ArgSelfRef {
728 pub and_token: Token![&],
729 pub lifetime: Option<Lifetime>,
730 pub mutability: Option<Token![mut]>,
731 pub self_token: Token![self],
732 }),
733
734 /// Self captured by value in a function signature: `self` or `mut
735 /// self`.
736 ///
737 /// *This type is available if Syn is built with the `"full"` feature.*
738 pub SelfValue(ArgSelf {
739 pub mutability: Option<Token![mut]>,
740 pub self_token: Token![self],
741 }),
742
743 /// An explicitly typed pattern captured by a function signature.
744 ///
745 /// *This type is available if Syn is built with the `"full"` feature.*
746 pub Captured(ArgCaptured {
747 pub pat: Pat,
748 pub colon_token: Token![:],
749 pub ty: Type,
750 }),
751
752 /// A pattern whose type is inferred captured by a function signature.
753 pub Inferred(Pat),
754 /// A type not bound to any pattern in a function signature.
755 pub Ignored(Type),
756 }
757 }
758
759 #[cfg(feature = "parsing")]
760 pub mod parsing {
761 use super::*;
762
763 use ext::IdentExt;
764 use parse::{Parse, ParseStream, Result};
765 use proc_macro2::{Punct, Spacing, TokenTree};
766 use std::iter::FromIterator;
767
768 impl Parse for Item {
769 fn parse(input: ParseStream) -> Result<Self> {
770 let ahead = input.fork();
771 ahead.call(Attribute::parse_outer)?;
772 let vis: Visibility = ahead.parse()?;
773
774 let lookahead = ahead.lookahead1();
775 if lookahead.peek(Token![extern]) {
776 ahead.parse::<Token![extern]>()?;
777 let lookahead = ahead.lookahead1();
778 if lookahead.peek(Token![crate]) {
779 input.parse().map(Item::ExternCrate)
780 } else if lookahead.peek(Token![fn]) {
781 input.parse().map(Item::Fn)
782 } else if lookahead.peek(token::Brace) {
783 input.parse().map(Item::ForeignMod)
784 } else if lookahead.peek(LitStr) {
785 ahead.parse::<LitStr>()?;
786 let lookahead = ahead.lookahead1();
787 if lookahead.peek(token::Brace) {
788 input.parse().map(Item::ForeignMod)
789 } else if lookahead.peek(Token![fn]) {
790 input.parse().map(Item::Fn)
791 } else {
792 Err(lookahead.error())
793 }
794 } else {
795 Err(lookahead.error())
796 }
797 } else if lookahead.peek(Token![use]) {
798 input.parse().map(Item::Use)
799 } else if lookahead.peek(Token![static]) {
800 input.parse().map(Item::Static)
801 } else if lookahead.peek(Token![const]) {
802 ahead.parse::<Token![const]>()?;
803 let lookahead = ahead.lookahead1();
804 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
805 input.parse().map(Item::Const)
806 } else if lookahead.peek(Token![unsafe])
807 || lookahead.peek(Token![async])
808 || lookahead.peek(Token![extern])
809 || lookahead.peek(Token![fn])
810 {
811 input.parse().map(Item::Fn)
812 } else {
813 Err(lookahead.error())
814 }
815 } else if lookahead.peek(Token![unsafe]) {
816 ahead.parse::<Token![unsafe]>()?;
817 let lookahead = ahead.lookahead1();
818 if lookahead.peek(Token![trait])
819 || lookahead.peek(Token![auto]) && ahead.peek2(Token![trait])
820 {
821 input.parse().map(Item::Trait)
822 } else if lookahead.peek(Token![impl ]) {
823 input.parse().map(Item::Impl)
824 } else if lookahead.peek(Token![async])
825 || lookahead.peek(Token![extern])
826 || lookahead.peek(Token![fn])
827 {
828 input.parse().map(Item::Fn)
829 } else {
830 Err(lookahead.error())
831 }
832 } else if lookahead.peek(Token![async]) || lookahead.peek(Token![fn]) {
833 input.parse().map(Item::Fn)
834 } else if lookahead.peek(Token![mod]) {
835 input.parse().map(Item::Mod)
836 } else if lookahead.peek(Token![type]) {
837 input.parse().map(Item::Type)
838 } else if lookahead.peek(Token![existential]) {
839 input.parse().map(Item::Existential)
840 } else if lookahead.peek(Token![struct]) {
841 input.parse().map(Item::Struct)
842 } else if lookahead.peek(Token![enum]) {
843 input.parse().map(Item::Enum)
844 } else if lookahead.peek(Token![union]) && ahead.peek2(Ident) {
845 input.parse().map(Item::Union)
846 } else if lookahead.peek(Token![trait]) {
847 input.call(parse_trait_or_trait_alias)
848 } else if lookahead.peek(Token![auto]) && ahead.peek2(Token![trait]) {
849 input.parse().map(Item::Trait)
850 } else if lookahead.peek(Token![impl ])
851 || lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
852 {
853 input.parse().map(Item::Impl)
854 } else if lookahead.peek(Token![macro]) {
855 input.parse().map(Item::Macro2)
856 } else if vis.is_inherited()
857 && (lookahead.peek(Ident)
858 || lookahead.peek(Token![self])
859 || lookahead.peek(Token![super])
860 || lookahead.peek(Token![extern])
861 || lookahead.peek(Token![crate])
862 || lookahead.peek(Token![::]))
863 {
864 input.parse().map(Item::Macro)
865 } else {
866 Err(lookahead.error())
867 }
868 }
869 }
870
871 impl Parse for ItemMacro {
872 fn parse(input: ParseStream) -> Result<Self> {
873 let attrs = input.call(Attribute::parse_outer)?;
874 let path = input.call(Path::parse_mod_style)?;
875 let bang_token: Token![!] = input.parse()?;
876 let ident: Option<Ident> = input.parse()?;
877 let (delimiter, tts) = input.call(mac::parse_delimiter)?;
878 let semi_token: Option<Token![;]> = if !delimiter.is_brace() {
879 Some(input.parse()?)
880 } else {
881 None
882 };
883 Ok(ItemMacro {
884 attrs: attrs,
885 ident: ident,
886 mac: Macro {
887 path: path,
888 bang_token: bang_token,
889 delimiter: delimiter,
890 tts: tts,
891 },
892 semi_token: semi_token,
893 })
894 }
895 }
896
897 // TODO: figure out the actual grammar; is body required to be braced?
898 impl Parse for ItemMacro2 {
899 fn parse(input: ParseStream) -> Result<Self> {
900 let attrs = input.call(Attribute::parse_outer)?;
901 let vis: Visibility = input.parse()?;
902 let macro_token: Token![macro] = input.parse()?;
903 let ident: Ident = input.parse()?;
904
905 let paren_token;
906 let args;
907 let brace_token;
908 let body;
909 let lookahead = input.lookahead1();
910 if lookahead.peek(token::Paren) {
911 let paren_content;
912 paren_token = parenthesized!(paren_content in input);
913 args = paren_content.parse()?;
914
915 let brace_content;
916 brace_token = braced!(brace_content in input);
917 body = brace_content.parse()?;
918 } else if lookahead.peek(token::Brace) {
919 // Hack: the ItemMacro2 syntax tree will need to change so that
920 // we can store None for the args.
921 //
922 // https://github.com/dtolnay/syn/issues/548
923 //
924 // For now, store some sentinel tokens that are otherwise
925 // illegal.
926 paren_token = token::Paren::default();
927 args = TokenStream::from_iter(vec![
928 TokenTree::Punct(Punct::new('$', Spacing::Alone)),
929 TokenTree::Punct(Punct::new('$', Spacing::Alone)),
930 ]);
931
932 let brace_content;
933 brace_token = braced!(brace_content in input);
934 body = brace_content.parse()?;
935 } else {
936 return Err(lookahead.error());
937 }
938
939 Ok(ItemMacro2 {
940 attrs: attrs,
941 vis: vis,
942 macro_token: macro_token,
943 ident: ident,
944 paren_token: paren_token,
945 args: args,
946 brace_token: brace_token,
947 body: body,
948 })
949 }
950 }
951
952 impl Parse for ItemExternCrate {
953 fn parse(input: ParseStream) -> Result<Self> {
954 Ok(ItemExternCrate {
955 attrs: input.call(Attribute::parse_outer)?,
956 vis: input.parse()?,
957 extern_token: input.parse()?,
958 crate_token: input.parse()?,
959 ident: {
960 if input.peek(Token![self]) {
961 input.call(Ident::parse_any)?
962 } else {
963 input.parse()?
964 }
965 },
966 rename: {
967 if input.peek(Token![as]) {
968 let as_token: Token![as] = input.parse()?;
969 let rename: Ident = input.parse()?;
970 Some((as_token, rename))
971 } else {
972 None
973 }
974 },
975 semi_token: input.parse()?,
976 })
977 }
978 }
979
980 impl Parse for ItemUse {
981 fn parse(input: ParseStream) -> Result<Self> {
982 Ok(ItemUse {
983 attrs: input.call(Attribute::parse_outer)?,
984 vis: input.parse()?,
985 use_token: input.parse()?,
986 leading_colon: input.parse()?,
987 tree: input.call(use_tree)?,
988 semi_token: input.parse()?,
989 })
990 }
991 }
992
993 fn use_tree(input: ParseStream) -> Result<UseTree> {
994 let lookahead = input.lookahead1();
995 if lookahead.peek(Ident)
996 || lookahead.peek(Token![self])
997 || lookahead.peek(Token![super])
998 || lookahead.peek(Token![crate])
999 || lookahead.peek(Token![extern])
1000 {
1001 let ident = input.call(Ident::parse_any)?;
1002 if input.peek(Token![::]) {
1003 Ok(UseTree::Path(UsePath {
1004 ident: ident,
1005 colon2_token: input.parse()?,
1006 tree: Box::new(input.call(use_tree)?),
1007 }))
1008 } else if input.peek(Token![as]) {
1009 Ok(UseTree::Rename(UseRename {
1010 ident: ident,
1011 as_token: input.parse()?,
1012 rename: {
1013 if input.peek(Ident) {
1014 input.parse()?
1015 } else if input.peek(Token![_]) {
1016 Ident::from(input.parse::<Token![_]>()?)
1017 } else {
1018 return Err(input.error("expected identifier or underscore"));
1019 }
1020 },
1021 }))
1022 } else {
1023 Ok(UseTree::Name(UseName { ident: ident }))
1024 }
1025 } else if lookahead.peek(Token![*]) {
1026 Ok(UseTree::Glob(UseGlob {
1027 star_token: input.parse()?,
1028 }))
1029 } else if lookahead.peek(token::Brace) {
1030 let content;
1031 Ok(UseTree::Group(UseGroup {
1032 brace_token: braced!(content in input),
1033 items: content.parse_terminated(use_tree)?,
1034 }))
1035 } else {
1036 Err(lookahead.error())
1037 }
1038 }
1039
1040 impl Parse for ItemStatic {
1041 fn parse(input: ParseStream) -> Result<Self> {
1042 Ok(ItemStatic {
1043 attrs: input.call(Attribute::parse_outer)?,
1044 vis: input.parse()?,
1045 static_token: input.parse()?,
1046 mutability: input.parse()?,
1047 ident: input.parse()?,
1048 colon_token: input.parse()?,
1049 ty: input.parse()?,
1050 eq_token: input.parse()?,
1051 expr: input.parse()?,
1052 semi_token: input.parse()?,
1053 })
1054 }
1055 }
1056
1057 impl Parse for ItemConst {
1058 fn parse(input: ParseStream) -> Result<Self> {
1059 Ok(ItemConst {
1060 attrs: input.call(Attribute::parse_outer)?,
1061 vis: input.parse()?,
1062 const_token: input.parse()?,
1063 ident: {
1064 let lookahead = input.lookahead1();
1065 if lookahead.peek(Ident) || lookahead.peek(Token![_]) {
1066 input.call(Ident::parse_any)?
1067 } else {
1068 return Err(lookahead.error());
1069 }
1070 },
1071 colon_token: input.parse()?,
1072 ty: input.parse()?,
1073 eq_token: input.parse()?,
1074 expr: input.parse()?,
1075 semi_token: input.parse()?,
1076 })
1077 }
1078 }
1079
1080 impl Parse for ItemFn {
1081 fn parse(input: ParseStream) -> Result<Self> {
1082 let outer_attrs = input.call(Attribute::parse_outer)?;
1083 let vis: Visibility = input.parse()?;
1084 let constness: Option<Token![const]> = input.parse()?;
1085 let unsafety: Option<Token![unsafe]> = input.parse()?;
1086 let asyncness: Option<Token![async]> = input.parse()?;
1087 let abi: Option<Abi> = input.parse()?;
1088 let fn_token: Token![fn] = input.parse()?;
1089 let ident: Ident = input.parse()?;
1090 let generics: Generics = input.parse()?;
1091
1092 let content;
1093 let paren_token = parenthesized!(content in input);
1094 let inputs = content.parse_terminated(FnArg::parse)?;
1095
1096 let output: ReturnType = input.parse()?;
1097 let where_clause: Option<WhereClause> = input.parse()?;
1098
1099 let content;
1100 let brace_token = braced!(content in input);
1101 let inner_attrs = content.call(Attribute::parse_inner)?;
1102 let stmts = content.call(Block::parse_within)?;
1103
1104 Ok(ItemFn {
1105 attrs: private::attrs(outer_attrs, inner_attrs),
1106 vis: vis,
1107 constness: constness,
1108 unsafety: unsafety,
1109 asyncness: asyncness,
1110 abi: abi,
1111 ident: ident,
1112 decl: Box::new(FnDecl {
1113 fn_token: fn_token,
1114 paren_token: paren_token,
1115 inputs: inputs,
1116 output: output,
1117 variadic: None,
1118 generics: Generics {
1119 where_clause: where_clause,
1120 ..generics
1121 },
1122 }),
1123 block: Box::new(Block {
1124 brace_token: brace_token,
1125 stmts: stmts,
1126 }),
1127 })
1128 }
1129 }
1130
1131 impl Parse for FnArg {
1132 fn parse(input: ParseStream) -> Result<Self> {
1133 if input.peek(Token![&]) {
1134 let ahead = input.fork();
1135 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1136 return input.call(arg_self_ref).map(FnArg::SelfRef);
1137 }
1138 }
1139
1140 if input.peek(Token![mut]) || input.peek(Token![self]) {
1141 let ahead = input.fork();
1142 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1143 return input.call(arg_self).map(FnArg::SelfValue);
1144 }
1145 }
1146
1147 let ahead = input.fork();
1148 let err = match ahead.call(arg_captured) {
1149 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
1150 Err(err) => err,
1151 };
1152
1153 let ahead = input.fork();
1154 if ahead.parse::<Type>().is_ok() {
1155 return input.parse().map(FnArg::Ignored);
1156 }
1157
1158 Err(err)
1159 }
1160 }
1161
1162 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1163 Ok(ArgSelfRef {
1164 and_token: input.parse()?,
1165 lifetime: input.parse()?,
1166 mutability: input.parse()?,
1167 self_token: input.parse()?,
1168 })
1169 }
1170
1171 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1172 Ok(ArgSelf {
1173 mutability: input.parse()?,
1174 self_token: input.parse()?,
1175 })
1176 }
1177
1178 fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1179 Ok(ArgCaptured {
1180 pat: input.parse()?,
1181 colon_token: input.parse()?,
1182 ty: input.parse()?,
1183 })
1184 }
1185
1186 impl Parse for ItemMod {
1187 fn parse(input: ParseStream) -> Result<Self> {
1188 let outer_attrs = input.call(Attribute::parse_outer)?;
1189 let vis: Visibility = input.parse()?;
1190 let mod_token: Token![mod] = input.parse()?;
1191 let ident: Ident = input.parse()?;
1192
1193 let lookahead = input.lookahead1();
1194 if lookahead.peek(Token![;]) {
1195 Ok(ItemMod {
1196 attrs: outer_attrs,
1197 vis: vis,
1198 mod_token: mod_token,
1199 ident: ident,
1200 content: None,
1201 semi: Some(input.parse()?),
1202 })
1203 } else if lookahead.peek(token::Brace) {
1204 let content;
1205 let brace_token = braced!(content in input);
1206 let inner_attrs = content.call(Attribute::parse_inner)?;
1207
1208 let mut items = Vec::new();
1209 while !content.is_empty() {
1210 items.push(content.parse()?);
1211 }
1212
1213 Ok(ItemMod {
1214 attrs: private::attrs(outer_attrs, inner_attrs),
1215 vis: vis,
1216 mod_token: mod_token,
1217 ident: ident,
1218 content: Some((brace_token, items)),
1219 semi: None,
1220 })
1221 } else {
1222 Err(lookahead.error())
1223 }
1224 }
1225 }
1226
1227 impl Parse for ItemForeignMod {
1228 fn parse(input: ParseStream) -> Result<Self> {
1229 let outer_attrs = input.call(Attribute::parse_outer)?;
1230 let abi: Abi = input.parse()?;
1231
1232 let content;
1233 let brace_token = braced!(content in input);
1234 let inner_attrs = content.call(Attribute::parse_inner)?;
1235 let mut items = Vec::new();
1236 while !content.is_empty() {
1237 items.push(content.parse()?);
1238 }
1239
1240 Ok(ItemForeignMod {
1241 attrs: private::attrs(outer_attrs, inner_attrs),
1242 abi: abi,
1243 brace_token: brace_token,
1244 items: items,
1245 })
1246 }
1247 }
1248
1249 impl Parse for ForeignItem {
1250 fn parse(input: ParseStream) -> Result<Self> {
1251 let ahead = input.fork();
1252 ahead.call(Attribute::parse_outer)?;
1253 let vis: Visibility = ahead.parse()?;
1254
1255 let lookahead = ahead.lookahead1();
1256 if lookahead.peek(Token![fn]) {
1257 input.parse().map(ForeignItem::Fn)
1258 } else if lookahead.peek(Token![static]) {
1259 input.parse().map(ForeignItem::Static)
1260 } else if lookahead.peek(Token![type]) {
1261 input.parse().map(ForeignItem::Type)
1262 } else if vis.is_inherited()
1263 && (lookahead.peek(Ident)
1264 || lookahead.peek(Token![self])
1265 || lookahead.peek(Token![super])
1266 || lookahead.peek(Token![extern])
1267 || lookahead.peek(Token![crate])
1268 || lookahead.peek(Token![::]))
1269 {
1270 input.parse().map(ForeignItem::Macro)
1271 } else {
1272 Err(lookahead.error())
1273 }
1274 }
1275 }
1276
1277 impl Parse for ForeignItemFn {
1278 fn parse(input: ParseStream) -> Result<Self> {
1279 let attrs = input.call(Attribute::parse_outer)?;
1280 let vis: Visibility = input.parse()?;
1281 let fn_token: Token![fn] = input.parse()?;
1282 let ident: Ident = input.parse()?;
1283 let generics: Generics = input.parse()?;
1284
1285 let content;
1286 let paren_token = parenthesized!(content in input);
1287 let mut inputs = Punctuated::new();
1288 while !content.is_empty() && !content.peek(Token![...]) {
1289 inputs.push_value(content.parse()?);
1290 if content.is_empty() {
1291 break;
1292 }
1293 inputs.push_punct(content.parse()?);
1294 }
1295 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1296 content.parse()?
1297 } else {
1298 None
1299 };
1300
1301 let output: ReturnType = input.parse()?;
1302 let where_clause: Option<WhereClause> = input.parse()?;
1303 let semi_token: Token![;] = input.parse()?;
1304
1305 Ok(ForeignItemFn {
1306 attrs: attrs,
1307 vis: vis,
1308 ident: ident,
1309 decl: Box::new(FnDecl {
1310 fn_token: fn_token,
1311 paren_token: paren_token,
1312 inputs: inputs,
1313 output: output,
1314 variadic: variadic,
1315 generics: Generics {
1316 where_clause: where_clause,
1317 ..generics
1318 },
1319 }),
1320 semi_token: semi_token,
1321 })
1322 }
1323 }
1324
1325 impl Parse for ForeignItemStatic {
1326 fn parse(input: ParseStream) -> Result<Self> {
1327 Ok(ForeignItemStatic {
1328 attrs: input.call(Attribute::parse_outer)?,
1329 vis: input.parse()?,
1330 static_token: input.parse()?,
1331 mutability: input.parse()?,
1332 ident: input.parse()?,
1333 colon_token: input.parse()?,
1334 ty: input.parse()?,
1335 semi_token: input.parse()?,
1336 })
1337 }
1338 }
1339
1340 impl Parse for ForeignItemType {
1341 fn parse(input: ParseStream) -> Result<Self> {
1342 Ok(ForeignItemType {
1343 attrs: input.call(Attribute::parse_outer)?,
1344 vis: input.parse()?,
1345 type_token: input.parse()?,
1346 ident: input.parse()?,
1347 semi_token: input.parse()?,
1348 })
1349 }
1350 }
1351
1352 impl Parse for ForeignItemMacro {
1353 fn parse(input: ParseStream) -> Result<Self> {
1354 let attrs = input.call(Attribute::parse_outer)?;
1355 let mac: Macro = input.parse()?;
1356 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1357 None
1358 } else {
1359 Some(input.parse()?)
1360 };
1361 Ok(ForeignItemMacro {
1362 attrs: attrs,
1363 mac: mac,
1364 semi_token: semi_token,
1365 })
1366 }
1367 }
1368
1369 impl Parse for ItemType {
1370 fn parse(input: ParseStream) -> Result<Self> {
1371 Ok(ItemType {
1372 attrs: input.call(Attribute::parse_outer)?,
1373 vis: input.parse()?,
1374 type_token: input.parse()?,
1375 ident: input.parse()?,
1376 generics: {
1377 let mut generics: Generics = input.parse()?;
1378 generics.where_clause = input.parse()?;
1379 generics
1380 },
1381 eq_token: input.parse()?,
1382 ty: input.parse()?,
1383 semi_token: input.parse()?,
1384 })
1385 }
1386 }
1387
1388 impl Parse for ItemExistential {
1389 fn parse(input: ParseStream) -> Result<Self> {
1390 Ok(ItemExistential {
1391 attrs: input.call(Attribute::parse_outer)?,
1392 vis: input.parse()?,
1393 existential_token: input.parse()?,
1394 type_token: input.parse()?,
1395 ident: input.parse()?,
1396 generics: {
1397 let mut generics: Generics = input.parse()?;
1398 generics.where_clause = input.parse()?;
1399 generics
1400 },
1401 colon_token: Some(input.parse()?),
1402 bounds: {
1403 let mut bounds = Punctuated::new();
1404 while !input.peek(Token![;]) {
1405 if !bounds.is_empty() {
1406 bounds.push_punct(input.parse()?);
1407 }
1408 bounds.push_value(input.parse()?);
1409 }
1410 bounds
1411 },
1412 semi_token: input.parse()?,
1413 })
1414 }
1415 }
1416
1417 impl Parse for ItemStruct {
1418 fn parse(input: ParseStream) -> Result<Self> {
1419 let attrs = input.call(Attribute::parse_outer)?;
1420 let vis = input.parse::<Visibility>()?;
1421 let struct_token = input.parse::<Token![struct]>()?;
1422 let ident = input.parse::<Ident>()?;
1423 let generics = input.parse::<Generics>()?;
1424 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1425 Ok(ItemStruct {
1426 attrs: attrs,
1427 vis: vis,
1428 struct_token: struct_token,
1429 ident: ident,
1430 generics: Generics {
1431 where_clause: where_clause,
1432 ..generics
1433 },
1434 fields: fields,
1435 semi_token: semi_token,
1436 })
1437 }
1438 }
1439
1440 impl Parse for ItemEnum {
1441 fn parse(input: ParseStream) -> Result<Self> {
1442 let attrs = input.call(Attribute::parse_outer)?;
1443 let vis = input.parse::<Visibility>()?;
1444 let enum_token = input.parse::<Token![enum]>()?;
1445 let ident = input.parse::<Ident>()?;
1446 let generics = input.parse::<Generics>()?;
1447 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1448 Ok(ItemEnum {
1449 attrs: attrs,
1450 vis: vis,
1451 enum_token: enum_token,
1452 ident: ident,
1453 generics: Generics {
1454 where_clause: where_clause,
1455 ..generics
1456 },
1457 brace_token: brace_token,
1458 variants: variants,
1459 })
1460 }
1461 }
1462
1463 impl Parse for ItemUnion {
1464 fn parse(input: ParseStream) -> Result<Self> {
1465 let attrs = input.call(Attribute::parse_outer)?;
1466 let vis = input.parse::<Visibility>()?;
1467 let union_token = input.parse::<Token![union]>()?;
1468 let ident = input.parse::<Ident>()?;
1469 let generics = input.parse::<Generics>()?;
1470 let (where_clause, fields) = derive::parsing::data_union(input)?;
1471 Ok(ItemUnion {
1472 attrs: attrs,
1473 vis: vis,
1474 union_token: union_token,
1475 ident: ident,
1476 generics: Generics {
1477 where_clause: where_clause,
1478 ..generics
1479 },
1480 fields: fields,
1481 })
1482 }
1483 }
1484
1485 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
1486 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1487 let lookahead = input.lookahead1();
1488 if lookahead.peek(token::Brace)
1489 || lookahead.peek(Token![:])
1490 || lookahead.peek(Token![where])
1491 {
1492 let unsafety = None;
1493 let auto_token = None;
1494 parse_rest_of_trait(
1495 input,
1496 attrs,
1497 vis,
1498 unsafety,
1499 auto_token,
1500 trait_token,
1501 ident,
1502 generics,
1503 )
1504 .map(Item::Trait)
1505 } else if lookahead.peek(Token![=]) {
1506 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
1507 .map(Item::TraitAlias)
1508 } else {
1509 Err(lookahead.error())
1510 }
1511 }
1512
1513 impl Parse for ItemTrait {
1514 fn parse(input: ParseStream) -> Result<Self> {
1515 let attrs = input.call(Attribute::parse_outer)?;
1516 let vis: Visibility = input.parse()?;
1517 let unsafety: Option<Token![unsafe]> = input.parse()?;
1518 let auto_token: Option<Token![auto]> = input.parse()?;
1519 let trait_token: Token![trait] = input.parse()?;
1520 let ident: Ident = input.parse()?;
1521 let generics: Generics = input.parse()?;
1522 parse_rest_of_trait(
1523 input,
1524 attrs,
1525 vis,
1526 unsafety,
1527 auto_token,
1528 trait_token,
1529 ident,
1530 generics,
1531 )
1532 }
1533 }
1534
1535 fn parse_rest_of_trait(
1536 input: ParseStream,
1537 attrs: Vec<Attribute>,
1538 vis: Visibility,
1539 unsafety: Option<Token![unsafe]>,
1540 auto_token: Option<Token![auto]>,
1541 trait_token: Token![trait],
1542 ident: Ident,
1543 mut generics: Generics,
1544 ) -> Result<ItemTrait> {
1545 let colon_token: Option<Token![:]> = input.parse()?;
1546
1547 let mut supertraits = Punctuated::new();
1548 if colon_token.is_some() {
1549 loop {
1550 supertraits.push_value(input.parse()?);
1551 if input.peek(Token![where]) || input.peek(token::Brace) {
1552 break;
1553 }
1554 supertraits.push_punct(input.parse()?);
1555 if input.peek(Token![where]) || input.peek(token::Brace) {
1556 break;
1557 }
1558 }
1559 }
1560
1561 generics.where_clause = input.parse()?;
1562
1563 let content;
1564 let brace_token = braced!(content in input);
1565 let mut items = Vec::new();
1566 while !content.is_empty() {
1567 items.push(content.parse()?);
1568 }
1569
1570 Ok(ItemTrait {
1571 attrs: attrs,
1572 vis: vis,
1573 unsafety: unsafety,
1574 auto_token: auto_token,
1575 trait_token: trait_token,
1576 ident: ident,
1577 generics: generics,
1578 colon_token: colon_token,
1579 supertraits: supertraits,
1580 brace_token: brace_token,
1581 items: items,
1582 })
1583 }
1584
1585 impl Parse for ItemTraitAlias {
1586 fn parse(input: ParseStream) -> Result<Self> {
1587 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1588 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
1589 }
1590 }
1591
1592 fn parse_start_of_trait_alias(
1593 input: ParseStream,
1594 ) -> Result<(Vec<Attribute>, Visibility, Token![trait], Ident, Generics)> {
1595 let attrs = input.call(Attribute::parse_outer)?;
1596 let vis: Visibility = input.parse()?;
1597 let trait_token: Token![trait] = input.parse()?;
1598 let ident: Ident = input.parse()?;
1599 let generics: Generics = input.parse()?;
1600 Ok((attrs, vis, trait_token, ident, generics))
1601 }
1602
1603 fn parse_rest_of_trait_alias(
1604 input: ParseStream,
1605 attrs: Vec<Attribute>,
1606 vis: Visibility,
1607 trait_token: Token![trait],
1608 ident: Ident,
1609 mut generics: Generics,
1610 ) -> Result<ItemTraitAlias> {
1611 let eq_token: Token![=] = input.parse()?;
1612
1613 let mut bounds = Punctuated::new();
1614 loop {
1615 if input.peek(Token![where]) || input.peek(Token![;]) {
1616 break;
1617 }
1618 bounds.push_value(input.parse()?);
1619 if input.peek(Token![where]) || input.peek(Token![;]) {
1620 break;
1621 }
1622 bounds.push_punct(input.parse()?);
1623 }
1624
1625 generics.where_clause = input.parse()?;
1626 let semi_token: Token![;] = input.parse()?;
1627
1628 Ok(ItemTraitAlias {
1629 attrs: attrs,
1630 vis: vis,
1631 trait_token: trait_token,
1632 ident: ident,
1633 generics: generics,
1634 eq_token: eq_token,
1635 bounds: bounds,
1636 semi_token: semi_token,
1637 })
1638 }
1639
1640 impl Parse for TraitItem {
1641 fn parse(input: ParseStream) -> Result<Self> {
1642 let ahead = input.fork();
1643 ahead.call(Attribute::parse_outer)?;
1644
1645 let lookahead = ahead.lookahead1();
1646 if lookahead.peek(Token![const]) {
1647 ahead.parse::<Token![const]>()?;
1648 let lookahead = ahead.lookahead1();
1649 if lookahead.peek(Ident) {
1650 input.parse().map(TraitItem::Const)
1651 } else if lookahead.peek(Token![unsafe])
1652 || lookahead.peek(Token![extern])
1653 || lookahead.peek(Token![fn])
1654 {
1655 input.parse().map(TraitItem::Method)
1656 } else {
1657 Err(lookahead.error())
1658 }
1659 } else if lookahead.peek(Token![unsafe])
1660 || lookahead.peek(Token![extern])
1661 || lookahead.peek(Token![fn])
1662 {
1663 input.parse().map(TraitItem::Method)
1664 } else if lookahead.peek(Token![type]) {
1665 input.parse().map(TraitItem::Type)
1666 } else if lookahead.peek(Ident)
1667 || lookahead.peek(Token![self])
1668 || lookahead.peek(Token![super])
1669 || lookahead.peek(Token![extern])
1670 || lookahead.peek(Token![crate])
1671 || lookahead.peek(Token![::])
1672 {
1673 input.parse().map(TraitItem::Macro)
1674 } else {
1675 Err(lookahead.error())
1676 }
1677 }
1678 }
1679
1680 impl Parse for TraitItemConst {
1681 fn parse(input: ParseStream) -> Result<Self> {
1682 Ok(TraitItemConst {
1683 attrs: input.call(Attribute::parse_outer)?,
1684 const_token: input.parse()?,
1685 ident: input.parse()?,
1686 colon_token: input.parse()?,
1687 ty: input.parse()?,
1688 default: {
1689 if input.peek(Token![=]) {
1690 let eq_token: Token![=] = input.parse()?;
1691 let default: Expr = input.parse()?;
1692 Some((eq_token, default))
1693 } else {
1694 None
1695 }
1696 },
1697 semi_token: input.parse()?,
1698 })
1699 }
1700 }
1701
1702 impl Parse for TraitItemMethod {
1703 fn parse(input: ParseStream) -> Result<Self> {
1704 let outer_attrs = input.call(Attribute::parse_outer)?;
1705 let constness: Option<Token![const]> = input.parse()?;
1706 let unsafety: Option<Token![unsafe]> = input.parse()?;
1707 let abi: Option<Abi> = input.parse()?;
1708 let fn_token: Token![fn] = input.parse()?;
1709 let ident: Ident = input.parse()?;
1710 let generics: Generics = input.parse()?;
1711
1712 let content;
1713 let paren_token = parenthesized!(content in input);
1714 let inputs = content.parse_terminated(FnArg::parse)?;
1715
1716 let output: ReturnType = input.parse()?;
1717 let where_clause: Option<WhereClause> = input.parse()?;
1718
1719 let lookahead = input.lookahead1();
1720 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1721 let content;
1722 let brace_token = braced!(content in input);
1723 let inner_attrs = content.call(Attribute::parse_inner)?;
1724 let stmts = content.call(Block::parse_within)?;
1725 (Some(brace_token), inner_attrs, stmts, None)
1726 } else if lookahead.peek(Token![;]) {
1727 let semi_token: Token![;] = input.parse()?;
1728 (None, Vec::new(), Vec::new(), Some(semi_token))
1729 } else {
1730 return Err(lookahead.error());
1731 };
1732
1733 Ok(TraitItemMethod {
1734 attrs: private::attrs(outer_attrs, inner_attrs),
1735 sig: MethodSig {
1736 constness: constness,
1737 unsafety: unsafety,
1738 asyncness: None,
1739 abi: abi,
1740 ident: ident,
1741 decl: FnDecl {
1742 fn_token: fn_token,
1743 paren_token: paren_token,
1744 inputs: inputs,
1745 output: output,
1746 variadic: None,
1747 generics: Generics {
1748 where_clause: where_clause,
1749 ..generics
1750 },
1751 },
1752 },
1753 default: brace_token.map(|brace_token| Block {
1754 brace_token: brace_token,
1755 stmts: stmts,
1756 }),
1757 semi_token: semi_token,
1758 })
1759 }
1760 }
1761
1762 impl Parse for TraitItemType {
1763 fn parse(input: ParseStream) -> Result<Self> {
1764 let attrs = input.call(Attribute::parse_outer)?;
1765 let type_token: Token![type] = input.parse()?;
1766 let ident: Ident = input.parse()?;
1767 let mut generics: Generics = input.parse()?;
1768 let colon_token: Option<Token![:]> = input.parse()?;
1769
1770 let mut bounds = Punctuated::new();
1771 if colon_token.is_some() {
1772 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1773 {
1774 if !bounds.is_empty() {
1775 bounds.push_punct(input.parse()?);
1776 }
1777 bounds.push_value(input.parse()?);
1778 }
1779 }
1780
1781 generics.where_clause = input.parse()?;
1782 let default = if input.peek(Token![=]) {
1783 let eq_token: Token![=] = input.parse()?;
1784 let default: Type = input.parse()?;
1785 Some((eq_token, default))
1786 } else {
1787 None
1788 };
1789 let semi_token: Token![;] = input.parse()?;
1790
1791 Ok(TraitItemType {
1792 attrs: attrs,
1793 type_token: type_token,
1794 ident: ident,
1795 generics: generics,
1796 colon_token: colon_token,
1797 bounds: bounds,
1798 default: default,
1799 semi_token: semi_token,
1800 })
1801 }
1802 }
1803
1804 impl Parse for TraitItemMacro {
1805 fn parse(input: ParseStream) -> Result<Self> {
1806 let attrs = input.call(Attribute::parse_outer)?;
1807 let mac: Macro = input.parse()?;
1808 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1809 None
1810 } else {
1811 Some(input.parse()?)
1812 };
1813 Ok(TraitItemMacro {
1814 attrs: attrs,
1815 mac: mac,
1816 semi_token: semi_token,
1817 })
1818 }
1819 }
1820
1821 impl Parse for ItemImpl {
1822 fn parse(input: ParseStream) -> Result<Self> {
1823 let outer_attrs = input.call(Attribute::parse_outer)?;
1824 let defaultness: Option<Token![default]> = input.parse()?;
1825 let unsafety: Option<Token![unsafe]> = input.parse()?;
1826 let impl_token: Token![impl ] = input.parse()?;
1827
1828 let has_generics = input.peek(Token![<])
1829 && (input.peek2(Token![>])
1830 || input.peek2(Token![#])
1831 || (input.peek2(Ident) || input.peek2(Lifetime))
1832 && (input.peek3(Token![:])
1833 || input.peek3(Token![,])
1834 || input.peek3(Token![>])));
1835 let generics: Generics = if has_generics {
1836 input.parse()?
1837 } else {
1838 Generics::default()
1839 };
1840
1841 let trait_ = {
1842 let ahead = input.fork();
1843 if ahead.parse::<Option<Token![!]>>().is_ok()
1844 && ahead.parse::<Path>().is_ok()
1845 && ahead.parse::<Token![for]>().is_ok()
1846 {
1847 let polarity: Option<Token![!]> = input.parse()?;
1848 let path: Path = input.parse()?;
1849 let for_token: Token![for] = input.parse()?;
1850 Some((polarity, path, for_token))
1851 } else {
1852 None
1853 }
1854 };
1855 let self_ty: Type = input.parse()?;
1856 let where_clause: Option<WhereClause> = input.parse()?;
1857
1858 let content;
1859 let brace_token = braced!(content in input);
1860 let inner_attrs = content.call(Attribute::parse_inner)?;
1861
1862 let mut items = Vec::new();
1863 while !content.is_empty() {
1864 items.push(content.parse()?);
1865 }
1866
1867 Ok(ItemImpl {
1868 attrs: private::attrs(outer_attrs, inner_attrs),
1869 defaultness: defaultness,
1870 unsafety: unsafety,
1871 impl_token: impl_token,
1872 generics: Generics {
1873 where_clause: where_clause,
1874 ..generics
1875 },
1876 trait_: trait_,
1877 self_ty: Box::new(self_ty),
1878 brace_token: brace_token,
1879 items: items,
1880 })
1881 }
1882 }
1883
1884 impl Parse for ImplItem {
1885 fn parse(input: ParseStream) -> Result<Self> {
1886 let ahead = input.fork();
1887 ahead.call(Attribute::parse_outer)?;
1888 let vis: Visibility = ahead.parse()?;
1889
1890 let mut lookahead = ahead.lookahead1();
1891 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1892 let defaultness: Token![default] = ahead.parse()?;
1893 lookahead = ahead.lookahead1();
1894 Some(defaultness)
1895 } else {
1896 None
1897 };
1898
1899 if lookahead.peek(Token![const]) {
1900 ahead.parse::<Token![const]>()?;
1901 let lookahead = ahead.lookahead1();
1902 if lookahead.peek(Ident) {
1903 input.parse().map(ImplItem::Const)
1904 } else if lookahead.peek(Token![unsafe])
1905 || lookahead.peek(Token![async])
1906 || lookahead.peek(Token![extern])
1907 || lookahead.peek(Token![fn])
1908 {
1909 input.parse().map(ImplItem::Method)
1910 } else {
1911 Err(lookahead.error())
1912 }
1913 } else if lookahead.peek(Token![unsafe])
1914 || lookahead.peek(Token![async])
1915 || lookahead.peek(Token![extern])
1916 || lookahead.peek(Token![fn])
1917 {
1918 input.parse().map(ImplItem::Method)
1919 } else if lookahead.peek(Token![type]) {
1920 input.parse().map(ImplItem::Type)
1921 } else if vis.is_inherited()
1922 && defaultness.is_none()
1923 && lookahead.peek(Token![existential])
1924 {
1925 input.parse().map(ImplItem::Existential)
1926 } else if vis.is_inherited()
1927 && defaultness.is_none()
1928 && (lookahead.peek(Ident)
1929 || lookahead.peek(Token![self])
1930 || lookahead.peek(Token![super])
1931 || lookahead.peek(Token![extern])
1932 || lookahead.peek(Token![crate])
1933 || lookahead.peek(Token![::]))
1934 {
1935 input.parse().map(ImplItem::Macro)
1936 } else {
1937 Err(lookahead.error())
1938 }
1939 }
1940 }
1941
1942 impl Parse for ImplItemConst {
1943 fn parse(input: ParseStream) -> Result<Self> {
1944 Ok(ImplItemConst {
1945 attrs: input.call(Attribute::parse_outer)?,
1946 vis: input.parse()?,
1947 defaultness: input.parse()?,
1948 const_token: input.parse()?,
1949 ident: input.parse()?,
1950 colon_token: input.parse()?,
1951 ty: input.parse()?,
1952 eq_token: input.parse()?,
1953 expr: input.parse()?,
1954 semi_token: input.parse()?,
1955 })
1956 }
1957 }
1958
1959 impl Parse for ImplItemMethod {
1960 fn parse(input: ParseStream) -> Result<Self> {
1961 let outer_attrs = input.call(Attribute::parse_outer)?;
1962 let vis: Visibility = input.parse()?;
1963 let defaultness: Option<Token![default]> = input.parse()?;
1964 let constness: Option<Token![const]> = input.parse()?;
1965 let unsafety: Option<Token![unsafe]> = input.parse()?;
1966 let asyncness: Option<Token![async]> = input.parse()?;
1967 let abi: Option<Abi> = input.parse()?;
1968 let fn_token: Token![fn] = input.parse()?;
1969 let ident: Ident = input.parse()?;
1970 let generics: Generics = input.parse()?;
1971
1972 let content;
1973 let paren_token = parenthesized!(content in input);
1974 let inputs = content.parse_terminated(FnArg::parse)?;
1975
1976 let output: ReturnType = input.parse()?;
1977 let where_clause: Option<WhereClause> = input.parse()?;
1978
1979 let content;
1980 let brace_token = braced!(content in input);
1981 let inner_attrs = content.call(Attribute::parse_inner)?;
1982 let stmts = content.call(Block::parse_within)?;
1983
1984 Ok(ImplItemMethod {
1985 attrs: private::attrs(outer_attrs, inner_attrs),
1986 vis: vis,
1987 defaultness: defaultness,
1988 sig: MethodSig {
1989 constness: constness,
1990 unsafety: unsafety,
1991 asyncness: asyncness,
1992 abi: abi,
1993 ident: ident,
1994 decl: FnDecl {
1995 fn_token: fn_token,
1996 paren_token: paren_token,
1997 inputs: inputs,
1998 output: output,
1999 variadic: None,
2000 generics: Generics {
2001 where_clause: where_clause,
2002 ..generics
2003 },
2004 },
2005 },
2006 block: Block {
2007 brace_token: brace_token,
2008 stmts: stmts,
2009 },
2010 })
2011 }
2012 }
2013
2014 impl Parse for ImplItemType {
2015 fn parse(input: ParseStream) -> Result<Self> {
2016 Ok(ImplItemType {
2017 attrs: input.call(Attribute::parse_outer)?,
2018 vis: input.parse()?,
2019 defaultness: input.parse()?,
2020 type_token: input.parse()?,
2021 ident: input.parse()?,
2022 generics: {
2023 let mut generics: Generics = input.parse()?;
2024 generics.where_clause = input.parse()?;
2025 generics
2026 },
2027 eq_token: input.parse()?,
2028 ty: input.parse()?,
2029 semi_token: input.parse()?,
2030 })
2031 }
2032 }
2033
2034 impl Parse for ImplItemExistential {
2035 fn parse(input: ParseStream) -> Result<Self> {
2036 let ety: ItemExistential = input.parse()?;
2037 Ok(ImplItemExistential {
2038 attrs: ety.attrs,
2039 existential_token: ety.existential_token,
2040 type_token: ety.type_token,
2041 ident: ety.ident,
2042 generics: ety.generics,
2043 colon_token: ety.colon_token,
2044 bounds: ety.bounds,
2045 semi_token: ety.semi_token,
2046 })
2047 }
2048 }
2049
2050 impl Parse for ImplItemMacro {
2051 fn parse(input: ParseStream) -> Result<Self> {
2052 let attrs = input.call(Attribute::parse_outer)?;
2053 let mac: Macro = input.parse()?;
2054 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
2055 None
2056 } else {
2057 Some(input.parse()?)
2058 };
2059 Ok(ImplItemMacro {
2060 attrs: attrs,
2061 mac: mac,
2062 semi_token: semi_token,
2063 })
2064 }
2065 }
2066
2067 impl Visibility {
2068 fn is_inherited(&self) -> bool {
2069 match *self {
2070 Visibility::Inherited => true,
2071 _ => false,
2072 }
2073 }
2074 }
2075
2076 impl MacroDelimiter {
2077 fn is_brace(&self) -> bool {
2078 match *self {
2079 MacroDelimiter::Brace(_) => true,
2080 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
2081 }
2082 }
2083 }
2084 }
2085
2086 #[cfg(feature = "printing")]
2087 mod printing {
2088 use super::*;
2089
2090 use proc_macro2::TokenStream;
2091 use quote::{ToTokens, TokenStreamExt};
2092
2093 use attr::FilterAttrs;
2094 use print::TokensOrDefault;
2095
2096 impl ToTokens for ItemExternCrate {
2097 fn to_tokens(&self, tokens: &mut TokenStream) {
2098 tokens.append_all(self.attrs.outer());
2099 self.vis.to_tokens(tokens);
2100 self.extern_token.to_tokens(tokens);
2101 self.crate_token.to_tokens(tokens);
2102 self.ident.to_tokens(tokens);
2103 if let Some((ref as_token, ref rename)) = self.rename {
2104 as_token.to_tokens(tokens);
2105 rename.to_tokens(tokens);
2106 }
2107 self.semi_token.to_tokens(tokens);
2108 }
2109 }
2110
2111 impl ToTokens for ItemUse {
2112 fn to_tokens(&self, tokens: &mut TokenStream) {
2113 tokens.append_all(self.attrs.outer());
2114 self.vis.to_tokens(tokens);
2115 self.use_token.to_tokens(tokens);
2116 self.leading_colon.to_tokens(tokens);
2117 self.tree.to_tokens(tokens);
2118 self.semi_token.to_tokens(tokens);
2119 }
2120 }
2121
2122 impl ToTokens for ItemStatic {
2123 fn to_tokens(&self, tokens: &mut TokenStream) {
2124 tokens.append_all(self.attrs.outer());
2125 self.vis.to_tokens(tokens);
2126 self.static_token.to_tokens(tokens);
2127 self.mutability.to_tokens(tokens);
2128 self.ident.to_tokens(tokens);
2129 self.colon_token.to_tokens(tokens);
2130 self.ty.to_tokens(tokens);
2131 self.eq_token.to_tokens(tokens);
2132 self.expr.to_tokens(tokens);
2133 self.semi_token.to_tokens(tokens);
2134 }
2135 }
2136
2137 impl ToTokens for ItemConst {
2138 fn to_tokens(&self, tokens: &mut TokenStream) {
2139 tokens.append_all(self.attrs.outer());
2140 self.vis.to_tokens(tokens);
2141 self.const_token.to_tokens(tokens);
2142 self.ident.to_tokens(tokens);
2143 self.colon_token.to_tokens(tokens);
2144 self.ty.to_tokens(tokens);
2145 self.eq_token.to_tokens(tokens);
2146 self.expr.to_tokens(tokens);
2147 self.semi_token.to_tokens(tokens);
2148 }
2149 }
2150
2151 impl ToTokens for ItemFn {
2152 fn to_tokens(&self, tokens: &mut TokenStream) {
2153 tokens.append_all(self.attrs.outer());
2154 self.vis.to_tokens(tokens);
2155 self.constness.to_tokens(tokens);
2156 self.unsafety.to_tokens(tokens);
2157 self.asyncness.to_tokens(tokens);
2158 self.abi.to_tokens(tokens);
2159 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2160 self.block.brace_token.surround(tokens, |tokens| {
2161 tokens.append_all(self.attrs.inner());
2162 tokens.append_all(&self.block.stmts);
2163 });
2164 }
2165 }
2166
2167 impl ToTokens for ItemMod {
2168 fn to_tokens(&self, tokens: &mut TokenStream) {
2169 tokens.append_all(self.attrs.outer());
2170 self.vis.to_tokens(tokens);
2171 self.mod_token.to_tokens(tokens);
2172 self.ident.to_tokens(tokens);
2173 if let Some((ref brace, ref items)) = self.content {
2174 brace.surround(tokens, |tokens| {
2175 tokens.append_all(self.attrs.inner());
2176 tokens.append_all(items);
2177 });
2178 } else {
2179 TokensOrDefault(&self.semi).to_tokens(tokens);
2180 }
2181 }
2182 }
2183
2184 impl ToTokens for ItemForeignMod {
2185 fn to_tokens(&self, tokens: &mut TokenStream) {
2186 tokens.append_all(self.attrs.outer());
2187 self.abi.to_tokens(tokens);
2188 self.brace_token.surround(tokens, |tokens| {
2189 tokens.append_all(self.attrs.inner());
2190 tokens.append_all(&self.items);
2191 });
2192 }
2193 }
2194
2195 impl ToTokens for ItemType {
2196 fn to_tokens(&self, tokens: &mut TokenStream) {
2197 tokens.append_all(self.attrs.outer());
2198 self.vis.to_tokens(tokens);
2199 self.type_token.to_tokens(tokens);
2200 self.ident.to_tokens(tokens);
2201 self.generics.to_tokens(tokens);
2202 self.generics.where_clause.to_tokens(tokens);
2203 self.eq_token.to_tokens(tokens);
2204 self.ty.to_tokens(tokens);
2205 self.semi_token.to_tokens(tokens);
2206 }
2207 }
2208
2209 impl ToTokens for ItemExistential {
2210 fn to_tokens(&self, tokens: &mut TokenStream) {
2211 tokens.append_all(self.attrs.outer());
2212 self.vis.to_tokens(tokens);
2213 self.existential_token.to_tokens(tokens);
2214 self.type_token.to_tokens(tokens);
2215 self.ident.to_tokens(tokens);
2216 self.generics.to_tokens(tokens);
2217 self.generics.where_clause.to_tokens(tokens);
2218 if !self.bounds.is_empty() {
2219 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2220 self.bounds.to_tokens(tokens);
2221 }
2222 self.semi_token.to_tokens(tokens);
2223 }
2224 }
2225
2226 impl ToTokens for ItemEnum {
2227 fn to_tokens(&self, tokens: &mut TokenStream) {
2228 tokens.append_all(self.attrs.outer());
2229 self.vis.to_tokens(tokens);
2230 self.enum_token.to_tokens(tokens);
2231 self.ident.to_tokens(tokens);
2232 self.generics.to_tokens(tokens);
2233 self.generics.where_clause.to_tokens(tokens);
2234 self.brace_token.surround(tokens, |tokens| {
2235 self.variants.to_tokens(tokens);
2236 });
2237 }
2238 }
2239
2240 impl ToTokens for ItemStruct {
2241 fn to_tokens(&self, tokens: &mut TokenStream) {
2242 tokens.append_all(self.attrs.outer());
2243 self.vis.to_tokens(tokens);
2244 self.struct_token.to_tokens(tokens);
2245 self.ident.to_tokens(tokens);
2246 self.generics.to_tokens(tokens);
2247 match self.fields {
2248 Fields::Named(ref fields) => {
2249 self.generics.where_clause.to_tokens(tokens);
2250 fields.to_tokens(tokens);
2251 }
2252 Fields::Unnamed(ref fields) => {
2253 fields.to_tokens(tokens);
2254 self.generics.where_clause.to_tokens(tokens);
2255 TokensOrDefault(&self.semi_token).to_tokens(tokens);
2256 }
2257 Fields::Unit => {
2258 self.generics.where_clause.to_tokens(tokens);
2259 TokensOrDefault(&self.semi_token).to_tokens(tokens);
2260 }
2261 }
2262 }
2263 }
2264
2265 impl ToTokens for ItemUnion {
2266 fn to_tokens(&self, tokens: &mut TokenStream) {
2267 tokens.append_all(self.attrs.outer());
2268 self.vis.to_tokens(tokens);
2269 self.union_token.to_tokens(tokens);
2270 self.ident.to_tokens(tokens);
2271 self.generics.to_tokens(tokens);
2272 self.generics.where_clause.to_tokens(tokens);
2273 self.fields.to_tokens(tokens);
2274 }
2275 }
2276
2277 impl ToTokens for ItemTrait {
2278 fn to_tokens(&self, tokens: &mut TokenStream) {
2279 tokens.append_all(self.attrs.outer());
2280 self.vis.to_tokens(tokens);
2281 self.unsafety.to_tokens(tokens);
2282 self.auto_token.to_tokens(tokens);
2283 self.trait_token.to_tokens(tokens);
2284 self.ident.to_tokens(tokens);
2285 self.generics.to_tokens(tokens);
2286 if !self.supertraits.is_empty() {
2287 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2288 self.supertraits.to_tokens(tokens);
2289 }
2290 self.generics.where_clause.to_tokens(tokens);
2291 self.brace_token.surround(tokens, |tokens| {
2292 tokens.append_all(&self.items);
2293 });
2294 }
2295 }
2296
2297 impl ToTokens for ItemTraitAlias {
2298 fn to_tokens(&self, tokens: &mut TokenStream) {
2299 tokens.append_all(self.attrs.outer());
2300 self.vis.to_tokens(tokens);
2301 self.trait_token.to_tokens(tokens);
2302 self.ident.to_tokens(tokens);
2303 self.generics.to_tokens(tokens);
2304 self.eq_token.to_tokens(tokens);
2305 self.bounds.to_tokens(tokens);
2306 self.generics.where_clause.to_tokens(tokens);
2307 self.semi_token.to_tokens(tokens);
2308 }
2309 }
2310
2311 impl ToTokens for ItemImpl {
2312 fn to_tokens(&self, tokens: &mut TokenStream) {
2313 tokens.append_all(self.attrs.outer());
2314 self.defaultness.to_tokens(tokens);
2315 self.unsafety.to_tokens(tokens);
2316 self.impl_token.to_tokens(tokens);
2317 self.generics.to_tokens(tokens);
2318 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2319 polarity.to_tokens(tokens);
2320 path.to_tokens(tokens);
2321 for_token.to_tokens(tokens);
2322 }
2323 self.self_ty.to_tokens(tokens);
2324 self.generics.where_clause.to_tokens(tokens);
2325 self.brace_token.surround(tokens, |tokens| {
2326 tokens.append_all(self.attrs.inner());
2327 tokens.append_all(&self.items);
2328 });
2329 }
2330 }
2331
2332 impl ToTokens for ItemMacro {
2333 fn to_tokens(&self, tokens: &mut TokenStream) {
2334 tokens.append_all(self.attrs.outer());
2335 self.mac.path.to_tokens(tokens);
2336 self.mac.bang_token.to_tokens(tokens);
2337 self.ident.to_tokens(tokens);
2338 match self.mac.delimiter {
2339 MacroDelimiter::Paren(ref paren) => {
2340 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2341 }
2342 MacroDelimiter::Brace(ref brace) => {
2343 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2344 }
2345 MacroDelimiter::Bracket(ref bracket) => {
2346 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2347 }
2348 }
2349 self.semi_token.to_tokens(tokens);
2350 }
2351 }
2352
2353 impl ToTokens for ItemMacro2 {
2354 fn to_tokens(&self, tokens: &mut TokenStream) {
2355 tokens.append_all(self.attrs.outer());
2356 self.vis.to_tokens(tokens);
2357 self.macro_token.to_tokens(tokens);
2358 self.ident.to_tokens(tokens);
2359
2360 // Hack: see comment in impl Parse for ItemMacro2.
2361 if self.args.to_string() != "$ $" {
2362 self.paren_token.surround(tokens, |tokens| {
2363 self.args.to_tokens(tokens);
2364 });
2365 }
2366
2367 self.brace_token.surround(tokens, |tokens| {
2368 self.body.to_tokens(tokens);
2369 });
2370 }
2371 }
2372
2373 impl ToTokens for ItemVerbatim {
2374 fn to_tokens(&self, tokens: &mut TokenStream) {
2375 self.tts.to_tokens(tokens);
2376 }
2377 }
2378
2379 impl ToTokens for UsePath {
2380 fn to_tokens(&self, tokens: &mut TokenStream) {
2381 self.ident.to_tokens(tokens);
2382 self.colon2_token.to_tokens(tokens);
2383 self.tree.to_tokens(tokens);
2384 }
2385 }
2386
2387 impl ToTokens for UseName {
2388 fn to_tokens(&self, tokens: &mut TokenStream) {
2389 self.ident.to_tokens(tokens);
2390 }
2391 }
2392
2393 impl ToTokens for UseRename {
2394 fn to_tokens(&self, tokens: &mut TokenStream) {
2395 self.ident.to_tokens(tokens);
2396 self.as_token.to_tokens(tokens);
2397 self.rename.to_tokens(tokens);
2398 }
2399 }
2400
2401 impl ToTokens for UseGlob {
2402 fn to_tokens(&self, tokens: &mut TokenStream) {
2403 self.star_token.to_tokens(tokens);
2404 }
2405 }
2406
2407 impl ToTokens for UseGroup {
2408 fn to_tokens(&self, tokens: &mut TokenStream) {
2409 self.brace_token.surround(tokens, |tokens| {
2410 self.items.to_tokens(tokens);
2411 });
2412 }
2413 }
2414
2415 impl ToTokens for TraitItemConst {
2416 fn to_tokens(&self, tokens: &mut TokenStream) {
2417 tokens.append_all(self.attrs.outer());
2418 self.const_token.to_tokens(tokens);
2419 self.ident.to_tokens(tokens);
2420 self.colon_token.to_tokens(tokens);
2421 self.ty.to_tokens(tokens);
2422 if let Some((ref eq_token, ref default)) = self.default {
2423 eq_token.to_tokens(tokens);
2424 default.to_tokens(tokens);
2425 }
2426 self.semi_token.to_tokens(tokens);
2427 }
2428 }
2429
2430 impl ToTokens for TraitItemMethod {
2431 fn to_tokens(&self, tokens: &mut TokenStream) {
2432 tokens.append_all(self.attrs.outer());
2433 self.sig.to_tokens(tokens);
2434 match self.default {
2435 Some(ref block) => {
2436 block.brace_token.surround(tokens, |tokens| {
2437 tokens.append_all(self.attrs.inner());
2438 tokens.append_all(&block.stmts);
2439 });
2440 }
2441 None => {
2442 TokensOrDefault(&self.semi_token).to_tokens(tokens);
2443 }
2444 }
2445 }
2446 }
2447
2448 impl ToTokens for TraitItemType {
2449 fn to_tokens(&self, tokens: &mut TokenStream) {
2450 tokens.append_all(self.attrs.outer());
2451 self.type_token.to_tokens(tokens);
2452 self.ident.to_tokens(tokens);
2453 self.generics.to_tokens(tokens);
2454 if !self.bounds.is_empty() {
2455 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2456 self.bounds.to_tokens(tokens);
2457 }
2458 self.generics.where_clause.to_tokens(tokens);
2459 if let Some((ref eq_token, ref default)) = self.default {
2460 eq_token.to_tokens(tokens);
2461 default.to_tokens(tokens);
2462 }
2463 self.semi_token.to_tokens(tokens);
2464 }
2465 }
2466
2467 impl ToTokens for TraitItemMacro {
2468 fn to_tokens(&self, tokens: &mut TokenStream) {
2469 tokens.append_all(self.attrs.outer());
2470 self.mac.to_tokens(tokens);
2471 self.semi_token.to_tokens(tokens);
2472 }
2473 }
2474
2475 impl ToTokens for TraitItemVerbatim {
2476 fn to_tokens(&self, tokens: &mut TokenStream) {
2477 self.tts.to_tokens(tokens);
2478 }
2479 }
2480
2481 impl ToTokens for ImplItemConst {
2482 fn to_tokens(&self, tokens: &mut TokenStream) {
2483 tokens.append_all(self.attrs.outer());
2484 self.vis.to_tokens(tokens);
2485 self.defaultness.to_tokens(tokens);
2486 self.const_token.to_tokens(tokens);
2487 self.ident.to_tokens(tokens);
2488 self.colon_token.to_tokens(tokens);
2489 self.ty.to_tokens(tokens);
2490 self.eq_token.to_tokens(tokens);
2491 self.expr.to_tokens(tokens);
2492 self.semi_token.to_tokens(tokens);
2493 }
2494 }
2495
2496 impl ToTokens for ImplItemMethod {
2497 fn to_tokens(&self, tokens: &mut TokenStream) {
2498 tokens.append_all(self.attrs.outer());
2499 self.vis.to_tokens(tokens);
2500 self.defaultness.to_tokens(tokens);
2501 self.sig.to_tokens(tokens);
2502 self.block.brace_token.surround(tokens, |tokens| {
2503 tokens.append_all(self.attrs.inner());
2504 tokens.append_all(&self.block.stmts);
2505 });
2506 }
2507 }
2508
2509 impl ToTokens for ImplItemType {
2510 fn to_tokens(&self, tokens: &mut TokenStream) {
2511 tokens.append_all(self.attrs.outer());
2512 self.vis.to_tokens(tokens);
2513 self.defaultness.to_tokens(tokens);
2514 self.type_token.to_tokens(tokens);
2515 self.ident.to_tokens(tokens);
2516 self.generics.to_tokens(tokens);
2517 self.generics.where_clause.to_tokens(tokens);
2518 self.eq_token.to_tokens(tokens);
2519 self.ty.to_tokens(tokens);
2520 self.semi_token.to_tokens(tokens);
2521 }
2522 }
2523
2524 impl ToTokens for ImplItemExistential {
2525 fn to_tokens(&self, tokens: &mut TokenStream) {
2526 tokens.append_all(self.attrs.outer());
2527 self.existential_token.to_tokens(tokens);
2528 self.type_token.to_tokens(tokens);
2529 self.ident.to_tokens(tokens);
2530 self.generics.to_tokens(tokens);
2531 self.generics.where_clause.to_tokens(tokens);
2532 if !self.bounds.is_empty() {
2533 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2534 self.bounds.to_tokens(tokens);
2535 }
2536 self.semi_token.to_tokens(tokens);
2537 }
2538 }
2539
2540 impl ToTokens for ImplItemMacro {
2541 fn to_tokens(&self, tokens: &mut TokenStream) {
2542 tokens.append_all(self.attrs.outer());
2543 self.mac.to_tokens(tokens);
2544 self.semi_token.to_tokens(tokens);
2545 }
2546 }
2547
2548 impl ToTokens for ImplItemVerbatim {
2549 fn to_tokens(&self, tokens: &mut TokenStream) {
2550 self.tts.to_tokens(tokens);
2551 }
2552 }
2553
2554 impl ToTokens for ForeignItemFn {
2555 fn to_tokens(&self, tokens: &mut TokenStream) {
2556 tokens.append_all(self.attrs.outer());
2557 self.vis.to_tokens(tokens);
2558 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2559 self.semi_token.to_tokens(tokens);
2560 }
2561 }
2562
2563 impl ToTokens for ForeignItemStatic {
2564 fn to_tokens(&self, tokens: &mut TokenStream) {
2565 tokens.append_all(self.attrs.outer());
2566 self.vis.to_tokens(tokens);
2567 self.static_token.to_tokens(tokens);
2568 self.mutability.to_tokens(tokens);
2569 self.ident.to_tokens(tokens);
2570 self.colon_token.to_tokens(tokens);
2571 self.ty.to_tokens(tokens);
2572 self.semi_token.to_tokens(tokens);
2573 }
2574 }
2575
2576 impl ToTokens for ForeignItemType {
2577 fn to_tokens(&self, tokens: &mut TokenStream) {
2578 tokens.append_all(self.attrs.outer());
2579 self.vis.to_tokens(tokens);
2580 self.type_token.to_tokens(tokens);
2581 self.ident.to_tokens(tokens);
2582 self.semi_token.to_tokens(tokens);
2583 }
2584 }
2585
2586 impl ToTokens for ForeignItemMacro {
2587 fn to_tokens(&self, tokens: &mut TokenStream) {
2588 tokens.append_all(self.attrs.outer());
2589 self.mac.to_tokens(tokens);
2590 self.semi_token.to_tokens(tokens);
2591 }
2592 }
2593
2594 impl ToTokens for ForeignItemVerbatim {
2595 fn to_tokens(&self, tokens: &mut TokenStream) {
2596 self.tts.to_tokens(tokens);
2597 }
2598 }
2599
2600 impl ToTokens for MethodSig {
2601 fn to_tokens(&self, tokens: &mut TokenStream) {
2602 self.constness.to_tokens(tokens);
2603 self.unsafety.to_tokens(tokens);
2604 self.asyncness.to_tokens(tokens);
2605 self.abi.to_tokens(tokens);
2606 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2607 }
2608 }
2609
2610 struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
2611
2612 impl<'a> ToTokens for NamedDecl<'a> {
2613 fn to_tokens(&self, tokens: &mut TokenStream) {
2614 self.0.fn_token.to_tokens(tokens);
2615 self.1.to_tokens(tokens);
2616 self.0.generics.to_tokens(tokens);
2617 self.0.paren_token.surround(tokens, |tokens| {
2618 self.0.inputs.to_tokens(tokens);
2619 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2620 <Token![,]>::default().to_tokens(tokens);
2621 }
2622 self.0.variadic.to_tokens(tokens);
2623 });
2624 self.0.output.to_tokens(tokens);
2625 self.0.generics.where_clause.to_tokens(tokens);
2626 }
2627 }
2628
2629 impl ToTokens for ArgSelfRef {
2630 fn to_tokens(&self, tokens: &mut TokenStream) {
2631 self.and_token.to_tokens(tokens);
2632 self.lifetime.to_tokens(tokens);
2633 self.mutability.to_tokens(tokens);
2634 self.self_token.to_tokens(tokens);
2635 }
2636 }
2637
2638 impl ToTokens for ArgSelf {
2639 fn to_tokens(&self, tokens: &mut TokenStream) {
2640 self.mutability.to_tokens(tokens);
2641 self.self_token.to_tokens(tokens);
2642 }
2643 }
2644
2645 impl ToTokens for ArgCaptured {
2646 fn to_tokens(&self, tokens: &mut TokenStream) {
2647 self.pat.to_tokens(tokens);
2648 self.colon_token.to_tokens(tokens);
2649 self.ty.to_tokens(tokens);
2650 }
2651 }
2652 }