1use super::*;
2use derive::{Data, DeriveInput};
3use proc_macro2::TokenStream;
4use punctuated::Punctuated;
5use token::{Brace, Paren};
6
7#[cfg(feature = "extra-traits")]
8use std::hash::{Hash, Hasher};
9#[cfg(feature = "extra-traits")]
10use tt::TokenStreamHelper;
11
12ast_enum_of_structs! {
13 pub enum Item {
23 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
227 pub self_ty: Box<Type>,
229 pub brace_token: token::Brace,
230 pub items: Vec<ImplItem>,
231 }),
232
233 pub Macro(ItemMacro {
237 pub attrs: Vec<Attribute>,
238 pub ident: Option<Ident>,
240 pub mac: Macro,
241 pub semi_token: Option<Token![;]>,
242 }),
243
244 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 pub Verbatim(ItemVerbatim #manual_extra_traits {
262 pub tts: TokenStream,
263 }),
264 }
265}
266
267#[cfg(feature = "extra-traits")]
268impl Eq for ItemMacro2 {}
269
270#[cfg(feature = "extra-traits")]
271impl 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")]
285impl 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")]
302impl Eq for ItemVerbatim {}
303
304#[cfg(feature = "extra-traits")]
305impl 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")]
312impl 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
321impl 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
354ast_enum_of_structs! {
355 pub enum UseTree {
365 pub Path(UsePath {
369 pub ident: Ident,
370 pub colon2_token: Token![::],
371 pub tree: Box<UseTree>,
372 }),
373
374 pub Name(UseName {
378 pub ident: Ident,
379 }),
380
381 pub Rename(UseRename {
385 pub ident: Ident,
386 pub as_token: Token![as],
387 pub rename: Ident,
388 }),
389
390 pub Glob(UseGlob {
394 pub star_token: Token![*],
395 }),
396
397 pub Group(UseGroup {
401 pub brace_token: token::Brace,
402 pub items: Punctuated<UseTree, Token![,]>,
403 }),
404 }
405}
406
407ast_enum_of_structs! {
408 pub enum ForeignItem {
418 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 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 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 pub Macro(ForeignItemMacro {
458 pub attrs: Vec<Attribute>,
459 pub mac: Macro,
460 pub semi_token: Option<Token![;]>,
461 }),
462
463 pub Verbatim(ForeignItemVerbatim #manual_extra_traits {
467 pub tts: TokenStream,
468 }),
469 }
470}
471
472#[cfg(feature = "extra-traits")]
473impl Eq for ForeignItemVerbatim {}
474
475#[cfg(feature = "extra-traits")]
476impl 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")]
483impl 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
492ast_enum_of_structs! {
493 pub enum TraitItem {
503 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 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 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 pub Macro(TraitItemMacro {
544 pub attrs: Vec<Attribute>,
545 pub mac: Macro,
546 pub semi_token: Option<Token![;]>,
547 }),
548
549 pub Verbatim(TraitItemVerbatim #manual_extra_traits {
553 pub tts: TokenStream,
554 }),
555 }
556}
557
558#[cfg(feature = "extra-traits")]
559impl Eq for TraitItemVerbatim {}
560
561#[cfg(feature = "extra-traits")]
562impl 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")]
569impl 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
578ast_enum_of_structs! {
579 pub enum ImplItem {
589 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 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 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 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 pub Macro(ImplItemMacro {
649 pub attrs: Vec<Attribute>,
650 pub mac: Macro,
651 pub semi_token: Option<Token![;]>,
652 }),
653
654 pub Verbatim(ImplItemVerbatim #manual_extra_traits {
658 pub tts: TokenStream,
659 }),
660 }
661}
662
663#[cfg(feature = "extra-traits")]
664impl Eq for ImplItemVerbatim {}
665
666#[cfg(feature = "extra-traits")]
667impl 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")]
674impl 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
683ast_struct! {
684 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
698ast_struct! {
699 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
712ast_enum_of_structs! {
713 pub enum FnArg {
723 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 pub SelfValue(ArgSelf {
739 pub mutability: Option<Token![mut]>,
740 pub self_token: Token![self],
741 }),
742
743 pub Captured(ArgCaptured {
747 pub pat: Pat,
748 pub colon_token: Token![:],
749 pub ty: Type,
750 }),
751
752 pub Inferred(Pat),
754 pub Ignored(Type),
756 }
757}
758
759#[cfg(feature = "parsing")]
760pub 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 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 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 let variadic: Option<Token![...]> = match inputs.last() {
1096 Some(punctuated::Pair::End(&FnArg::Captured(ArgCaptured {
1097 ty: Type::Verbatim(TypeVerbatim { ref tts }),
1098 ..
1099 }))) => parse2(tts.clone()).ok(),
1100 _ => None,
1101 };
1102
1103 let output: ReturnType = input.parse()?;
1104 let where_clause: Option<WhereClause> = input.parse()?;
1105
1106 let content;
1107 let brace_token = braced!(content in input);
1108 let inner_attrs = content.call(Attribute::parse_inner)?;
1109 let stmts = content.call(Block::parse_within)?;
1110
1111 Ok(ItemFn {
1112 attrs: private::attrs(outer_attrs, inner_attrs),
1113 vis: vis,
1114 constness: constness,
1115 unsafety: unsafety,
1116 asyncness: asyncness,
1117 abi: abi,
1118 ident: ident,
1119 decl: Box::new(FnDecl {
1120 fn_token: fn_token,
1121 paren_token: paren_token,
1122 inputs: inputs,
1123 output: output,
1124 variadic: variadic,
1125 generics: Generics {
1126 where_clause: where_clause,
1127 ..generics
1128 },
1129 }),
1130 block: Box::new(Block {
1131 brace_token: brace_token,
1132 stmts: stmts,
1133 }),
1134 })
1135 }
1136 }
1137
1138 impl Parse for FnArg {
1139 fn parse(input: ParseStream) -> Result<Self> {
1140 if input.peek(Token![&]) {
1141 let ahead = input.fork();
1142 if ahead.call(arg_self_ref).is_ok() && !ahead.peek(Token![:]) {
1143 return input.call(arg_self_ref).map(FnArg::SelfRef);
1144 }
1145 }
1146
1147 if input.peek(Token![mut]) || input.peek(Token![self]) {
1148 let ahead = input.fork();
1149 if ahead.call(arg_self).is_ok() && !ahead.peek(Token![:]) {
1150 return input.call(arg_self).map(FnArg::SelfValue);
1151 }
1152 }
1153
1154 let ahead = input.fork();
1155 let err = match ahead.call(arg_captured) {
1156 Ok(_) => return input.call(arg_captured).map(FnArg::Captured),
1157 Err(err) => err,
1158 };
1159
1160 let ahead = input.fork();
1161 if ahead.parse::<Type>().is_ok() {
1162 return input.parse().map(FnArg::Ignored);
1163 }
1164
1165 Err(err)
1166 }
1167 }
1168
1169 fn arg_self_ref(input: ParseStream) -> Result<ArgSelfRef> {
1170 Ok(ArgSelfRef {
1171 and_token: input.parse()?,
1172 lifetime: input.parse()?,
1173 mutability: input.parse()?,
1174 self_token: input.parse()?,
1175 })
1176 }
1177
1178 fn arg_self(input: ParseStream) -> Result<ArgSelf> {
1179 Ok(ArgSelf {
1180 mutability: input.parse()?,
1181 self_token: input.parse()?,
1182 })
1183 }
1184
1185 pub fn arg_captured(input: ParseStream) -> Result<ArgCaptured> {
1186 Ok(ArgCaptured {
1187 pat: input.parse()?,
1188 colon_token: input.parse()?,
1189 ty: match input.parse::<Token![...]>() {
1190 Ok(dot3) => {
1191 let mut args = vec![
1192 TokenTree::Punct(Punct::new('.', Spacing::Joint)),
1193 TokenTree::Punct(Punct::new('.', Spacing::Joint)),
1194 TokenTree::Punct(Punct::new('.', Spacing::Alone)),
1195 ];
1196 let tokens = TokenStream::from_iter(args.into_iter().zip(&dot3.spans).map(
1197 |(mut arg, span)| {
1198 arg.set_span(*span);
1199 arg
1200 },
1201 ));
1202 Type::Verbatim(TypeVerbatim { tts: tokens })
1203 }
1204 Err(_) => input.parse()?,
1205 },
1206 })
1207 }
1208
1209 impl Parse for ItemMod {
1210 fn parse(input: ParseStream) -> Result<Self> {
1211 let outer_attrs = input.call(Attribute::parse_outer)?;
1212 let vis: Visibility = input.parse()?;
1213 let mod_token: Token![mod] = input.parse()?;
1214 let ident: Ident = input.parse()?;
1215
1216 let lookahead = input.lookahead1();
1217 if lookahead.peek(Token![;]) {
1218 Ok(ItemMod {
1219 attrs: outer_attrs,
1220 vis: vis,
1221 mod_token: mod_token,
1222 ident: ident,
1223 content: None,
1224 semi: Some(input.parse()?),
1225 })
1226 } else if lookahead.peek(token::Brace) {
1227 let content;
1228 let brace_token = braced!(content in input);
1229 let inner_attrs = content.call(Attribute::parse_inner)?;
1230
1231 let mut items = Vec::new();
1232 while !content.is_empty() {
1233 items.push(content.parse()?);
1234 }
1235
1236 Ok(ItemMod {
1237 attrs: private::attrs(outer_attrs, inner_attrs),
1238 vis: vis,
1239 mod_token: mod_token,
1240 ident: ident,
1241 content: Some((brace_token, items)),
1242 semi: None,
1243 })
1244 } else {
1245 Err(lookahead.error())
1246 }
1247 }
1248 }
1249
1250 impl Parse for ItemForeignMod {
1251 fn parse(input: ParseStream) -> Result<Self> {
1252 let outer_attrs = input.call(Attribute::parse_outer)?;
1253 let abi: Abi = input.parse()?;
1254
1255 let content;
1256 let brace_token = braced!(content in input);
1257 let inner_attrs = content.call(Attribute::parse_inner)?;
1258 let mut items = Vec::new();
1259 while !content.is_empty() {
1260 items.push(content.parse()?);
1261 }
1262
1263 Ok(ItemForeignMod {
1264 attrs: private::attrs(outer_attrs, inner_attrs),
1265 abi: abi,
1266 brace_token: brace_token,
1267 items: items,
1268 })
1269 }
1270 }
1271
1272 impl Parse for ForeignItem {
1273 fn parse(input: ParseStream) -> Result<Self> {
1274 let ahead = input.fork();
1275 ahead.call(Attribute::parse_outer)?;
1276 let vis: Visibility = ahead.parse()?;
1277
1278 let lookahead = ahead.lookahead1();
1279 if lookahead.peek(Token![fn]) {
1280 input.parse().map(ForeignItem::Fn)
1281 } else if lookahead.peek(Token![static]) {
1282 input.parse().map(ForeignItem::Static)
1283 } else if lookahead.peek(Token![type]) {
1284 input.parse().map(ForeignItem::Type)
1285 } else if vis.is_inherited()
1286 && (lookahead.peek(Ident)
1287 || lookahead.peek(Token![self])
1288 || lookahead.peek(Token![super])
1289 || lookahead.peek(Token![extern])
1290 || lookahead.peek(Token![crate])
1291 || lookahead.peek(Token![::]))
1292 {
1293 input.parse().map(ForeignItem::Macro)
1294 } else {
1295 Err(lookahead.error())
1296 }
1297 }
1298 }
1299
1300 impl Parse for ForeignItemFn {
1301 fn parse(input: ParseStream) -> Result<Self> {
1302 let attrs = input.call(Attribute::parse_outer)?;
1303 let vis: Visibility = input.parse()?;
1304 let fn_token: Token![fn] = input.parse()?;
1305 let ident: Ident = input.parse()?;
1306 let generics: Generics = input.parse()?;
1307
1308 let content;
1309 let paren_token = parenthesized!(content in input);
1310 let mut inputs = Punctuated::new();
1311 while !content.is_empty() && !content.peek(Token![...]) {
1312 inputs.push_value(content.parse()?);
1313 if content.is_empty() {
1314 break;
1315 }
1316 inputs.push_punct(content.parse()?);
1317 }
1318 let variadic: Option<Token![...]> = if inputs.empty_or_trailing() {
1319 content.parse()?
1320 } else {
1321 None
1322 };
1323
1324 let output: ReturnType = input.parse()?;
1325 let where_clause: Option<WhereClause> = input.parse()?;
1326 let semi_token: Token![;] = input.parse()?;
1327
1328 Ok(ForeignItemFn {
1329 attrs: attrs,
1330 vis: vis,
1331 ident: ident,
1332 decl: Box::new(FnDecl {
1333 fn_token: fn_token,
1334 paren_token: paren_token,
1335 inputs: inputs,
1336 output: output,
1337 variadic: variadic,
1338 generics: Generics {
1339 where_clause: where_clause,
1340 ..generics
1341 },
1342 }),
1343 semi_token: semi_token,
1344 })
1345 }
1346 }
1347
1348 impl Parse for ForeignItemStatic {
1349 fn parse(input: ParseStream) -> Result<Self> {
1350 Ok(ForeignItemStatic {
1351 attrs: input.call(Attribute::parse_outer)?,
1352 vis: input.parse()?,
1353 static_token: input.parse()?,
1354 mutability: input.parse()?,
1355 ident: input.parse()?,
1356 colon_token: input.parse()?,
1357 ty: input.parse()?,
1358 semi_token: input.parse()?,
1359 })
1360 }
1361 }
1362
1363 impl Parse for ForeignItemType {
1364 fn parse(input: ParseStream) -> Result<Self> {
1365 Ok(ForeignItemType {
1366 attrs: input.call(Attribute::parse_outer)?,
1367 vis: input.parse()?,
1368 type_token: input.parse()?,
1369 ident: input.parse()?,
1370 semi_token: input.parse()?,
1371 })
1372 }
1373 }
1374
1375 impl Parse for ForeignItemMacro {
1376 fn parse(input: ParseStream) -> Result<Self> {
1377 let attrs = input.call(Attribute::parse_outer)?;
1378 let mac: Macro = input.parse()?;
1379 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1380 None
1381 } else {
1382 Some(input.parse()?)
1383 };
1384 Ok(ForeignItemMacro {
1385 attrs: attrs,
1386 mac: mac,
1387 semi_token: semi_token,
1388 })
1389 }
1390 }
1391
1392 impl Parse for ItemType {
1393 fn parse(input: ParseStream) -> Result<Self> {
1394 Ok(ItemType {
1395 attrs: input.call(Attribute::parse_outer)?,
1396 vis: input.parse()?,
1397 type_token: input.parse()?,
1398 ident: input.parse()?,
1399 generics: {
1400 let mut generics: Generics = input.parse()?;
1401 generics.where_clause = input.parse()?;
1402 generics
1403 },
1404 eq_token: input.parse()?,
1405 ty: input.parse()?,
1406 semi_token: input.parse()?,
1407 })
1408 }
1409 }
1410
1411 impl Parse for ItemExistential {
1412 fn parse(input: ParseStream) -> Result<Self> {
1413 Ok(ItemExistential {
1414 attrs: input.call(Attribute::parse_outer)?,
1415 vis: input.parse()?,
1416 existential_token: input.parse()?,
1417 type_token: input.parse()?,
1418 ident: input.parse()?,
1419 generics: {
1420 let mut generics: Generics = input.parse()?;
1421 generics.where_clause = input.parse()?;
1422 generics
1423 },
1424 colon_token: Some(input.parse()?),
1425 bounds: {
1426 let mut bounds = Punctuated::new();
1427 while !input.peek(Token![;]) {
1428 if !bounds.is_empty() {
1429 bounds.push_punct(input.parse()?);
1430 }
1431 bounds.push_value(input.parse()?);
1432 }
1433 bounds
1434 },
1435 semi_token: input.parse()?,
1436 })
1437 }
1438 }
1439
1440 impl Parse for ItemStruct {
1441 fn parse(input: ParseStream) -> Result<Self> {
1442 let attrs = input.call(Attribute::parse_outer)?;
1443 let vis = input.parse::<Visibility>()?;
1444 let struct_token = input.parse::<Token![struct]>()?;
1445 let ident = input.parse::<Ident>()?;
1446 let generics = input.parse::<Generics>()?;
1447 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
1448 Ok(ItemStruct {
1449 attrs: attrs,
1450 vis: vis,
1451 struct_token: struct_token,
1452 ident: ident,
1453 generics: Generics {
1454 where_clause: where_clause,
1455 ..generics
1456 },
1457 fields: fields,
1458 semi_token: semi_token,
1459 })
1460 }
1461 }
1462
1463 impl Parse for ItemEnum {
1464 fn parse(input: ParseStream) -> Result<Self> {
1465 let attrs = input.call(Attribute::parse_outer)?;
1466 let vis = input.parse::<Visibility>()?;
1467 let enum_token = input.parse::<Token![enum]>()?;
1468 let ident = input.parse::<Ident>()?;
1469 let generics = input.parse::<Generics>()?;
1470 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
1471 Ok(ItemEnum {
1472 attrs: attrs,
1473 vis: vis,
1474 enum_token: enum_token,
1475 ident: ident,
1476 generics: Generics {
1477 where_clause: where_clause,
1478 ..generics
1479 },
1480 brace_token: brace_token,
1481 variants: variants,
1482 })
1483 }
1484 }
1485
1486 impl Parse for ItemUnion {
1487 fn parse(input: ParseStream) -> Result<Self> {
1488 let attrs = input.call(Attribute::parse_outer)?;
1489 let vis = input.parse::<Visibility>()?;
1490 let union_token = input.parse::<Token![union]>()?;
1491 let ident = input.parse::<Ident>()?;
1492 let generics = input.parse::<Generics>()?;
1493 let (where_clause, fields) = derive::parsing::data_union(input)?;
1494 Ok(ItemUnion {
1495 attrs: attrs,
1496 vis: vis,
1497 union_token: union_token,
1498 ident: ident,
1499 generics: Generics {
1500 where_clause: where_clause,
1501 ..generics
1502 },
1503 fields: fields,
1504 })
1505 }
1506 }
1507
1508 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
1509 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1510 let lookahead = input.lookahead1();
1511 if lookahead.peek(token::Brace)
1512 || lookahead.peek(Token![:])
1513 || lookahead.peek(Token![where])
1514 {
1515 let unsafety = None;
1516 let auto_token = None;
1517 parse_rest_of_trait(
1518 input,
1519 attrs,
1520 vis,
1521 unsafety,
1522 auto_token,
1523 trait_token,
1524 ident,
1525 generics,
1526 )
1527 .map(Item::Trait)
1528 } else if lookahead.peek(Token![=]) {
1529 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
1530 .map(Item::TraitAlias)
1531 } else {
1532 Err(lookahead.error())
1533 }
1534 }
1535
1536 impl Parse for ItemTrait {
1537 fn parse(input: ParseStream) -> Result<Self> {
1538 let attrs = input.call(Attribute::parse_outer)?;
1539 let vis: Visibility = input.parse()?;
1540 let unsafety: Option<Token![unsafe]> = input.parse()?;
1541 let auto_token: Option<Token![auto]> = input.parse()?;
1542 let trait_token: Token![trait] = input.parse()?;
1543 let ident: Ident = input.parse()?;
1544 let generics: Generics = input.parse()?;
1545 parse_rest_of_trait(
1546 input,
1547 attrs,
1548 vis,
1549 unsafety,
1550 auto_token,
1551 trait_token,
1552 ident,
1553 generics,
1554 )
1555 }
1556 }
1557
1558 fn parse_rest_of_trait(
1559 input: ParseStream,
1560 attrs: Vec<Attribute>,
1561 vis: Visibility,
1562 unsafety: Option<Token![unsafe]>,
1563 auto_token: Option<Token![auto]>,
1564 trait_token: Token![trait],
1565 ident: Ident,
1566 mut generics: Generics,
1567 ) -> Result<ItemTrait> {
1568 let colon_token: Option<Token![:]> = input.parse()?;
1569
1570 let mut supertraits = Punctuated::new();
1571 if colon_token.is_some() {
1572 loop {
1573 supertraits.push_value(input.parse()?);
1574 if input.peek(Token![where]) || input.peek(token::Brace) {
1575 break;
1576 }
1577 supertraits.push_punct(input.parse()?);
1578 if input.peek(Token![where]) || input.peek(token::Brace) {
1579 break;
1580 }
1581 }
1582 }
1583
1584 generics.where_clause = input.parse()?;
1585
1586 let content;
1587 let brace_token = braced!(content in input);
1588 let mut items = Vec::new();
1589 while !content.is_empty() {
1590 items.push(content.parse()?);
1591 }
1592
1593 Ok(ItemTrait {
1594 attrs: attrs,
1595 vis: vis,
1596 unsafety: unsafety,
1597 auto_token: auto_token,
1598 trait_token: trait_token,
1599 ident: ident,
1600 generics: generics,
1601 colon_token: colon_token,
1602 supertraits: supertraits,
1603 brace_token: brace_token,
1604 items: items,
1605 })
1606 }
1607
1608 impl Parse for ItemTraitAlias {
1609 fn parse(input: ParseStream) -> Result<Self> {
1610 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
1611 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
1612 }
1613 }
1614
1615 fn parse_start_of_trait_alias(
1616 input: ParseStream,
1617 ) -> Result<(Vec<Attribute>, Visibility, Token![trait], Ident, Generics)> {
1618 let attrs = input.call(Attribute::parse_outer)?;
1619 let vis: Visibility = input.parse()?;
1620 let trait_token: Token![trait] = input.parse()?;
1621 let ident: Ident = input.parse()?;
1622 let generics: Generics = input.parse()?;
1623 Ok((attrs, vis, trait_token, ident, generics))
1624 }
1625
1626 fn parse_rest_of_trait_alias(
1627 input: ParseStream,
1628 attrs: Vec<Attribute>,
1629 vis: Visibility,
1630 trait_token: Token![trait],
1631 ident: Ident,
1632 mut generics: Generics,
1633 ) -> Result<ItemTraitAlias> {
1634 let eq_token: Token![=] = input.parse()?;
1635
1636 let mut bounds = Punctuated::new();
1637 loop {
1638 if input.peek(Token![where]) || input.peek(Token![;]) {
1639 break;
1640 }
1641 bounds.push_value(input.parse()?);
1642 if input.peek(Token![where]) || input.peek(Token![;]) {
1643 break;
1644 }
1645 bounds.push_punct(input.parse()?);
1646 }
1647
1648 generics.where_clause = input.parse()?;
1649 let semi_token: Token![;] = input.parse()?;
1650
1651 Ok(ItemTraitAlias {
1652 attrs: attrs,
1653 vis: vis,
1654 trait_token: trait_token,
1655 ident: ident,
1656 generics: generics,
1657 eq_token: eq_token,
1658 bounds: bounds,
1659 semi_token: semi_token,
1660 })
1661 }
1662
1663 impl Parse for TraitItem {
1664 fn parse(input: ParseStream) -> Result<Self> {
1665 let ahead = input.fork();
1666 ahead.call(Attribute::parse_outer)?;
1667
1668 let lookahead = ahead.lookahead1();
1669 if lookahead.peek(Token![const]) {
1670 ahead.parse::<Token![const]>()?;
1671 let lookahead = ahead.lookahead1();
1672 if lookahead.peek(Ident) {
1673 input.parse().map(TraitItem::Const)
1674 } else if lookahead.peek(Token![unsafe])
1675 || lookahead.peek(Token![extern])
1676 || lookahead.peek(Token![fn])
1677 {
1678 input.parse().map(TraitItem::Method)
1679 } else {
1680 Err(lookahead.error())
1681 }
1682 } else if lookahead.peek(Token![unsafe])
1683 || lookahead.peek(Token![extern])
1684 || lookahead.peek(Token![fn])
1685 {
1686 input.parse().map(TraitItem::Method)
1687 } else if lookahead.peek(Token![type]) {
1688 input.parse().map(TraitItem::Type)
1689 } else if lookahead.peek(Ident)
1690 || lookahead.peek(Token![self])
1691 || lookahead.peek(Token![super])
1692 || lookahead.peek(Token![extern])
1693 || lookahead.peek(Token![crate])
1694 || lookahead.peek(Token![::])
1695 {
1696 input.parse().map(TraitItem::Macro)
1697 } else {
1698 Err(lookahead.error())
1699 }
1700 }
1701 }
1702
1703 impl Parse for TraitItemConst {
1704 fn parse(input: ParseStream) -> Result<Self> {
1705 Ok(TraitItemConst {
1706 attrs: input.call(Attribute::parse_outer)?,
1707 const_token: input.parse()?,
1708 ident: input.parse()?,
1709 colon_token: input.parse()?,
1710 ty: input.parse()?,
1711 default: {
1712 if input.peek(Token![=]) {
1713 let eq_token: Token![=] = input.parse()?;
1714 let default: Expr = input.parse()?;
1715 Some((eq_token, default))
1716 } else {
1717 None
1718 }
1719 },
1720 semi_token: input.parse()?,
1721 })
1722 }
1723 }
1724
1725 impl Parse for TraitItemMethod {
1726 fn parse(input: ParseStream) -> Result<Self> {
1727 let outer_attrs = input.call(Attribute::parse_outer)?;
1728 let constness: Option<Token![const]> = input.parse()?;
1729 let unsafety: Option<Token![unsafe]> = input.parse()?;
1730 let abi: Option<Abi> = input.parse()?;
1731 let fn_token: Token![fn] = input.parse()?;
1732 let ident: Ident = input.parse()?;
1733 let generics: Generics = input.parse()?;
1734
1735 let content;
1736 let paren_token = parenthesized!(content in input);
1737 let inputs = content.parse_terminated(FnArg::parse)?;
1738
1739 let output: ReturnType = input.parse()?;
1740 let where_clause: Option<WhereClause> = input.parse()?;
1741
1742 let lookahead = input.lookahead1();
1743 let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) {
1744 let content;
1745 let brace_token = braced!(content in input);
1746 let inner_attrs = content.call(Attribute::parse_inner)?;
1747 let stmts = content.call(Block::parse_within)?;
1748 (Some(brace_token), inner_attrs, stmts, None)
1749 } else if lookahead.peek(Token![;]) {
1750 let semi_token: Token![;] = input.parse()?;
1751 (None, Vec::new(), Vec::new(), Some(semi_token))
1752 } else {
1753 return Err(lookahead.error());
1754 };
1755
1756 Ok(TraitItemMethod {
1757 attrs: private::attrs(outer_attrs, inner_attrs),
1758 sig: MethodSig {
1759 constness: constness,
1760 unsafety: unsafety,
1761 asyncness: None,
1762 abi: abi,
1763 ident: ident,
1764 decl: FnDecl {
1765 fn_token: fn_token,
1766 paren_token: paren_token,
1767 inputs: inputs,
1768 output: output,
1769 variadic: None,
1770 generics: Generics {
1771 where_clause: where_clause,
1772 ..generics
1773 },
1774 },
1775 },
1776 default: brace_token.map(|brace_token| Block {
1777 brace_token: brace_token,
1778 stmts: stmts,
1779 }),
1780 semi_token: semi_token,
1781 })
1782 }
1783 }
1784
1785 impl Parse for TraitItemType {
1786 fn parse(input: ParseStream) -> Result<Self> {
1787 let attrs = input.call(Attribute::parse_outer)?;
1788 let type_token: Token![type] = input.parse()?;
1789 let ident: Ident = input.parse()?;
1790 let mut generics: Generics = input.parse()?;
1791 let colon_token: Option<Token![:]> = input.parse()?;
1792
1793 let mut bounds = Punctuated::new();
1794 if colon_token.is_some() {
1795 while !input.peek(Token![where]) && !input.peek(Token![=]) && !input.peek(Token![;])
1796 {
1797 if !bounds.is_empty() {
1798 bounds.push_punct(input.parse()?);
1799 }
1800 bounds.push_value(input.parse()?);
1801 }
1802 }
1803
1804 generics.where_clause = input.parse()?;
1805 let default = if input.peek(Token![=]) {
1806 let eq_token: Token![=] = input.parse()?;
1807 let default: Type = input.parse()?;
1808 Some((eq_token, default))
1809 } else {
1810 None
1811 };
1812 let semi_token: Token![;] = input.parse()?;
1813
1814 Ok(TraitItemType {
1815 attrs: attrs,
1816 type_token: type_token,
1817 ident: ident,
1818 generics: generics,
1819 colon_token: colon_token,
1820 bounds: bounds,
1821 default: default,
1822 semi_token: semi_token,
1823 })
1824 }
1825 }
1826
1827 impl Parse for TraitItemMacro {
1828 fn parse(input: ParseStream) -> Result<Self> {
1829 let attrs = input.call(Attribute::parse_outer)?;
1830 let mac: Macro = input.parse()?;
1831 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
1832 None
1833 } else {
1834 Some(input.parse()?)
1835 };
1836 Ok(TraitItemMacro {
1837 attrs: attrs,
1838 mac: mac,
1839 semi_token: semi_token,
1840 })
1841 }
1842 }
1843
1844 impl Parse for ItemImpl {
1845 fn parse(input: ParseStream) -> Result<Self> {
1846 let outer_attrs = input.call(Attribute::parse_outer)?;
1847 let defaultness: Option<Token![default]> = input.parse()?;
1848 let unsafety: Option<Token![unsafe]> = input.parse()?;
1849 let impl_token: Token![impl] = input.parse()?;
1850
1851 let has_generics = input.peek(Token![<])
1852 && (input.peek2(Token![>])
1853 || input.peek2(Token![#])
1854 || (input.peek2(Ident) || input.peek2(Lifetime))
1855 && (input.peek3(Token![:])
1856 || input.peek3(Token![,])
1857 || input.peek3(Token![>])));
1858 let generics: Generics = if has_generics {
1859 input.parse()?
1860 } else {
1861 Generics::default()
1862 };
1863
1864 let trait_ = {
1865 let ahead = input.fork();
1866 if ahead.parse::<Option<Token![!]>>().is_ok()
1867 && ahead.parse::<Path>().is_ok()
1868 && ahead.parse::<Token![for]>().is_ok()
1869 {
1870 let polarity: Option<Token![!]> = input.parse()?;
1871 let path: Path = input.parse()?;
1872 let for_token: Token![for] = input.parse()?;
1873 Some((polarity, path, for_token))
1874 } else {
1875 None
1876 }
1877 };
1878 let self_ty: Type = input.parse()?;
1879 let where_clause: Option<WhereClause> = input.parse()?;
1880
1881 let content;
1882 let brace_token = braced!(content in input);
1883 let inner_attrs = content.call(Attribute::parse_inner)?;
1884
1885 let mut items = Vec::new();
1886 while !content.is_empty() {
1887 items.push(content.parse()?);
1888 }
1889
1890 Ok(ItemImpl {
1891 attrs: private::attrs(outer_attrs, inner_attrs),
1892 defaultness: defaultness,
1893 unsafety: unsafety,
1894 impl_token: impl_token,
1895 generics: Generics {
1896 where_clause: where_clause,
1897 ..generics
1898 },
1899 trait_: trait_,
1900 self_ty: Box::new(self_ty),
1901 brace_token: brace_token,
1902 items: items,
1903 })
1904 }
1905 }
1906
1907 impl Parse for ImplItem {
1908 fn parse(input: ParseStream) -> Result<Self> {
1909 let ahead = input.fork();
1910 ahead.call(Attribute::parse_outer)?;
1911 let vis: Visibility = ahead.parse()?;
1912
1913 let mut lookahead = ahead.lookahead1();
1914 let defaultness = if lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) {
1915 let defaultness: Token![default] = ahead.parse()?;
1916 lookahead = ahead.lookahead1();
1917 Some(defaultness)
1918 } else {
1919 None
1920 };
1921
1922 if lookahead.peek(Token![const]) {
1923 ahead.parse::<Token![const]>()?;
1924 let lookahead = ahead.lookahead1();
1925 if lookahead.peek(Ident) {
1926 input.parse().map(ImplItem::Const)
1927 } else if lookahead.peek(Token![unsafe])
1928 || lookahead.peek(Token![async])
1929 || lookahead.peek(Token![extern])
1930 || lookahead.peek(Token![fn])
1931 {
1932 input.parse().map(ImplItem::Method)
1933 } else {
1934 Err(lookahead.error())
1935 }
1936 } else if lookahead.peek(Token![unsafe])
1937 || lookahead.peek(Token![async])
1938 || lookahead.peek(Token![extern])
1939 || lookahead.peek(Token![fn])
1940 {
1941 input.parse().map(ImplItem::Method)
1942 } else if lookahead.peek(Token![type]) {
1943 input.parse().map(ImplItem::Type)
1944 } else if vis.is_inherited()
1945 && defaultness.is_none()
1946 && lookahead.peek(Token![existential])
1947 {
1948 input.parse().map(ImplItem::Existential)
1949 } else if vis.is_inherited()
1950 && defaultness.is_none()
1951 && (lookahead.peek(Ident)
1952 || lookahead.peek(Token![self])
1953 || lookahead.peek(Token![super])
1954 || lookahead.peek(Token![extern])
1955 || lookahead.peek(Token![crate])
1956 || lookahead.peek(Token![::]))
1957 {
1958 input.parse().map(ImplItem::Macro)
1959 } else {
1960 Err(lookahead.error())
1961 }
1962 }
1963 }
1964
1965 impl Parse for ImplItemConst {
1966 fn parse(input: ParseStream) -> Result<Self> {
1967 Ok(ImplItemConst {
1968 attrs: input.call(Attribute::parse_outer)?,
1969 vis: input.parse()?,
1970 defaultness: input.parse()?,
1971 const_token: input.parse()?,
1972 ident: input.parse()?,
1973 colon_token: input.parse()?,
1974 ty: input.parse()?,
1975 eq_token: input.parse()?,
1976 expr: input.parse()?,
1977 semi_token: input.parse()?,
1978 })
1979 }
1980 }
1981
1982 impl Parse for ImplItemMethod {
1983 fn parse(input: ParseStream) -> Result<Self> {
1984 let outer_attrs = input.call(Attribute::parse_outer)?;
1985 let vis: Visibility = input.parse()?;
1986 let defaultness: Option<Token![default]> = input.parse()?;
1987 let constness: Option<Token![const]> = input.parse()?;
1988 let unsafety: Option<Token![unsafe]> = input.parse()?;
1989 let asyncness: Option<Token![async]> = input.parse()?;
1990 let abi: Option<Abi> = input.parse()?;
1991 let fn_token: Token![fn] = input.parse()?;
1992 let ident: Ident = input.parse()?;
1993 let generics: Generics = input.parse()?;
1994
1995 let content;
1996 let paren_token = parenthesized!(content in input);
1997 let inputs = content.parse_terminated(FnArg::parse)?;
1998
1999 let output: ReturnType = input.parse()?;
2000 let where_clause: Option<WhereClause> = input.parse()?;
2001
2002 let content;
2003 let brace_token = braced!(content in input);
2004 let inner_attrs = content.call(Attribute::parse_inner)?;
2005 let stmts = content.call(Block::parse_within)?;
2006
2007 Ok(ImplItemMethod {
2008 attrs: private::attrs(outer_attrs, inner_attrs),
2009 vis: vis,
2010 defaultness: defaultness,
2011 sig: MethodSig {
2012 constness: constness,
2013 unsafety: unsafety,
2014 asyncness: asyncness,
2015 abi: abi,
2016 ident: ident,
2017 decl: FnDecl {
2018 fn_token: fn_token,
2019 paren_token: paren_token,
2020 inputs: inputs,
2021 output: output,
2022 variadic: None,
2023 generics: Generics {
2024 where_clause: where_clause,
2025 ..generics
2026 },
2027 },
2028 },
2029 block: Block {
2030 brace_token: brace_token,
2031 stmts: stmts,
2032 },
2033 })
2034 }
2035 }
2036
2037 impl Parse for ImplItemType {
2038 fn parse(input: ParseStream) -> Result<Self> {
2039 Ok(ImplItemType {
2040 attrs: input.call(Attribute::parse_outer)?,
2041 vis: input.parse()?,
2042 defaultness: input.parse()?,
2043 type_token: input.parse()?,
2044 ident: input.parse()?,
2045 generics: {
2046 let mut generics: Generics = input.parse()?;
2047 generics.where_clause = input.parse()?;
2048 generics
2049 },
2050 eq_token: input.parse()?,
2051 ty: input.parse()?,
2052 semi_token: input.parse()?,
2053 })
2054 }
2055 }
2056
2057 impl Parse for ImplItemExistential {
2058 fn parse(input: ParseStream) -> Result<Self> {
2059 let ety: ItemExistential = input.parse()?;
2060 Ok(ImplItemExistential {
2061 attrs: ety.attrs,
2062 existential_token: ety.existential_token,
2063 type_token: ety.type_token,
2064 ident: ety.ident,
2065 generics: ety.generics,
2066 colon_token: ety.colon_token,
2067 bounds: ety.bounds,
2068 semi_token: ety.semi_token,
2069 })
2070 }
2071 }
2072
2073 impl Parse for ImplItemMacro {
2074 fn parse(input: ParseStream) -> Result<Self> {
2075 let attrs = input.call(Attribute::parse_outer)?;
2076 let mac: Macro = input.parse()?;
2077 let semi_token: Option<Token![;]> = if mac.delimiter.is_brace() {
2078 None
2079 } else {
2080 Some(input.parse()?)
2081 };
2082 Ok(ImplItemMacro {
2083 attrs: attrs,
2084 mac: mac,
2085 semi_token: semi_token,
2086 })
2087 }
2088 }
2089
2090 impl Visibility {
2091 fn is_inherited(&self) -> bool {
2092 match *self {
2093 Visibility::Inherited => true,
2094 _ => false,
2095 }
2096 }
2097 }
2098
2099 impl MacroDelimiter {
2100 fn is_brace(&self) -> bool {
2101 match *self {
2102 MacroDelimiter::Brace(_) => true,
2103 MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => false,
2104 }
2105 }
2106 }
2107}
2108
2109#[cfg(feature = "printing")]
2110pub mod printing {
2111 use super::*;
2112
2113 use proc_macro2::TokenStream;
2114 use quote::{ToTokens, TokenStreamExt};
2115
2116 use attr::FilterAttrs;
2117 use print::TokensOrDefault;
2118
2119 impl ToTokens for ItemExternCrate {
2120 fn to_tokens(&self, tokens: &mut TokenStream) {
2121 tokens.append_all(self.attrs.outer());
2122 self.vis.to_tokens(tokens);
2123 self.extern_token.to_tokens(tokens);
2124 self.crate_token.to_tokens(tokens);
2125 self.ident.to_tokens(tokens);
2126 if let Some((ref as_token, ref rename)) = self.rename {
2127 as_token.to_tokens(tokens);
2128 rename.to_tokens(tokens);
2129 }
2130 self.semi_token.to_tokens(tokens);
2131 }
2132 }
2133
2134 impl ToTokens for ItemUse {
2135 fn to_tokens(&self, tokens: &mut TokenStream) {
2136 tokens.append_all(self.attrs.outer());
2137 self.vis.to_tokens(tokens);
2138 self.use_token.to_tokens(tokens);
2139 self.leading_colon.to_tokens(tokens);
2140 self.tree.to_tokens(tokens);
2141 self.semi_token.to_tokens(tokens);
2142 }
2143 }
2144
2145 impl ToTokens for ItemStatic {
2146 fn to_tokens(&self, tokens: &mut TokenStream) {
2147 tokens.append_all(self.attrs.outer());
2148 self.vis.to_tokens(tokens);
2149 self.static_token.to_tokens(tokens);
2150 self.mutability.to_tokens(tokens);
2151 self.ident.to_tokens(tokens);
2152 self.colon_token.to_tokens(tokens);
2153 self.ty.to_tokens(tokens);
2154 self.eq_token.to_tokens(tokens);
2155 self.expr.to_tokens(tokens);
2156 self.semi_token.to_tokens(tokens);
2157 }
2158 }
2159
2160 impl ToTokens for ItemConst {
2161 fn to_tokens(&self, tokens: &mut TokenStream) {
2162 tokens.append_all(self.attrs.outer());
2163 self.vis.to_tokens(tokens);
2164 self.const_token.to_tokens(tokens);
2165 self.ident.to_tokens(tokens);
2166 self.colon_token.to_tokens(tokens);
2167 self.ty.to_tokens(tokens);
2168 self.eq_token.to_tokens(tokens);
2169 self.expr.to_tokens(tokens);
2170 self.semi_token.to_tokens(tokens);
2171 }
2172 }
2173
2174 impl ToTokens for ItemFn {
2175 fn to_tokens(&self, tokens: &mut TokenStream) {
2176 tokens.append_all(self.attrs.outer());
2177 self.vis.to_tokens(tokens);
2178 self.constness.to_tokens(tokens);
2179 self.unsafety.to_tokens(tokens);
2180 self.asyncness.to_tokens(tokens);
2181 self.abi.to_tokens(tokens);
2182 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2183 self.block.brace_token.surround(tokens, |tokens| {
2184 tokens.append_all(self.attrs.inner());
2185 tokens.append_all(&self.block.stmts);
2186 });
2187 }
2188 }
2189
2190 impl ToTokens for ItemMod {
2191 fn to_tokens(&self, tokens: &mut TokenStream) {
2192 tokens.append_all(self.attrs.outer());
2193 self.vis.to_tokens(tokens);
2194 self.mod_token.to_tokens(tokens);
2195 self.ident.to_tokens(tokens);
2196 if let Some((ref brace, ref items)) = self.content {
2197 brace.surround(tokens, |tokens| {
2198 tokens.append_all(self.attrs.inner());
2199 tokens.append_all(items);
2200 });
2201 } else {
2202 TokensOrDefault(&self.semi).to_tokens(tokens);
2203 }
2204 }
2205 }
2206
2207 impl ToTokens for ItemForeignMod {
2208 fn to_tokens(&self, tokens: &mut TokenStream) {
2209 tokens.append_all(self.attrs.outer());
2210 self.abi.to_tokens(tokens);
2211 self.brace_token.surround(tokens, |tokens| {
2212 tokens.append_all(self.attrs.inner());
2213 tokens.append_all(&self.items);
2214 });
2215 }
2216 }
2217
2218 impl ToTokens for ItemType {
2219 fn to_tokens(&self, tokens: &mut TokenStream) {
2220 tokens.append_all(self.attrs.outer());
2221 self.vis.to_tokens(tokens);
2222 self.type_token.to_tokens(tokens);
2223 self.ident.to_tokens(tokens);
2224 self.generics.to_tokens(tokens);
2225 self.generics.where_clause.to_tokens(tokens);
2226 self.eq_token.to_tokens(tokens);
2227 self.ty.to_tokens(tokens);
2228 self.semi_token.to_tokens(tokens);
2229 }
2230 }
2231
2232 impl ToTokens for ItemExistential {
2233 fn to_tokens(&self, tokens: &mut TokenStream) {
2234 tokens.append_all(self.attrs.outer());
2235 self.vis.to_tokens(tokens);
2236 self.existential_token.to_tokens(tokens);
2237 self.type_token.to_tokens(tokens);
2238 self.ident.to_tokens(tokens);
2239 self.generics.to_tokens(tokens);
2240 self.generics.where_clause.to_tokens(tokens);
2241 if !self.bounds.is_empty() {
2242 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2243 self.bounds.to_tokens(tokens);
2244 }
2245 self.semi_token.to_tokens(tokens);
2246 }
2247 }
2248
2249 impl ToTokens for ItemEnum {
2250 fn to_tokens(&self, tokens: &mut TokenStream) {
2251 tokens.append_all(self.attrs.outer());
2252 self.vis.to_tokens(tokens);
2253 self.enum_token.to_tokens(tokens);
2254 self.ident.to_tokens(tokens);
2255 self.generics.to_tokens(tokens);
2256 self.generics.where_clause.to_tokens(tokens);
2257 self.brace_token.surround(tokens, |tokens| {
2258 self.variants.to_tokens(tokens);
2259 });
2260 }
2261 }
2262
2263 impl ToTokens for ItemStruct {
2264 fn to_tokens(&self, tokens: &mut TokenStream) {
2265 tokens.append_all(self.attrs.outer());
2266 self.vis.to_tokens(tokens);
2267 self.struct_token.to_tokens(tokens);
2268 self.ident.to_tokens(tokens);
2269 self.generics.to_tokens(tokens);
2270 match self.fields {
2271 Fields::Named(ref fields) => {
2272 self.generics.where_clause.to_tokens(tokens);
2273 fields.to_tokens(tokens);
2274 }
2275 Fields::Unnamed(ref fields) => {
2276 fields.to_tokens(tokens);
2277 self.generics.where_clause.to_tokens(tokens);
2278 TokensOrDefault(&self.semi_token).to_tokens(tokens);
2279 }
2280 Fields::Unit => {
2281 self.generics.where_clause.to_tokens(tokens);
2282 TokensOrDefault(&self.semi_token).to_tokens(tokens);
2283 }
2284 }
2285 }
2286 }
2287
2288 impl ToTokens for ItemUnion {
2289 fn to_tokens(&self, tokens: &mut TokenStream) {
2290 tokens.append_all(self.attrs.outer());
2291 self.vis.to_tokens(tokens);
2292 self.union_token.to_tokens(tokens);
2293 self.ident.to_tokens(tokens);
2294 self.generics.to_tokens(tokens);
2295 self.generics.where_clause.to_tokens(tokens);
2296 self.fields.to_tokens(tokens);
2297 }
2298 }
2299
2300 impl ToTokens for ItemTrait {
2301 fn to_tokens(&self, tokens: &mut TokenStream) {
2302 tokens.append_all(self.attrs.outer());
2303 self.vis.to_tokens(tokens);
2304 self.unsafety.to_tokens(tokens);
2305 self.auto_token.to_tokens(tokens);
2306 self.trait_token.to_tokens(tokens);
2307 self.ident.to_tokens(tokens);
2308 self.generics.to_tokens(tokens);
2309 if !self.supertraits.is_empty() {
2310 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2311 self.supertraits.to_tokens(tokens);
2312 }
2313 self.generics.where_clause.to_tokens(tokens);
2314 self.brace_token.surround(tokens, |tokens| {
2315 tokens.append_all(&self.items);
2316 });
2317 }
2318 }
2319
2320 impl ToTokens for ItemTraitAlias {
2321 fn to_tokens(&self, tokens: &mut TokenStream) {
2322 tokens.append_all(self.attrs.outer());
2323 self.vis.to_tokens(tokens);
2324 self.trait_token.to_tokens(tokens);
2325 self.ident.to_tokens(tokens);
2326 self.generics.to_tokens(tokens);
2327 self.eq_token.to_tokens(tokens);
2328 self.bounds.to_tokens(tokens);
2329 self.generics.where_clause.to_tokens(tokens);
2330 self.semi_token.to_tokens(tokens);
2331 }
2332 }
2333
2334 impl ToTokens for ItemImpl {
2335 fn to_tokens(&self, tokens: &mut TokenStream) {
2336 tokens.append_all(self.attrs.outer());
2337 self.defaultness.to_tokens(tokens);
2338 self.unsafety.to_tokens(tokens);
2339 self.impl_token.to_tokens(tokens);
2340 self.generics.to_tokens(tokens);
2341 if let Some((ref polarity, ref path, ref for_token)) = self.trait_ {
2342 polarity.to_tokens(tokens);
2343 path.to_tokens(tokens);
2344 for_token.to_tokens(tokens);
2345 }
2346 self.self_ty.to_tokens(tokens);
2347 self.generics.where_clause.to_tokens(tokens);
2348 self.brace_token.surround(tokens, |tokens| {
2349 tokens.append_all(self.attrs.inner());
2350 tokens.append_all(&self.items);
2351 });
2352 }
2353 }
2354
2355 impl ToTokens for ItemMacro {
2356 fn to_tokens(&self, tokens: &mut TokenStream) {
2357 tokens.append_all(self.attrs.outer());
2358 self.mac.path.to_tokens(tokens);
2359 self.mac.bang_token.to_tokens(tokens);
2360 self.ident.to_tokens(tokens);
2361 match self.mac.delimiter {
2362 MacroDelimiter::Paren(ref paren) => {
2363 paren.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2364 }
2365 MacroDelimiter::Brace(ref brace) => {
2366 brace.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2367 }
2368 MacroDelimiter::Bracket(ref bracket) => {
2369 bracket.surround(tokens, |tokens| self.mac.tts.to_tokens(tokens));
2370 }
2371 }
2372 self.semi_token.to_tokens(tokens);
2373 }
2374 }
2375
2376 impl ToTokens for ItemMacro2 {
2377 fn to_tokens(&self, tokens: &mut TokenStream) {
2378 tokens.append_all(self.attrs.outer());
2379 self.vis.to_tokens(tokens);
2380 self.macro_token.to_tokens(tokens);
2381 self.ident.to_tokens(tokens);
2382
2383 if self.args.to_string() != "$ $" {
2385 self.paren_token.surround(tokens, |tokens| {
2386 self.args.to_tokens(tokens);
2387 });
2388 }
2389
2390 self.brace_token.surround(tokens, |tokens| {
2391 self.body.to_tokens(tokens);
2392 });
2393 }
2394 }
2395
2396 impl ToTokens for ItemVerbatim {
2397 fn to_tokens(&self, tokens: &mut TokenStream) {
2398 self.tts.to_tokens(tokens);
2399 }
2400 }
2401
2402 impl ToTokens for UsePath {
2403 fn to_tokens(&self, tokens: &mut TokenStream) {
2404 self.ident.to_tokens(tokens);
2405 self.colon2_token.to_tokens(tokens);
2406 self.tree.to_tokens(tokens);
2407 }
2408 }
2409
2410 impl ToTokens for UseName {
2411 fn to_tokens(&self, tokens: &mut TokenStream) {
2412 self.ident.to_tokens(tokens);
2413 }
2414 }
2415
2416 impl ToTokens for UseRename {
2417 fn to_tokens(&self, tokens: &mut TokenStream) {
2418 self.ident.to_tokens(tokens);
2419 self.as_token.to_tokens(tokens);
2420 self.rename.to_tokens(tokens);
2421 }
2422 }
2423
2424 impl ToTokens for UseGlob {
2425 fn to_tokens(&self, tokens: &mut TokenStream) {
2426 self.star_token.to_tokens(tokens);
2427 }
2428 }
2429
2430 impl ToTokens for UseGroup {
2431 fn to_tokens(&self, tokens: &mut TokenStream) {
2432 self.brace_token.surround(tokens, |tokens| {
2433 self.items.to_tokens(tokens);
2434 });
2435 }
2436 }
2437
2438 impl ToTokens for TraitItemConst {
2439 fn to_tokens(&self, tokens: &mut TokenStream) {
2440 tokens.append_all(self.attrs.outer());
2441 self.const_token.to_tokens(tokens);
2442 self.ident.to_tokens(tokens);
2443 self.colon_token.to_tokens(tokens);
2444 self.ty.to_tokens(tokens);
2445 if let Some((ref eq_token, ref default)) = self.default {
2446 eq_token.to_tokens(tokens);
2447 default.to_tokens(tokens);
2448 }
2449 self.semi_token.to_tokens(tokens);
2450 }
2451 }
2452
2453 impl ToTokens for TraitItemMethod {
2454 fn to_tokens(&self, tokens: &mut TokenStream) {
2455 tokens.append_all(self.attrs.outer());
2456 self.sig.to_tokens(tokens);
2457 match self.default {
2458 Some(ref block) => {
2459 block.brace_token.surround(tokens, |tokens| {
2460 tokens.append_all(self.attrs.inner());
2461 tokens.append_all(&block.stmts);
2462 });
2463 }
2464 None => {
2465 TokensOrDefault(&self.semi_token).to_tokens(tokens);
2466 }
2467 }
2468 }
2469 }
2470
2471 impl ToTokens for TraitItemType {
2472 fn to_tokens(&self, tokens: &mut TokenStream) {
2473 tokens.append_all(self.attrs.outer());
2474 self.type_token.to_tokens(tokens);
2475 self.ident.to_tokens(tokens);
2476 self.generics.to_tokens(tokens);
2477 if !self.bounds.is_empty() {
2478 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2479 self.bounds.to_tokens(tokens);
2480 }
2481 self.generics.where_clause.to_tokens(tokens);
2482 if let Some((ref eq_token, ref default)) = self.default {
2483 eq_token.to_tokens(tokens);
2484 default.to_tokens(tokens);
2485 }
2486 self.semi_token.to_tokens(tokens);
2487 }
2488 }
2489
2490 impl ToTokens for TraitItemMacro {
2491 fn to_tokens(&self, tokens: &mut TokenStream) {
2492 tokens.append_all(self.attrs.outer());
2493 self.mac.to_tokens(tokens);
2494 self.semi_token.to_tokens(tokens);
2495 }
2496 }
2497
2498 impl ToTokens for TraitItemVerbatim {
2499 fn to_tokens(&self, tokens: &mut TokenStream) {
2500 self.tts.to_tokens(tokens);
2501 }
2502 }
2503
2504 impl ToTokens for ImplItemConst {
2505 fn to_tokens(&self, tokens: &mut TokenStream) {
2506 tokens.append_all(self.attrs.outer());
2507 self.vis.to_tokens(tokens);
2508 self.defaultness.to_tokens(tokens);
2509 self.const_token.to_tokens(tokens);
2510 self.ident.to_tokens(tokens);
2511 self.colon_token.to_tokens(tokens);
2512 self.ty.to_tokens(tokens);
2513 self.eq_token.to_tokens(tokens);
2514 self.expr.to_tokens(tokens);
2515 self.semi_token.to_tokens(tokens);
2516 }
2517 }
2518
2519 impl ToTokens for ImplItemMethod {
2520 fn to_tokens(&self, tokens: &mut TokenStream) {
2521 tokens.append_all(self.attrs.outer());
2522 self.vis.to_tokens(tokens);
2523 self.defaultness.to_tokens(tokens);
2524 self.sig.to_tokens(tokens);
2525 self.block.brace_token.surround(tokens, |tokens| {
2526 tokens.append_all(self.attrs.inner());
2527 tokens.append_all(&self.block.stmts);
2528 });
2529 }
2530 }
2531
2532 impl ToTokens for ImplItemType {
2533 fn to_tokens(&self, tokens: &mut TokenStream) {
2534 tokens.append_all(self.attrs.outer());
2535 self.vis.to_tokens(tokens);
2536 self.defaultness.to_tokens(tokens);
2537 self.type_token.to_tokens(tokens);
2538 self.ident.to_tokens(tokens);
2539 self.generics.to_tokens(tokens);
2540 self.generics.where_clause.to_tokens(tokens);
2541 self.eq_token.to_tokens(tokens);
2542 self.ty.to_tokens(tokens);
2543 self.semi_token.to_tokens(tokens);
2544 }
2545 }
2546
2547 impl ToTokens for ImplItemExistential {
2548 fn to_tokens(&self, tokens: &mut TokenStream) {
2549 tokens.append_all(self.attrs.outer());
2550 self.existential_token.to_tokens(tokens);
2551 self.type_token.to_tokens(tokens);
2552 self.ident.to_tokens(tokens);
2553 self.generics.to_tokens(tokens);
2554 self.generics.where_clause.to_tokens(tokens);
2555 if !self.bounds.is_empty() {
2556 TokensOrDefault(&self.colon_token).to_tokens(tokens);
2557 self.bounds.to_tokens(tokens);
2558 }
2559 self.semi_token.to_tokens(tokens);
2560 }
2561 }
2562
2563 impl ToTokens for ImplItemMacro {
2564 fn to_tokens(&self, tokens: &mut TokenStream) {
2565 tokens.append_all(self.attrs.outer());
2566 self.mac.to_tokens(tokens);
2567 self.semi_token.to_tokens(tokens);
2568 }
2569 }
2570
2571 impl ToTokens for ImplItemVerbatim {
2572 fn to_tokens(&self, tokens: &mut TokenStream) {
2573 self.tts.to_tokens(tokens);
2574 }
2575 }
2576
2577 impl ToTokens for ForeignItemFn {
2578 fn to_tokens(&self, tokens: &mut TokenStream) {
2579 tokens.append_all(self.attrs.outer());
2580 self.vis.to_tokens(tokens);
2581 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2582 self.semi_token.to_tokens(tokens);
2583 }
2584 }
2585
2586 impl ToTokens for ForeignItemStatic {
2587 fn to_tokens(&self, tokens: &mut TokenStream) {
2588 tokens.append_all(self.attrs.outer());
2589 self.vis.to_tokens(tokens);
2590 self.static_token.to_tokens(tokens);
2591 self.mutability.to_tokens(tokens);
2592 self.ident.to_tokens(tokens);
2593 self.colon_token.to_tokens(tokens);
2594 self.ty.to_tokens(tokens);
2595 self.semi_token.to_tokens(tokens);
2596 }
2597 }
2598
2599 impl ToTokens for ForeignItemType {
2600 fn to_tokens(&self, tokens: &mut TokenStream) {
2601 tokens.append_all(self.attrs.outer());
2602 self.vis.to_tokens(tokens);
2603 self.type_token.to_tokens(tokens);
2604 self.ident.to_tokens(tokens);
2605 self.semi_token.to_tokens(tokens);
2606 }
2607 }
2608
2609 impl ToTokens for ForeignItemMacro {
2610 fn to_tokens(&self, tokens: &mut TokenStream) {
2611 tokens.append_all(self.attrs.outer());
2612 self.mac.to_tokens(tokens);
2613 self.semi_token.to_tokens(tokens);
2614 }
2615 }
2616
2617 impl ToTokens for ForeignItemVerbatim {
2618 fn to_tokens(&self, tokens: &mut TokenStream) {
2619 self.tts.to_tokens(tokens);
2620 }
2621 }
2622
2623 impl ToTokens for MethodSig {
2624 fn to_tokens(&self, tokens: &mut TokenStream) {
2625 self.constness.to_tokens(tokens);
2626 self.unsafety.to_tokens(tokens);
2627 self.asyncness.to_tokens(tokens);
2628 self.abi.to_tokens(tokens);
2629 NamedDecl(&self.decl, &self.ident).to_tokens(tokens);
2630 }
2631 }
2632
2633 pub struct NamedDecl<'a>(&'a FnDecl, &'a Ident);
2634
2635 impl<'a> ToTokens for NamedDecl<'a> {
2636 fn to_tokens(&self, tokens: &mut TokenStream) {
2637 self.0.fn_token.to_tokens(tokens);
2638 self.1.to_tokens(tokens);
2639 self.0.generics.to_tokens(tokens);
2640 self.0.paren_token.surround(tokens, |tokens| {
2641 self.0.inputs.to_tokens(tokens);
2642 if self.0.variadic.is_some() && !self.0.inputs.empty_or_trailing() {
2643 <Token![,]>::default().to_tokens(tokens);
2644 }
2645 self.0.variadic.to_tokens(tokens);
2646 });
2647 self.0.output.to_tokens(tokens);
2648 self.0.generics.where_clause.to_tokens(tokens);
2649 }
2650 }
2651
2652 impl ToTokens for ArgSelfRef {
2653 fn to_tokens(&self, tokens: &mut TokenStream) {
2654 self.and_token.to_tokens(tokens);
2655 self.lifetime.to_tokens(tokens);
2656 self.mutability.to_tokens(tokens);
2657 self.self_token.to_tokens(tokens);
2658 }
2659 }
2660
2661 impl ToTokens for ArgSelf {
2662 fn to_tokens(&self, tokens: &mut TokenStream) {
2663 self.mutability.to_tokens(tokens);
2664 self.self_token.to_tokens(tokens);
2665 }
2666 }
2667
2668 impl ToTokens for ArgCaptured {
2669 fn to_tokens(&self, tokens: &mut TokenStream) {
2670 self.pat.to_tokens(tokens);
2671 self.colon_token.to_tokens(tokens);
2672 self.ty.to_tokens(tokens);
2673 }
2674 }
2675}