2 use crate::derive
::{Data, DataEnum, DataStruct, DataUnion, DeriveInput}
;
3 use crate::punctuated
::Punctuated
;
4 use proc_macro2
::TokenStream
;
6 #[cfg(feature = "parsing")]
10 /// Things that can appear directly inside of a module or scope.
12 /// *This type is available only if Syn is built with the `"full"` feature.*
14 /// # Syntax tree enum
16 /// This type is a [syntax tree enum].
18 /// [syntax tree enum]: Expr#syntax-tree-enums
19 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
21 /// A constant item: `const MAX: u16 = 65535`.
24 /// An enum definition: `enum Foo<A, B> { A(A), B(B) }`.
27 /// An `extern crate` item: `extern crate serde`.
28 ExternCrate(ItemExternCrate
),
30 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
34 /// A block of foreign items: `extern "C" { ... }`.
35 ForeignMod(ItemForeignMod
),
37 /// An impl block providing trait or associated items: `impl<A> Trait
38 /// for Data<A> { ... }`.
41 /// A macro invocation, which includes `macro_rules!` definitions.
44 /// A 2.0-style declarative macro introduced by the `macro` keyword.
47 /// A module or module declaration: `mod m` or `mod m { ... }`.
50 /// A static item: `static BIKE: Shed = Shed(42)`.
53 /// A struct definition: `struct Foo<A> { x: A }`.
56 /// A trait definition: `pub trait Iterator { ... }`.
59 /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.
60 TraitAlias(ItemTraitAlias
),
62 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
65 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
68 /// A use declaration: `use std::collections::HashMap`.
71 /// Tokens forming an item not interpreted by Syn.
72 Verbatim(TokenStream
),
74 // The following is the only supported idiom for exhaustive matching of
78 // Item::Const(e) => {...}
79 // Item::Enum(e) => {...}
81 // Item::Verbatim(e) => {...}
84 // Item::__TestExhaustive(_) => unimplemented!(),
86 // _ => { /* some sane fallback */ }
89 // This way we fail your tests but don't break your library when adding
90 // a variant. You will be notified by a test failure when a variant is
91 // added, so that you can add code to handle it, but your library will
92 // continue to compile and work for downstream users in the interim.
94 // Once `deny(reachable)` is available in rustc, Item will be
95 // reimplemented as a non_exhaustive enum.
96 // https://github.com/rust-lang/rust/issues/44109#issuecomment-521781237
98 __TestExhaustive(crate::private
),
103 /// A constant item: `const MAX: u16 = 65535`.
105 /// *This type is available only if Syn is built with the `"full"` feature.*
106 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
107 pub struct ItemConst
{
108 pub attrs
: Vec
<Attribute
>,
110 pub const_token
: Token
![const],
112 pub colon_token
: Token
![:],
114 pub eq_token
: Token
![=],
116 pub semi_token
: Token
![;],
121 /// An enum definition: `enum Foo<A, B> { A(A), B(B) }`.
123 /// *This type is available only if Syn is built with the `"full"` feature.*
124 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
125 pub struct ItemEnum
{
126 pub attrs
: Vec
<Attribute
>,
128 pub enum_token
: Token
![enum],
130 pub generics
: Generics
,
131 pub brace_token
: token
::Brace
,
132 pub variants
: Punctuated
<Variant
, Token
![,]>,
137 /// An `extern crate` item: `extern crate serde`.
139 /// *This type is available only if Syn is built with the `"full"` feature.*
140 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
141 pub struct ItemExternCrate
{
142 pub attrs
: Vec
<Attribute
>,
144 pub extern_token
: Token
![extern],
145 pub crate_token
: Token
![crate],
147 pub rename
: Option
<(Token
![as], Ident
)>,
148 pub semi_token
: Token
![;],
153 /// A free-standing function: `fn process(n: usize) -> Result<()> { ...
156 /// *This type is available only if Syn is built with the `"full"` feature.*
157 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
159 pub attrs
: Vec
<Attribute
>,
162 pub block
: Box
<Block
>,
167 /// A block of foreign items: `extern "C" { ... }`.
169 /// *This type is available only if Syn is built with the `"full"` feature.*
170 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
171 pub struct ItemForeignMod
{
172 pub attrs
: Vec
<Attribute
>,
174 pub brace_token
: token
::Brace
,
175 pub items
: Vec
<ForeignItem
>,
180 /// An impl block providing trait or associated items: `impl<A> Trait
181 /// for Data<A> { ... }`.
183 /// *This type is available only if Syn is built with the `"full"` feature.*
184 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
185 pub struct ItemImpl
{
186 pub attrs
: Vec
<Attribute
>,
187 pub defaultness
: Option
<Token
![default]>,
188 pub unsafety
: Option
<Token
![unsafe]>,
189 pub impl_token
: Token
![impl],
190 pub generics
: Generics
,
191 /// Trait this impl implements.
192 pub trait_
: Option
<(Option
<Token
![!]>, Path
, Token
![for])>,
193 /// The Self type of the impl.
194 pub self_ty
: Box
<Type
>,
195 pub brace_token
: token
::Brace
,
196 pub items
: Vec
<ImplItem
>,
201 /// A macro invocation, which includes `macro_rules!` definitions.
203 /// *This type is available only if Syn is built with the `"full"` feature.*
204 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
205 pub struct ItemMacro
{
206 pub attrs
: Vec
<Attribute
>,
207 /// The `example` in `macro_rules! example { ... }`.
208 pub ident
: Option
<Ident
>,
210 pub semi_token
: Option
<Token
![;]>,
215 /// A 2.0-style declarative macro introduced by the `macro` keyword.
217 /// *This type is available only if Syn is built with the `"full"` feature.*
218 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
219 pub struct ItemMacro2
{
220 pub attrs
: Vec
<Attribute
>,
222 pub macro_token
: Token
![macro],
224 pub rules
: TokenStream
,
229 /// A module or module declaration: `mod m` or `mod m { ... }`.
231 /// *This type is available only if Syn is built with the `"full"` feature.*
232 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
234 pub attrs
: Vec
<Attribute
>,
236 pub mod_token
: Token
![mod],
238 pub content
: Option
<(token
::Brace
, Vec
<Item
>)>,
239 pub semi
: Option
<Token
![;]>,
244 /// A static item: `static BIKE: Shed = Shed(42)`.
246 /// *This type is available only if Syn is built with the `"full"` feature.*
247 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
248 pub struct ItemStatic
{
249 pub attrs
: Vec
<Attribute
>,
251 pub static_token
: Token
![static],
252 pub mutability
: Option
<Token
![mut]>,
254 pub colon_token
: Token
![:],
256 pub eq_token
: Token
![=],
258 pub semi_token
: Token
![;],
263 /// A struct definition: `struct Foo<A> { x: A }`.
265 /// *This type is available only if Syn is built with the `"full"` feature.*
266 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
267 pub struct ItemStruct
{
268 pub attrs
: Vec
<Attribute
>,
270 pub struct_token
: Token
![struct],
272 pub generics
: Generics
,
274 pub semi_token
: Option
<Token
![;]>,
279 /// A trait definition: `pub trait Iterator { ... }`.
281 /// *This type is available only if Syn is built with the `"full"` feature.*
282 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
283 pub struct ItemTrait
{
284 pub attrs
: Vec
<Attribute
>,
286 pub unsafety
: Option
<Token
![unsafe]>,
287 pub auto_token
: Option
<Token
![auto]>,
288 pub trait_token
: Token
![trait],
290 pub generics
: Generics
,
291 pub colon_token
: Option
<Token
![:]>,
292 pub supertraits
: Punctuated
<TypeParamBound
, Token
![+]>,
293 pub brace_token
: token
::Brace
,
294 pub items
: Vec
<TraitItem
>,
299 /// A trait alias: `pub trait SharableIterator = Iterator + Sync`.
301 /// *This type is available only if Syn is built with the `"full"` feature.*
302 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
303 pub struct ItemTraitAlias
{
304 pub attrs
: Vec
<Attribute
>,
306 pub trait_token
: Token
![trait],
308 pub generics
: Generics
,
309 pub eq_token
: Token
![=],
310 pub bounds
: Punctuated
<TypeParamBound
, Token
![+]>,
311 pub semi_token
: Token
![;],
316 /// A type alias: `type Result<T> = std::result::Result<T, MyError>`.
318 /// *This type is available only if Syn is built with the `"full"` feature.*
319 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
320 pub struct ItemType
{
321 pub attrs
: Vec
<Attribute
>,
323 pub type_token
: Token
![type],
325 pub generics
: Generics
,
326 pub eq_token
: Token
![=],
328 pub semi_token
: Token
![;],
333 /// A union definition: `union Foo<A, B> { x: A, y: B }`.
335 /// *This type is available only if Syn is built with the `"full"` feature.*
336 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
337 pub struct ItemUnion
{
338 pub attrs
: Vec
<Attribute
>,
340 pub union_token
: Token
![union],
342 pub generics
: Generics
,
343 pub fields
: FieldsNamed
,
348 /// A use declaration: `use std::collections::HashMap`.
350 /// *This type is available only if Syn is built with the `"full"` feature.*
351 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
353 pub attrs
: Vec
<Attribute
>,
355 pub use_token
: Token
![use],
356 pub leading_colon
: Option
<Token
![::]>,
358 pub semi_token
: Token
![;],
363 #[cfg(feature = "parsing")]
364 pub(crate) fn replace_attrs(&mut self, new
: Vec
<Attribute
>) -> Vec
<Attribute
> {
366 Item
::ExternCrate(ItemExternCrate { attrs, .. }
)
367 | Item
::Use(ItemUse { attrs, .. }
)
368 | Item
::Static(ItemStatic { attrs, .. }
)
369 | Item
::Const(ItemConst { attrs, .. }
)
370 | Item
::Fn(ItemFn { attrs, .. }
)
371 | Item
::Mod(ItemMod { attrs, .. }
)
372 | Item
::ForeignMod(ItemForeignMod { attrs, .. }
)
373 | Item
::Type(ItemType { attrs, .. }
)
374 | Item
::Struct(ItemStruct { attrs, .. }
)
375 | Item
::Enum(ItemEnum { attrs, .. }
)
376 | Item
::Union(ItemUnion { attrs, .. }
)
377 | Item
::Trait(ItemTrait { attrs, .. }
)
378 | Item
::TraitAlias(ItemTraitAlias { attrs, .. }
)
379 | Item
::Impl(ItemImpl { attrs, .. }
)
380 | Item
::Macro(ItemMacro { attrs, .. }
)
381 | Item
::Macro2(ItemMacro2 { attrs, .. }
) => mem
::replace(attrs
, new
),
382 Item
::Verbatim(_
) => Vec
::new(),
385 Item
::__TestExhaustive(_
) => unimplemented
!(),
392 impl From
<DeriveInput
> for Item
{
393 fn from(input
: DeriveInput
) -> Item
{
395 Data
::Struct(data
) => Item
::Struct(ItemStruct
{
398 struct_token
: data
.struct_token
,
400 generics
: input
.generics
,
402 semi_token
: data
.semi_token
,
404 Data
::Enum(data
) => Item
::Enum(ItemEnum
{
407 enum_token
: data
.enum_token
,
409 generics
: input
.generics
,
410 brace_token
: data
.brace_token
,
411 variants
: data
.variants
,
413 Data
::Union(data
) => Item
::Union(ItemUnion
{
416 union_token
: data
.union_token
,
418 generics
: input
.generics
,
425 impl From
<ItemStruct
> for DeriveInput
{
426 fn from(input
: ItemStruct
) -> DeriveInput
{
431 generics
: input
.generics
,
432 data
: Data
::Struct(DataStruct
{
433 struct_token
: input
.struct_token
,
434 fields
: input
.fields
,
435 semi_token
: input
.semi_token
,
441 impl From
<ItemEnum
> for DeriveInput
{
442 fn from(input
: ItemEnum
) -> DeriveInput
{
447 generics
: input
.generics
,
448 data
: Data
::Enum(DataEnum
{
449 enum_token
: input
.enum_token
,
450 brace_token
: input
.brace_token
,
451 variants
: input
.variants
,
457 impl From
<ItemUnion
> for DeriveInput
{
458 fn from(input
: ItemUnion
) -> DeriveInput
{
463 generics
: input
.generics
,
464 data
: Data
::Union(DataUnion
{
465 union_token
: input
.union_token
,
466 fields
: input
.fields
,
472 ast_enum_of_structs
! {
473 /// A suffix of an import tree in a `use` item: `Type as Renamed` or `*`.
475 /// *This type is available only if Syn is built with the `"full"` feature.*
477 /// # Syntax tree enum
479 /// This type is a [syntax tree enum].
481 /// [syntax tree enum]: Expr#syntax-tree-enums
482 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
484 /// A path prefix of imports in a `use` item: `std::...`.
487 /// An identifier imported by a `use` item: `HashMap`.
490 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
493 /// A glob import in a `use` item: `*`.
496 /// A braced group of imports in a `use` item: `{A, B, C}`.
502 /// A path prefix of imports in a `use` item: `std::...`.
504 /// *This type is available only if Syn is built with the `"full"` feature.*
505 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
508 pub colon2_token
: Token
![::],
509 pub tree
: Box
<UseTree
>,
514 /// An identifier imported by a `use` item: `HashMap`.
516 /// *This type is available only if Syn is built with the `"full"` feature.*
517 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
524 /// An renamed identifier imported by a `use` item: `HashMap as Map`.
526 /// *This type is available only if Syn is built with the `"full"` feature.*
527 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
528 pub struct UseRename
{
530 pub as_token
: Token
![as],
536 /// A glob import in a `use` item: `*`.
538 /// *This type is available only if Syn is built with the `"full"` feature.*
539 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
541 pub star_token
: Token
![*],
546 /// A braced group of imports in a `use` item: `{A, B, C}`.
548 /// *This type is available only if Syn is built with the `"full"` feature.*
549 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
550 pub struct UseGroup
{
551 pub brace_token
: token
::Brace
,
552 pub items
: Punctuated
<UseTree
, Token
![,]>,
556 ast_enum_of_structs
! {
557 /// An item within an `extern` block.
559 /// *This type is available only if Syn is built with the `"full"` feature.*
561 /// # Syntax tree enum
563 /// This type is a [syntax tree enum].
565 /// [syntax tree enum]: Expr#syntax-tree-enums
566 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
567 pub enum ForeignItem
{
568 /// A foreign function in an `extern` block.
571 /// A foreign static item in an `extern` block: `static ext: u8`.
572 Static(ForeignItemStatic
),
574 /// A foreign type in an `extern` block: `type void`.
575 Type(ForeignItemType
),
577 /// A macro invocation within an extern block.
578 Macro(ForeignItemMacro
),
580 /// Tokens in an `extern` block not interpreted by Syn.
581 Verbatim(TokenStream
),
583 // The following is the only supported idiom for exhaustive matching of
587 // ForeignItem::Fn(e) => {...}
588 // ForeignItem::Static(e) => {...}
590 // ForeignItem::Verbatim(e) => {...}
593 // ForeignItem::__TestExhaustive(_) => unimplemented!(),
595 // _ => { /* some sane fallback */ }
598 // This way we fail your tests but don't break your library when adding
599 // a variant. You will be notified by a test failure when a variant is
600 // added, so that you can add code to handle it, but your library will
601 // continue to compile and work for downstream users in the interim.
603 // Once `deny(reachable)` is available in rustc, ForeignItem will be
604 // reimplemented as a non_exhaustive enum.
605 // https://github.com/rust-lang/rust/issues/44109#issuecomment-521781237
607 __TestExhaustive(crate::private
),
612 /// A foreign function in an `extern` block.
614 /// *This type is available only if Syn is built with the `"full"` feature.*
615 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
616 pub struct ForeignItemFn
{
617 pub attrs
: Vec
<Attribute
>,
620 pub semi_token
: Token
![;],
625 /// A foreign static item in an `extern` block: `static ext: u8`.
627 /// *This type is available only if Syn is built with the `"full"` feature.*
628 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
629 pub struct ForeignItemStatic
{
630 pub attrs
: Vec
<Attribute
>,
632 pub static_token
: Token
![static],
633 pub mutability
: Option
<Token
![mut]>,
635 pub colon_token
: Token
![:],
637 pub semi_token
: Token
![;],
642 /// A foreign type in an `extern` block: `type void`.
644 /// *This type is available only if Syn is built with the `"full"` feature.*
645 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
646 pub struct ForeignItemType
{
647 pub attrs
: Vec
<Attribute
>,
649 pub type_token
: Token
![type],
651 pub semi_token
: Token
![;],
656 /// A macro invocation within an extern block.
658 /// *This type is available only if Syn is built with the `"full"` feature.*
659 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
660 pub struct ForeignItemMacro
{
661 pub attrs
: Vec
<Attribute
>,
663 pub semi_token
: Option
<Token
![;]>,
667 ast_enum_of_structs
! {
668 /// An item declaration within the definition of a trait.
670 /// *This type is available only if Syn is built with the `"full"` feature.*
672 /// # Syntax tree enum
674 /// This type is a [syntax tree enum].
676 /// [syntax tree enum]: Expr#syntax-tree-enums
677 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
679 /// An associated constant within the definition of a trait.
680 Const(TraitItemConst
),
682 /// A trait method within the definition of a trait.
683 Method(TraitItemMethod
),
685 /// An associated type within the definition of a trait.
688 /// A macro invocation within the definition of a trait.
689 Macro(TraitItemMacro
),
691 /// Tokens within the definition of a trait not interpreted by Syn.
692 Verbatim(TokenStream
),
694 // The following is the only supported idiom for exhaustive matching of
698 // TraitItem::Const(e) => {...}
699 // TraitItem::Method(e) => {...}
701 // TraitItem::Verbatim(e) => {...}
704 // TraitItem::__TestExhaustive(_) => unimplemented!(),
706 // _ => { /* some sane fallback */ }
709 // This way we fail your tests but don't break your library when adding
710 // a variant. You will be notified by a test failure when a variant is
711 // added, so that you can add code to handle it, but your library will
712 // continue to compile and work for downstream users in the interim.
714 // Once `deny(reachable)` is available in rustc, TraitItem will be
715 // reimplemented as a non_exhaustive enum.
716 // https://github.com/rust-lang/rust/issues/44109#issuecomment-521781237
718 __TestExhaustive(crate::private
),
723 /// An associated constant within the definition of a trait.
725 /// *This type is available only if Syn is built with the `"full"` feature.*
726 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
727 pub struct TraitItemConst
{
728 pub attrs
: Vec
<Attribute
>,
729 pub const_token
: Token
![const],
731 pub colon_token
: Token
![:],
733 pub default: Option
<(Token
![=], Expr
)>,
734 pub semi_token
: Token
![;],
739 /// A trait method within the definition of a trait.
741 /// *This type is available only if Syn is built with the `"full"` feature.*
742 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
743 pub struct TraitItemMethod
{
744 pub attrs
: Vec
<Attribute
>,
746 pub default: Option
<Block
>,
747 pub semi_token
: Option
<Token
![;]>,
752 /// An associated type within the definition of a trait.
754 /// *This type is available only if Syn is built with the `"full"` feature.*
755 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
756 pub struct TraitItemType
{
757 pub attrs
: Vec
<Attribute
>,
758 pub type_token
: Token
![type],
760 pub generics
: Generics
,
761 pub colon_token
: Option
<Token
![:]>,
762 pub bounds
: Punctuated
<TypeParamBound
, Token
![+]>,
763 pub default: Option
<(Token
![=], Type
)>,
764 pub semi_token
: Token
![;],
769 /// A macro invocation within the definition of a trait.
771 /// *This type is available only if Syn is built with the `"full"` feature.*
772 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
773 pub struct TraitItemMacro
{
774 pub attrs
: Vec
<Attribute
>,
776 pub semi_token
: Option
<Token
![;]>,
780 ast_enum_of_structs
! {
781 /// An item within an impl block.
783 /// *This type is available only if Syn is built with the `"full"` feature.*
785 /// # Syntax tree enum
787 /// This type is a [syntax tree enum].
789 /// [syntax tree enum]: Expr#syntax-tree-enums
790 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
792 /// An associated constant within an impl block.
793 Const(ImplItemConst
),
795 /// A method within an impl block.
796 Method(ImplItemMethod
),
798 /// An associated type within an impl block.
801 /// A macro invocation within an impl block.
802 Macro(ImplItemMacro
),
804 /// Tokens within an impl block not interpreted by Syn.
805 Verbatim(TokenStream
),
807 // The following is the only supported idiom for exhaustive matching of
811 // ImplItem::Const(e) => {...}
812 // ImplItem::Method(e) => {...}
814 // ImplItem::Verbatim(e) => {...}
817 // ImplItem::__TestExhaustive(_) => unimplemented!(),
819 // _ => { /* some sane fallback */ }
822 // This way we fail your tests but don't break your library when adding
823 // a variant. You will be notified by a test failure when a variant is
824 // added, so that you can add code to handle it, but your library will
825 // continue to compile and work for downstream users in the interim.
827 // Once `deny(reachable)` is available in rustc, ImplItem will be
828 // reimplemented as a non_exhaustive enum.
829 // https://github.com/rust-lang/rust/issues/44109#issuecomment-521781237
831 __TestExhaustive(crate::private
),
836 /// An associated constant within an impl block.
838 /// *This type is available only if Syn is built with the `"full"` feature.*
839 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
840 pub struct ImplItemConst
{
841 pub attrs
: Vec
<Attribute
>,
843 pub defaultness
: Option
<Token
![default]>,
844 pub const_token
: Token
![const],
846 pub colon_token
: Token
![:],
848 pub eq_token
: Token
![=],
850 pub semi_token
: Token
![;],
855 /// A method within an impl block.
857 /// *This type is available only if Syn is built with the `"full"` feature.*
858 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
859 pub struct ImplItemMethod
{
860 pub attrs
: Vec
<Attribute
>,
862 pub defaultness
: Option
<Token
![default]>,
869 /// An associated type within an impl block.
871 /// *This type is available only if Syn is built with the `"full"` feature.*
872 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
873 pub struct ImplItemType
{
874 pub attrs
: Vec
<Attribute
>,
876 pub defaultness
: Option
<Token
![default]>,
877 pub type_token
: Token
![type],
879 pub generics
: Generics
,
880 pub eq_token
: Token
![=],
882 pub semi_token
: Token
![;],
887 /// A macro invocation within an impl block.
889 /// *This type is available only if Syn is built with the `"full"` feature.*
890 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
891 pub struct ImplItemMacro
{
892 pub attrs
: Vec
<Attribute
>,
894 pub semi_token
: Option
<Token
![;]>,
899 /// A function signature in a trait or implementation: `unsafe fn
900 /// initialize(&self)`.
902 /// *This type is available only if Syn is built with the `"full"` feature.*
903 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
904 pub struct Signature
{
905 pub constness
: Option
<Token
![const]>,
906 pub asyncness
: Option
<Token
![async
]>,
907 pub unsafety
: Option
<Token
![unsafe]>,
908 pub abi
: Option
<Abi
>,
909 pub fn_token
: Token
![fn],
911 pub generics
: Generics
,
912 pub paren_token
: token
::Paren
,
913 pub inputs
: Punctuated
<FnArg
, Token
![,]>,
914 pub variadic
: Option
<Variadic
>,
915 pub output
: ReturnType
,
920 /// A method's `self` receiver, such as `&self` or `self: Box<Self>`.
921 pub fn receiver(&self) -> Option
<&FnArg
> {
922 let arg
= self.inputs
.first()?
;
924 FnArg
::Receiver(_
) => Some(arg
),
925 FnArg
::Typed(PatType { pat, .. }
) => {
926 if let Pat
::Ident(PatIdent { ident, .. }
) = &**pat
{
937 ast_enum_of_structs
! {
938 /// An argument in a function signature: the `n: usize` in `fn f(n: usize)`.
940 /// *This type is available only if Syn is built with the `"full"` feature.*
941 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
943 /// The `self` argument of an associated method, whether taken by value
946 /// Note that `self` receivers with a specified type, such as `self:
947 /// Box<Self>`, are parsed as a `FnArg::Typed`.
950 /// A function argument accepted by pattern and type.
956 /// The `self` argument of an associated method, whether taken by value
959 /// Note that `self` receivers with a specified type, such as `self:
960 /// Box<Self>`, are parsed as a `FnArg::Typed`.
962 /// *This type is available only if Syn is built with the `"full"` feature.*
963 #[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
964 pub struct Receiver
{
965 pub attrs
: Vec
<Attribute
>,
966 pub reference
: Option
<(Token
![&], Option
<Lifetime
>)>,
967 pub mutability
: Option
<Token
![mut]>,
968 pub self_token
: Token
![self],
973 pub fn lifetime(&self) -> Option
<&Lifetime
> {
974 self.reference
.as_ref()?
.1.as_ref()
978 #[cfg(feature = "parsing")]
981 use crate::ext
::IdentExt
;
982 use crate::parse
::discouraged
::Speculative
;
983 use crate::parse
::{Parse, ParseBuffer, ParseStream, Result}
;
984 use crate::token
::Brace
;
985 use proc_macro2
::{Delimiter, Group, Punct, Spacing, TokenTree}
;
986 use std
::iter
::{self, FromIterator}
;
988 crate::custom_keyword
!(existential
);
989 crate::custom_keyword
!(macro_rules
);
991 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
992 impl Parse
for Item
{
993 fn parse(input
: ParseStream
) -> Result
<Self> {
994 let begin
= input
.fork();
995 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
996 let ahead
= input
.fork();
997 let vis
: Visibility
= ahead
.parse()?
;
999 let lookahead
= ahead
.lookahead1();
1000 let mut item
= if lookahead
.peek(Token
![fn]) || peek_signature(&ahead
) {
1001 let vis
: Visibility
= input
.parse()?
;
1002 let sig
: Signature
= input
.parse()?
;
1003 if input
.peek(Token
![;]) {
1004 input
.parse
::<Token
![;]>()?
;
1005 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1007 parse_rest_of_fn(input
, Vec
::new(), vis
, sig
).map(Item
::Fn
)
1009 } else if lookahead
.peek(Token
![extern]) {
1010 ahead
.parse
::<Token
![extern]>()?
;
1011 let lookahead
= ahead
.lookahead1();
1012 if lookahead
.peek(Token
![crate]) {
1013 input
.parse().map(Item
::ExternCrate
)
1014 } else if lookahead
.peek(token
::Brace
) {
1015 input
.parse().map(Item
::ForeignMod
)
1016 } else if lookahead
.peek(LitStr
) {
1017 ahead
.parse
::<LitStr
>()?
;
1018 let lookahead
= ahead
.lookahead1();
1019 if lookahead
.peek(token
::Brace
) {
1020 input
.parse().map(Item
::ForeignMod
)
1022 Err(lookahead
.error())
1025 Err(lookahead
.error())
1027 } else if lookahead
.peek(Token
![use]) {
1028 input
.parse().map(Item
::Use
)
1029 } else if lookahead
.peek(Token
![static]) {
1030 let vis
= input
.parse()?
;
1031 let static_token
= input
.parse()?
;
1032 let mutability
= input
.parse()?
;
1033 let ident
= input
.parse()?
;
1034 if input
.peek(Token
![=]) {
1035 input
.parse
::<Token
![=]>()?
;
1036 input
.parse
::<Expr
>()?
;
1037 input
.parse
::<Token
![;]>()?
;
1038 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1040 let colon_token
= input
.parse()?
;
1041 let ty
= input
.parse()?
;
1042 if input
.peek(Token
![;]) {
1043 input
.parse
::<Token
![;]>()?
;
1044 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1046 Ok(Item
::Static(ItemStatic
{
1054 eq_token
: input
.parse()?
,
1055 expr
: input
.parse()?
,
1056 semi_token
: input
.parse()?
,
1060 } else if lookahead
.peek(Token
![const]) {
1061 ahead
.parse
::<Token
![const]>()?
;
1062 let lookahead
= ahead
.lookahead1();
1063 if lookahead
.peek(Ident
) || lookahead
.peek(Token
![_
]) {
1064 let vis
= input
.parse()?
;
1065 let const_token
= input
.parse()?
;
1067 let lookahead
= input
.lookahead1();
1068 if lookahead
.peek(Ident
) || lookahead
.peek(Token
![_
]) {
1069 input
.call(Ident
::parse_any
)?
1071 return Err(lookahead
.error());
1074 let colon_token
= input
.parse()?
;
1075 let ty
= input
.parse()?
;
1076 if input
.peek(Token
![;]) {
1077 input
.parse
::<Token
![;]>()?
;
1078 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1080 Ok(Item
::Const(ItemConst
{
1087 eq_token
: input
.parse()?
,
1088 expr
: input
.parse()?
,
1089 semi_token
: input
.parse()?
,
1093 Err(lookahead
.error())
1095 } else if lookahead
.peek(Token
![unsafe]) {
1096 ahead
.parse
::<Token
![unsafe]>()?
;
1097 let lookahead
= ahead
.lookahead1();
1098 if lookahead
.peek(Token
![trait])
1099 || lookahead
.peek(Token
![auto]) && ahead
.peek2(Token
![trait])
1101 input
.parse().map(Item
::Trait
)
1102 } else if lookahead
.peek(Token
![impl]) {
1103 let allow_verbatim_impl
= true;
1104 if let Some(item
) = parse_impl(input
, allow_verbatim_impl
)?
{
1105 Ok(Item
::Impl(item
))
1107 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1109 } else if lookahead
.peek(Token
![extern]) {
1110 input
.parse
::<Visibility
>()?
;
1111 input
.parse
::<Token
![unsafe]>()?
;
1112 input
.parse
::<ItemForeignMod
>()?
;
1113 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1114 } else if lookahead
.peek(Token
![mod]) {
1115 input
.parse
::<Visibility
>()?
;
1116 input
.parse
::<Token
![unsafe]>()?
;
1117 input
.parse
::<ItemMod
>()?
;
1118 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1120 Err(lookahead
.error())
1122 } else if lookahead
.peek(Token
![mod]) {
1123 input
.parse().map(Item
::Mod
)
1124 } else if lookahead
.peek(Token
![type]) {
1125 parse_item_type(begin
, input
)
1126 } else if lookahead
.peek(existential
) {
1127 input
.call(item_existential
).map(Item
::Verbatim
)
1128 } else if lookahead
.peek(Token
![struct]) {
1129 input
.parse().map(Item
::Struct
)
1130 } else if lookahead
.peek(Token
![enum]) {
1131 input
.parse().map(Item
::Enum
)
1132 } else if lookahead
.peek(Token
![union]) && ahead
.peek2(Ident
) {
1133 input
.parse().map(Item
::Union
)
1134 } else if lookahead
.peek(Token
![trait]) {
1135 input
.call(parse_trait_or_trait_alias
)
1136 } else if lookahead
.peek(Token
![auto]) && ahead
.peek2(Token
![trait]) {
1137 input
.parse().map(Item
::Trait
)
1138 } else if lookahead
.peek(Token
![impl])
1139 || lookahead
.peek(Token
![default]) && !ahead
.peek2(Token
![!])
1141 let allow_verbatim_impl
= true;
1142 if let Some(item
) = parse_impl(input
, allow_verbatim_impl
)?
{
1143 Ok(Item
::Impl(item
))
1145 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1147 } else if lookahead
.peek(Token
![macro]) {
1148 input
.parse().map(Item
::Macro2
)
1149 } else if vis
.is_inherited()
1150 && (lookahead
.peek(Ident
)
1151 || lookahead
.peek(Token
![self])
1152 || lookahead
.peek(Token
![super])
1153 || lookahead
.peek(Token
![crate])
1154 || lookahead
.peek(Token
![::]))
1156 input
.parse().map(Item
::Macro
)
1157 } else if ahead
.peek(macro_rules
) {
1158 input
.advance_to(&ahead
);
1159 input
.parse
::<ItemMacro
>()?
;
1160 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1162 Err(lookahead
.error())
1165 attrs
.extend(item
.replace_attrs(Vec
::new()));
1166 item
.replace_attrs(attrs
);
1171 struct FlexibleItemType
{
1173 defaultness
: Option
<Token
![default]>,
1174 type_token
: Token
![type],
1177 colon_token
: Option
<Token
![:]>,
1178 bounds
: Punctuated
<TypeParamBound
, Token
![+]>,
1179 ty
: Option
<(Token
![=], Type
)>,
1180 semi_token
: Token
![;],
1183 impl Parse
for FlexibleItemType
{
1184 fn parse(input
: ParseStream
) -> Result
<Self> {
1185 let vis
: Visibility
= input
.parse()?
;
1186 let defaultness
: Option
<Token
![default]> = input
.parse()?
;
1187 let type_token
: Token
![type] = input
.parse()?
;
1188 let ident
: Ident
= input
.parse()?
;
1189 let mut generics
: Generics
= input
.parse()?
;
1190 let colon_token
: Option
<Token
![:]> = input
.parse()?
;
1191 let mut bounds
= Punctuated
::new();
1192 if colon_token
.is_some() {
1194 if input
.peek(Token
![where]) || input
.peek(Token
![=]) || input
.peek(Token
![;]) {
1197 bounds
.push_value(input
.parse
::<TypeParamBound
>()?
);
1198 if input
.peek(Token
![where]) || input
.peek(Token
![=]) || input
.peek(Token
![;]) {
1201 bounds
.push_punct(input
.parse
::<Token
![+]>()?
);
1204 generics
.where_clause
= input
.parse()?
;
1205 let ty
= if let Some(eq_token
) = input
.parse()?
{
1206 Some((eq_token
, input
.parse
::<Type
>()?
))
1210 let semi_token
: Token
![;] = input
.parse()?
;
1212 Ok(FlexibleItemType
{
1226 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1227 impl Parse
for ItemMacro
{
1228 fn parse(input
: ParseStream
) -> Result
<Self> {
1229 let attrs
= input
.call(Attribute
::parse_outer
)?
;
1230 let path
= input
.call(Path
::parse_mod_style
)?
;
1231 let bang_token
: Token
![!] = input
.parse()?
;
1232 let ident
: Option
<Ident
> = input
.parse()?
;
1233 let (delimiter
, tokens
) = input
.call(mac
::parse_delimiter
)?
;
1234 let semi_token
: Option
<Token
![;]> = if !delimiter
.is_brace() {
1235 Some(input
.parse()?
)
1253 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1254 impl Parse
for ItemMacro2
{
1255 fn parse(input
: ParseStream
) -> Result
<Self> {
1256 let attrs
= input
.call(Attribute
::parse_outer
)?
;
1257 let vis
: Visibility
= input
.parse()?
;
1258 let macro_token
: Token
![macro] = input
.parse()?
;
1259 let ident
: Ident
= input
.parse()?
;
1260 let mut rules
= TokenStream
::new();
1262 let mut lookahead
= input
.lookahead1();
1263 if lookahead
.peek(token
::Paren
) {
1265 let paren_token
= parenthesized
!(paren_content
in input
);
1266 let args
: TokenStream
= paren_content
.parse()?
;
1267 let mut args
= Group
::new(Delimiter
::Parenthesis
, args
);
1268 args
.set_span(paren_token
.span
);
1269 rules
.extend(iter
::once(TokenTree
::Group(args
)));
1270 lookahead
= input
.lookahead1();
1273 if lookahead
.peek(token
::Brace
) {
1275 let brace_token
= braced
!(brace_content
in input
);
1276 let body
: TokenStream
= brace_content
.parse()?
;
1277 let mut body
= Group
::new(Delimiter
::Brace
, body
);
1278 body
.set_span(brace_token
.span
);
1279 rules
.extend(iter
::once(TokenTree
::Group(body
)));
1281 return Err(lookahead
.error());
1294 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1295 impl Parse
for ItemExternCrate
{
1296 fn parse(input
: ParseStream
) -> Result
<Self> {
1297 Ok(ItemExternCrate
{
1298 attrs
: input
.call(Attribute
::parse_outer
)?
,
1299 vis
: input
.parse()?
,
1300 extern_token
: input
.parse()?
,
1301 crate_token
: input
.parse()?
,
1303 if input
.peek(Token
![self]) {
1304 input
.call(Ident
::parse_any
)?
1310 if input
.peek(Token
![as]) {
1311 let as_token
: Token
![as] = input
.parse()?
;
1312 let rename
: Ident
= if input
.peek(Token
![_
]) {
1313 Ident
::from(input
.parse
::<Token
![_
]>()?
)
1317 Some((as_token
, rename
))
1322 semi_token
: input
.parse()?
,
1327 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1328 impl Parse
for ItemUse
{
1329 fn parse(input
: ParseStream
) -> Result
<Self> {
1331 attrs
: input
.call(Attribute
::parse_outer
)?
,
1332 vis
: input
.parse()?
,
1333 use_token
: input
.parse()?
,
1334 leading_colon
: input
.parse()?
,
1335 tree
: input
.parse()?
,
1336 semi_token
: input
.parse()?
,
1341 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1342 impl Parse
for UseTree
{
1343 fn parse(input
: ParseStream
) -> Result
<UseTree
> {
1344 let lookahead
= input
.lookahead1();
1345 if lookahead
.peek(Ident
)
1346 || lookahead
.peek(Token
![self])
1347 || lookahead
.peek(Token
![super])
1348 || lookahead
.peek(Token
![crate])
1350 let ident
= input
.call(Ident
::parse_any
)?
;
1351 if input
.peek(Token
![::]) {
1352 Ok(UseTree
::Path(UsePath
{
1354 colon2_token
: input
.parse()?
,
1355 tree
: Box
::new(input
.parse()?
),
1357 } else if input
.peek(Token
![as]) {
1358 Ok(UseTree
::Rename(UseRename
{
1360 as_token
: input
.parse()?
,
1362 if input
.peek(Ident
) {
1364 } else if input
.peek(Token
![_
]) {
1365 Ident
::from(input
.parse
::<Token
![_
]>()?
)
1367 return Err(input
.error("expected identifier or underscore"));
1372 Ok(UseTree
::Name(UseName { ident }
))
1374 } else if lookahead
.peek(Token
![*]) {
1375 Ok(UseTree
::Glob(UseGlob
{
1376 star_token
: input
.parse()?
,
1378 } else if lookahead
.peek(token
::Brace
) {
1380 Ok(UseTree
::Group(UseGroup
{
1381 brace_token
: braced
!(content
in input
),
1382 items
: content
.parse_terminated(UseTree
::parse
)?
,
1385 Err(lookahead
.error())
1390 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1391 impl Parse
for ItemStatic
{
1392 fn parse(input
: ParseStream
) -> Result
<Self> {
1394 attrs
: input
.call(Attribute
::parse_outer
)?
,
1395 vis
: input
.parse()?
,
1396 static_token
: input
.parse()?
,
1397 mutability
: input
.parse()?
,
1398 ident
: input
.parse()?
,
1399 colon_token
: input
.parse()?
,
1401 eq_token
: input
.parse()?
,
1402 expr
: input
.parse()?
,
1403 semi_token
: input
.parse()?
,
1408 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1409 impl Parse
for ItemConst
{
1410 fn parse(input
: ParseStream
) -> Result
<Self> {
1412 attrs
: input
.call(Attribute
::parse_outer
)?
,
1413 vis
: input
.parse()?
,
1414 const_token
: input
.parse()?
,
1416 let lookahead
= input
.lookahead1();
1417 if lookahead
.peek(Ident
) || lookahead
.peek(Token
![_
]) {
1418 input
.call(Ident
::parse_any
)?
1420 return Err(lookahead
.error());
1423 colon_token
: input
.parse()?
,
1425 eq_token
: input
.parse()?
,
1426 expr
: input
.parse()?
,
1427 semi_token
: input
.parse()?
,
1432 fn pop_variadic(args
: &mut Punctuated
<FnArg
, Token
![,]>) -> Option
<Variadic
> {
1433 let trailing_punct
= args
.trailing_punct();
1435 let last
= match args
.last_mut()?
{
1436 FnArg
::Typed(last
) => last
,
1440 let ty
= match last
.ty
.as_ref() {
1441 Type
::Verbatim(ty
) => ty
,
1445 let mut variadic
= Variadic
{
1447 dots
: parse2(ty
.clone()).ok()?
,
1450 if let Pat
::Verbatim(pat
) = last
.pat
.as_ref() {
1451 if pat
.to_string() == "..." && !trailing_punct
{
1452 variadic
.attrs
= mem
::replace(&mut last
.attrs
, Vec
::new());
1460 fn variadic_to_tokens(dots
: &Token
![...]) -> TokenStream
{
1461 TokenStream
::from_iter(vec
![
1463 let mut dot
= Punct
::new('
.'
, Spacing
::Joint
);
1464 dot
.set_span(dots
.spans
[0]);
1468 let mut dot
= Punct
::new('
.'
, Spacing
::Joint
);
1469 dot
.set_span(dots
.spans
[1]);
1473 let mut dot
= Punct
::new('
.'
, Spacing
::Alone
);
1474 dot
.set_span(dots
.spans
[2]);
1480 fn peek_signature(input
: ParseStream
) -> bool
{
1481 let fork
= input
.fork();
1482 fork
.parse
::<Option
<Token
![const]>>().is_ok()
1483 && fork
.parse
::<Option
<Token
![async
]>>().is_ok()
1484 && fork
.parse
::<Option
<Token
![unsafe]>>().is_ok()
1485 && fork
.parse
::<Option
<Abi
>>().is_ok()
1486 && fork
.peek(Token
![fn])
1489 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1490 impl Parse
for Signature
{
1491 fn parse(input
: ParseStream
) -> Result
<Self> {
1492 let constness
: Option
<Token
![const]> = input
.parse()?
;
1493 let asyncness
: Option
<Token
![async
]> = input
.parse()?
;
1494 let unsafety
: Option
<Token
![unsafe]> = input
.parse()?
;
1495 let abi
: Option
<Abi
> = input
.parse()?
;
1496 let fn_token
: Token
![fn] = input
.parse()?
;
1497 let ident
: Ident
= input
.parse()?
;
1498 let mut generics
: Generics
= input
.parse()?
;
1501 let paren_token
= parenthesized
!(content
in input
);
1502 let mut inputs
= parse_fn_args(&content
)?
;
1503 let variadic
= pop_variadic(&mut inputs
);
1505 let output
: ReturnType
= input
.parse()?
;
1506 generics
.where_clause
= input
.parse()?
;
1524 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1525 impl Parse
for ItemFn
{
1526 fn parse(input
: ParseStream
) -> Result
<Self> {
1527 let outer_attrs
= input
.call(Attribute
::parse_outer
)?
;
1528 let vis
: Visibility
= input
.parse()?
;
1529 let sig
: Signature
= input
.parse()?
;
1530 parse_rest_of_fn(input
, outer_attrs
, vis
, sig
)
1534 fn parse_rest_of_fn(
1536 mut attrs
: Vec
<Attribute
>,
1539 ) -> Result
<ItemFn
> {
1541 let brace_token
= braced
!(content
in input
);
1542 attr
::parsing
::parse_inner(&content
, &mut attrs
)?
;
1543 let stmts
= content
.call(Block
::parse_within
)?
;
1549 block
: Box
::new(Block { brace_token, stmts }
),
1553 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1554 impl Parse
for FnArg
{
1555 fn parse(input
: ParseStream
) -> Result
<Self> {
1556 let attrs
= input
.call(Attribute
::parse_outer
)?
;
1558 let ahead
= input
.fork();
1559 if let Ok(mut receiver
) = ahead
.parse
::<Receiver
>() {
1560 if !ahead
.peek(Token
![:]) {
1561 input
.advance_to(&ahead
);
1562 receiver
.attrs
= attrs
;
1563 return Ok(FnArg
::Receiver(receiver
));
1567 let mut typed
= input
.call(fn_arg_typed
)?
;
1568 typed
.attrs
= attrs
;
1569 Ok(FnArg
::Typed(typed
))
1573 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1574 impl Parse
for Receiver
{
1575 fn parse(input
: ParseStream
) -> Result
<Self> {
1579 if input
.peek(Token
![&]) {
1580 Some((input
.parse()?
, input
.parse()?
))
1585 mutability
: input
.parse()?
,
1586 self_token
: input
.parse()?
,
1591 fn parse_fn_args(input
: ParseStream
) -> Result
<Punctuated
<FnArg
, Token
![,]>> {
1592 let mut args
= Punctuated
::new();
1593 let mut has_receiver
= false;
1595 while !input
.is_empty() {
1596 let attrs
= input
.call(Attribute
::parse_outer
)?
;
1598 let arg
= if let Some(dots
) = input
.parse
::<Option
<Token
![...]>>()?
{
1599 FnArg
::Typed(PatType
{
1601 pat
: Box
::new(Pat
::Verbatim(variadic_to_tokens(&dots
))),
1602 colon_token
: Token
![:](dots
.spans
[0]),
1603 ty
: Box
::new(Type
::Verbatim(variadic_to_tokens(&dots
))),
1606 let mut arg
: FnArg
= input
.parse()?
;
1608 FnArg
::Receiver(receiver
) if has_receiver
=> {
1609 return Err(Error
::new(
1610 receiver
.self_token
.span
,
1611 "unexpected second method receiver",
1614 FnArg
::Receiver(receiver
) if !args
.is_empty() => {
1615 return Err(Error
::new(
1616 receiver
.self_token
.span
,
1617 "unexpected method receiver",
1620 FnArg
::Receiver(receiver
) => {
1621 has_receiver
= true;
1622 receiver
.attrs
= attrs
;
1624 FnArg
::Typed(arg
) => arg
.attrs
= attrs
,
1628 args
.push_value(arg
);
1630 if input
.is_empty() {
1634 let comma
: Token
![,] = input
.parse()?
;
1635 args
.push_punct(comma
);
1641 fn fn_arg_typed(input
: ParseStream
) -> Result
<PatType
> {
1642 // Hack to parse pre-2018 syntax in
1643 // test/ui/rfc-2565-param-attrs/param-attrs-pretty.rs
1644 // because the rest of the test case is valuable.
1645 if input
.peek(Ident
) && input
.peek2(Token
![<]) {
1646 let span
= input
.fork().parse
::<Ident
>()?
.span();
1649 pat
: Box
::new(Pat
::Wild(PatWild
{
1651 underscore_token
: Token
![_
](span
),
1653 colon_token
: Token
![:](span
),
1660 pat
: Box
::new(pat
::parsing
::multi_pat(input
)?
),
1661 colon_token
: input
.parse()?
,
1662 ty
: Box
::new(match input
.parse
::<Option
<Token
![...]>>()?
{
1663 Some(dot3
) => Type
::Verbatim(variadic_to_tokens(&dot3
)),
1664 None
=> input
.parse()?
,
1669 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1670 impl Parse
for ItemMod
{
1671 fn parse(input
: ParseStream
) -> Result
<Self> {
1672 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
1673 let vis
: Visibility
= input
.parse()?
;
1674 let mod_token
: Token
![mod] = input
.parse()?
;
1675 let ident
: Ident
= input
.parse()?
;
1677 let lookahead
= input
.lookahead1();
1678 if lookahead
.peek(Token
![;]) {
1685 semi
: Some(input
.parse()?
),
1687 } else if lookahead
.peek(token
::Brace
) {
1689 let brace_token
= braced
!(content
in input
);
1690 attr
::parsing
::parse_inner(&content
, &mut attrs
)?
;
1692 let mut items
= Vec
::new();
1693 while !content
.is_empty() {
1694 items
.push(content
.parse()?
);
1702 content
: Some((brace_token
, items
)),
1706 Err(lookahead
.error())
1711 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1712 impl Parse
for ItemForeignMod
{
1713 fn parse(input
: ParseStream
) -> Result
<Self> {
1714 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
1715 let abi
: Abi
= input
.parse()?
;
1718 let brace_token
= braced
!(content
in input
);
1719 attr
::parsing
::parse_inner(&content
, &mut attrs
)?
;
1720 let mut items
= Vec
::new();
1721 while !content
.is_empty() {
1722 items
.push(content
.parse()?
);
1734 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1735 impl Parse
for ForeignItem
{
1736 fn parse(input
: ParseStream
) -> Result
<Self> {
1737 let begin
= input
.fork();
1738 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
1739 let ahead
= input
.fork();
1740 let vis
: Visibility
= ahead
.parse()?
;
1742 let lookahead
= ahead
.lookahead1();
1743 let mut item
= if lookahead
.peek(Token
![fn]) || peek_signature(&ahead
) {
1744 let vis
: Visibility
= input
.parse()?
;
1745 let sig
: Signature
= input
.parse()?
;
1746 if input
.peek(token
::Brace
) {
1748 braced
!(content
in input
);
1749 content
.call(Attribute
::parse_inner
)?
;
1750 content
.call(Block
::parse_within
)?
;
1752 Ok(ForeignItem
::Verbatim(verbatim
::between(begin
, input
)))
1754 Ok(ForeignItem
::Fn(ForeignItemFn
{
1758 semi_token
: input
.parse()?
,
1761 } else if lookahead
.peek(Token
![static]) {
1762 let vis
= input
.parse()?
;
1763 let static_token
= input
.parse()?
;
1764 let mutability
= input
.parse()?
;
1765 let ident
= input
.parse()?
;
1766 let colon_token
= input
.parse()?
;
1767 let ty
= input
.parse()?
;
1768 if input
.peek(Token
![=]) {
1769 input
.parse
::<Token
![=]>()?
;
1770 input
.parse
::<Expr
>()?
;
1771 input
.parse
::<Token
![;]>()?
;
1772 Ok(ForeignItem
::Verbatim(verbatim
::between(begin
, input
)))
1774 Ok(ForeignItem
::Static(ForeignItemStatic
{
1782 semi_token
: input
.parse()?
,
1785 } else if lookahead
.peek(Token
![type]) {
1786 parse_foreign_item_type(begin
, input
)
1787 } else if vis
.is_inherited()
1788 && (lookahead
.peek(Ident
)
1789 || lookahead
.peek(Token
![self])
1790 || lookahead
.peek(Token
![super])
1791 || lookahead
.peek(Token
![crate])
1792 || lookahead
.peek(Token
![::]))
1794 input
.parse().map(ForeignItem
::Macro
)
1796 Err(lookahead
.error())
1799 let item_attrs
= match &mut item
{
1800 ForeignItem
::Fn(item
) => &mut item
.attrs
,
1801 ForeignItem
::Static(item
) => &mut item
.attrs
,
1802 ForeignItem
::Type(item
) => &mut item
.attrs
,
1803 ForeignItem
::Macro(item
) => &mut item
.attrs
,
1804 ForeignItem
::Verbatim(_
) => return Ok(item
),
1807 ForeignItem
::__TestExhaustive(_
) => unimplemented
!(),
1809 _
=> unreachable
!(),
1811 attrs
.append(item_attrs
);
1812 *item_attrs
= attrs
;
1818 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1819 impl Parse
for ForeignItemFn
{
1820 fn parse(input
: ParseStream
) -> Result
<Self> {
1821 let attrs
= input
.call(Attribute
::parse_outer
)?
;
1822 let vis
: Visibility
= input
.parse()?
;
1823 let sig
: Signature
= input
.parse()?
;
1824 let semi_token
: Token
![;] = input
.parse()?
;
1834 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1835 impl Parse
for ForeignItemStatic
{
1836 fn parse(input
: ParseStream
) -> Result
<Self> {
1837 Ok(ForeignItemStatic
{
1838 attrs
: input
.call(Attribute
::parse_outer
)?
,
1839 vis
: input
.parse()?
,
1840 static_token
: input
.parse()?
,
1841 mutability
: input
.parse()?
,
1842 ident
: input
.parse()?
,
1843 colon_token
: input
.parse()?
,
1845 semi_token
: input
.parse()?
,
1850 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1851 impl Parse
for ForeignItemType
{
1852 fn parse(input
: ParseStream
) -> Result
<Self> {
1853 Ok(ForeignItemType
{
1854 attrs
: input
.call(Attribute
::parse_outer
)?
,
1855 vis
: input
.parse()?
,
1856 type_token
: input
.parse()?
,
1857 ident
: input
.parse()?
,
1858 semi_token
: input
.parse()?
,
1863 fn parse_foreign_item_type(begin
: ParseBuffer
, input
: ParseStream
) -> Result
<ForeignItem
> {
1864 let FlexibleItemType
{
1876 if defaultness
.is_some()
1877 || generics
.lt_token
.is_some()
1878 || generics
.where_clause
.is_some()
1879 || colon_token
.is_some()
1882 Ok(ForeignItem
::Verbatim(verbatim
::between(begin
, input
)))
1884 Ok(ForeignItem
::Type(ForeignItemType
{
1894 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1895 impl Parse
for ForeignItemMacro
{
1896 fn parse(input
: ParseStream
) -> Result
<Self> {
1897 let attrs
= input
.call(Attribute
::parse_outer
)?
;
1898 let mac
: Macro
= input
.parse()?
;
1899 let semi_token
: Option
<Token
![;]> = if mac
.delimiter
.is_brace() {
1902 Some(input
.parse()?
)
1904 Ok(ForeignItemMacro
{
1912 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
1913 impl Parse
for ItemType
{
1914 fn parse(input
: ParseStream
) -> Result
<Self> {
1916 attrs
: input
.call(Attribute
::parse_outer
)?
,
1917 vis
: input
.parse()?
,
1918 type_token
: input
.parse()?
,
1919 ident
: input
.parse()?
,
1921 let mut generics
: Generics
= input
.parse()?
;
1922 generics
.where_clause
= input
.parse()?
;
1925 eq_token
: input
.parse()?
,
1927 semi_token
: input
.parse()?
,
1932 fn parse_item_type(begin
: ParseBuffer
, input
: ParseStream
) -> Result
<Item
> {
1933 let FlexibleItemType
{
1945 if defaultness
.is_some() || colon_token
.is_some() || ty
.is_none() {
1946 Ok(Item
::Verbatim(verbatim
::between(begin
, input
)))
1948 let (eq_token
, ty
) = ty
.unwrap();
1949 Ok(Item
::Type(ItemType
{
1962 #[cfg(not(feature = "printing"))]
1963 fn item_existential(input
: ParseStream
) -> Result
<TokenStream
> {
1964 Err(input
.error("existential type is not supported"))
1967 #[cfg(feature = "printing")]
1968 fn item_existential(input
: ParseStream
) -> Result
<TokenStream
> {
1969 use crate::attr
::FilterAttrs
;
1970 use quote
::{ToTokens, TokenStreamExt}
;
1972 let attrs
= input
.call(Attribute
::parse_outer
)?
;
1973 let vis
: Visibility
= input
.parse()?
;
1974 let existential_token
: existential
= input
.parse()?
;
1975 let type_token
: Token
![type] = input
.parse()?
;
1976 let ident
: Ident
= input
.parse()?
;
1978 let mut generics
: Generics
= input
.parse()?
;
1979 generics
.where_clause
= input
.parse()?
;
1981 let colon_token
: Token
![:] = input
.parse()?
;
1983 let mut bounds
= Punctuated
::new();
1984 while !input
.peek(Token
![;]) {
1985 if !bounds
.is_empty() {
1986 bounds
.push_punct(input
.parse
::<Token
![+]>()?
);
1988 bounds
.push_value(input
.parse
::<TypeParamBound
>()?
);
1991 let semi_token
: Token
![;] = input
.parse()?
;
1993 let mut tokens
= TokenStream
::new();
1994 tokens
.append_all(attrs
.outer());
1995 vis
.to_tokens(&mut tokens
);
1996 existential_token
.to_tokens(&mut tokens
);
1997 type_token
.to_tokens(&mut tokens
);
1998 ident
.to_tokens(&mut tokens
);
1999 generics
.to_tokens(&mut tokens
);
2000 generics
.where_clause
.to_tokens(&mut tokens
);
2001 if !bounds
.is_empty() {
2002 colon_token
.to_tokens(&mut tokens
);
2003 bounds
.to_tokens(&mut tokens
);
2005 semi_token
.to_tokens(&mut tokens
);
2009 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2010 impl Parse
for ItemStruct
{
2011 fn parse(input
: ParseStream
) -> Result
<Self> {
2012 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2013 let vis
= input
.parse
::<Visibility
>()?
;
2014 let struct_token
= input
.parse
::<Token
![struct]>()?
;
2015 let ident
= input
.parse
::<Ident
>()?
;
2016 let generics
= input
.parse
::<Generics
>()?
;
2017 let (where_clause
, fields
, semi_token
) =
2018 derive
::parsing
::data_struct(input
, &mut attrs
)?
;
2024 generics
: Generics
{
2034 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2035 impl Parse
for ItemEnum
{
2036 fn parse(input
: ParseStream
) -> Result
<Self> {
2037 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2038 let vis
= input
.parse
::<Visibility
>()?
;
2039 let enum_token
= input
.parse
::<Token
![enum]>()?
;
2040 let ident
= input
.parse
::<Ident
>()?
;
2041 let generics
= input
.parse
::<Generics
>()?
;
2042 let (where_clause
, brace_token
, variants
) =
2043 derive
::parsing
::data_enum(input
, &mut attrs
)?
;
2049 generics
: Generics
{
2059 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2060 impl Parse
for ItemUnion
{
2061 fn parse(input
: ParseStream
) -> Result
<Self> {
2062 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2063 let vis
= input
.parse
::<Visibility
>()?
;
2064 let union_token
= input
.parse
::<Token
![union]>()?
;
2065 let ident
= input
.parse
::<Ident
>()?
;
2066 let generics
= input
.parse
::<Generics
>()?
;
2067 let (where_clause
, fields
) = derive
::parsing
::data_union(input
, &mut attrs
)?
;
2073 generics
: Generics
{
2082 fn parse_trait_or_trait_alias(input
: ParseStream
) -> Result
<Item
> {
2083 let (attrs
, vis
, trait_token
, ident
, generics
) = parse_start_of_trait_alias(input
)?
;
2084 let lookahead
= input
.lookahead1();
2085 if lookahead
.peek(token
::Brace
)
2086 || lookahead
.peek(Token
![:])
2087 || lookahead
.peek(Token
![where])
2089 let unsafety
= None
;
2090 let auto_token
= None
;
2091 parse_rest_of_trait(
2102 } else if lookahead
.peek(Token
![=]) {
2103 parse_rest_of_trait_alias(input
, attrs
, vis
, trait_token
, ident
, generics
)
2104 .map(Item
::TraitAlias
)
2106 Err(lookahead
.error())
2110 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2111 impl Parse
for ItemTrait
{
2112 fn parse(input
: ParseStream
) -> Result
<Self> {
2113 let outer_attrs
= input
.call(Attribute
::parse_outer
)?
;
2114 let vis
: Visibility
= input
.parse()?
;
2115 let unsafety
: Option
<Token
![unsafe]> = input
.parse()?
;
2116 let auto_token
: Option
<Token
![auto]> = input
.parse()?
;
2117 let trait_token
: Token
![trait] = input
.parse()?
;
2118 let ident
: Ident
= input
.parse()?
;
2119 let generics
: Generics
= input
.parse()?
;
2120 parse_rest_of_trait(
2133 fn parse_rest_of_trait(
2135 mut attrs
: Vec
<Attribute
>,
2137 unsafety
: Option
<Token
![unsafe]>,
2138 auto_token
: Option
<Token
![auto]>,
2139 trait_token
: Token
![trait],
2141 mut generics
: Generics
,
2142 ) -> Result
<ItemTrait
> {
2143 let colon_token
: Option
<Token
![:]> = input
.parse()?
;
2145 let mut supertraits
= Punctuated
::new();
2146 if colon_token
.is_some() {
2148 if input
.peek(Token
![where]) || input
.peek(token
::Brace
) {
2151 supertraits
.push_value(input
.parse()?
);
2152 if input
.peek(Token
![where]) || input
.peek(token
::Brace
) {
2155 supertraits
.push_punct(input
.parse()?
);
2159 generics
.where_clause
= input
.parse()?
;
2162 let brace_token
= braced
!(content
in input
);
2163 attr
::parsing
::parse_inner(&content
, &mut attrs
)?
;
2164 let mut items
= Vec
::new();
2165 while !content
.is_empty() {
2166 items
.push(content
.parse()?
);
2184 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2185 impl Parse
for ItemTraitAlias
{
2186 fn parse(input
: ParseStream
) -> Result
<Self> {
2187 let (attrs
, vis
, trait_token
, ident
, generics
) = parse_start_of_trait_alias(input
)?
;
2188 parse_rest_of_trait_alias(input
, attrs
, vis
, trait_token
, ident
, generics
)
2192 fn parse_start_of_trait_alias(
2194 ) -> Result
<(Vec
<Attribute
>, Visibility
, Token
![trait], Ident
, Generics
)> {
2195 let attrs
= input
.call(Attribute
::parse_outer
)?
;
2196 let vis
: Visibility
= input
.parse()?
;
2197 let trait_token
: Token
![trait] = input
.parse()?
;
2198 let ident
: Ident
= input
.parse()?
;
2199 let generics
: Generics
= input
.parse()?
;
2200 Ok((attrs
, vis
, trait_token
, ident
, generics
))
2203 fn parse_rest_of_trait_alias(
2205 attrs
: Vec
<Attribute
>,
2207 trait_token
: Token
![trait],
2209 mut generics
: Generics
,
2210 ) -> Result
<ItemTraitAlias
> {
2211 let eq_token
: Token
![=] = input
.parse()?
;
2213 let mut bounds
= Punctuated
::new();
2215 if input
.peek(Token
![where]) || input
.peek(Token
![;]) {
2218 bounds
.push_value(input
.parse()?
);
2219 if input
.peek(Token
![where]) || input
.peek(Token
![;]) {
2222 bounds
.push_punct(input
.parse()?
);
2225 generics
.where_clause
= input
.parse()?
;
2226 let semi_token
: Token
![;] = input
.parse()?
;
2240 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2241 impl Parse
for TraitItem
{
2242 fn parse(input
: ParseStream
) -> Result
<Self> {
2243 let begin
= input
.fork();
2244 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2245 let vis
: Visibility
= input
.parse()?
;
2246 let defaultness
: Option
<Token
![default]> = input
.parse()?
;
2247 let ahead
= input
.fork();
2249 let lookahead
= ahead
.lookahead1();
2250 let mut item
= if lookahead
.peek(Token
![fn]) || peek_signature(&ahead
) {
2251 input
.parse().map(TraitItem
::Method
)
2252 } else if lookahead
.peek(Token
![const]) {
2253 ahead
.parse
::<Token
![const]>()?
;
2254 let lookahead
= ahead
.lookahead1();
2255 if lookahead
.peek(Ident
) || lookahead
.peek(Token
![_
]) {
2256 input
.parse().map(TraitItem
::Const
)
2257 } else if lookahead
.peek(Token
![async
])
2258 || lookahead
.peek(Token
![unsafe])
2259 || lookahead
.peek(Token
![extern])
2260 || lookahead
.peek(Token
![fn])
2262 input
.parse().map(TraitItem
::Method
)
2264 Err(lookahead
.error())
2266 } else if lookahead
.peek(Token
![type]) {
2267 parse_trait_item_type(begin
.fork(), input
)
2268 } else if lookahead
.peek(Ident
)
2269 || lookahead
.peek(Token
![self])
2270 || lookahead
.peek(Token
![super])
2271 || lookahead
.peek(Token
![crate])
2272 || lookahead
.peek(Token
![::])
2274 input
.parse().map(TraitItem
::Macro
)
2276 Err(lookahead
.error())
2279 match (vis
, defaultness
) {
2280 (Visibility
::Inherited
, None
) => {}
2281 _
=> return Ok(TraitItem
::Verbatim(verbatim
::between(begin
, input
))),
2284 let item_attrs
= match &mut item
{
2285 TraitItem
::Const(item
) => &mut item
.attrs
,
2286 TraitItem
::Method(item
) => &mut item
.attrs
,
2287 TraitItem
::Type(item
) => &mut item
.attrs
,
2288 TraitItem
::Macro(item
) => &mut item
.attrs
,
2289 TraitItem
::Verbatim(_
) => unreachable
!(),
2292 TraitItem
::__TestExhaustive(_
) => unimplemented
!(),
2294 _
=> unreachable
!(),
2296 attrs
.append(item_attrs
);
2297 *item_attrs
= attrs
;
2302 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2303 impl Parse
for TraitItemConst
{
2304 fn parse(input
: ParseStream
) -> Result
<Self> {
2306 attrs
: input
.call(Attribute
::parse_outer
)?
,
2307 const_token
: input
.parse()?
,
2309 let lookahead
= input
.lookahead1();
2310 if lookahead
.peek(Ident
) || lookahead
.peek(Token
![_
]) {
2311 input
.call(Ident
::parse_any
)?
2313 return Err(lookahead
.error());
2316 colon_token
: input
.parse()?
,
2319 if input
.peek(Token
![=]) {
2320 let eq_token
: Token
![=] = input
.parse()?
;
2321 let default: Expr
= input
.parse()?
;
2322 Some((eq_token
, default))
2327 semi_token
: input
.parse()?
,
2332 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2333 impl Parse
for TraitItemMethod
{
2334 fn parse(input
: ParseStream
) -> Result
<Self> {
2335 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2336 let sig
: Signature
= input
.parse()?
;
2338 let lookahead
= input
.lookahead1();
2339 let (brace_token
, stmts
, semi_token
) = if lookahead
.peek(token
::Brace
) {
2341 let brace_token
= braced
!(content
in input
);
2342 attr
::parsing
::parse_inner(&content
, &mut attrs
)?
;
2343 let stmts
= content
.call(Block
::parse_within
)?
;
2344 (Some(brace_token
), stmts
, None
)
2345 } else if lookahead
.peek(Token
![;]) {
2346 let semi_token
: Token
![;] = input
.parse()?
;
2347 (None
, Vec
::new(), Some(semi_token
))
2349 return Err(lookahead
.error());
2352 Ok(TraitItemMethod
{
2355 default: brace_token
.map(|brace_token
| Block { brace_token, stmts }
),
2361 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2362 impl Parse
for TraitItemType
{
2363 fn parse(input
: ParseStream
) -> Result
<Self> {
2364 let attrs
= input
.call(Attribute
::parse_outer
)?
;
2365 let type_token
: Token
![type] = input
.parse()?
;
2366 let ident
: Ident
= input
.parse()?
;
2367 let mut generics
: Generics
= input
.parse()?
;
2368 let colon_token
: Option
<Token
![:]> = input
.parse()?
;
2370 let mut bounds
= Punctuated
::new();
2371 if colon_token
.is_some() {
2372 while !input
.peek(Token
![where]) && !input
.peek(Token
![=]) && !input
.peek(Token
![;])
2374 if !bounds
.is_empty() {
2375 bounds
.push_punct(input
.parse()?
);
2377 bounds
.push_value(input
.parse()?
);
2381 generics
.where_clause
= input
.parse()?
;
2382 let default = if input
.peek(Token
![=]) {
2383 let eq_token
: Token
![=] = input
.parse()?
;
2384 let default: Type
= input
.parse()?
;
2385 Some((eq_token
, default))
2389 let semi_token
: Token
![;] = input
.parse()?
;
2404 fn parse_trait_item_type(begin
: ParseBuffer
, input
: ParseStream
) -> Result
<TraitItem
> {
2405 let FlexibleItemType
{
2417 if defaultness
.is_some() || vis
.is_some() {
2418 Ok(TraitItem
::Verbatim(verbatim
::between(begin
, input
)))
2420 Ok(TraitItem
::Type(TraitItemType
{
2433 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2434 impl Parse
for TraitItemMacro
{
2435 fn parse(input
: ParseStream
) -> Result
<Self> {
2436 let attrs
= input
.call(Attribute
::parse_outer
)?
;
2437 let mac
: Macro
= input
.parse()?
;
2438 let semi_token
: Option
<Token
![;]> = if mac
.delimiter
.is_brace() {
2441 Some(input
.parse()?
)
2451 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2452 impl Parse
for ItemImpl
{
2453 fn parse(input
: ParseStream
) -> Result
<Self> {
2454 let allow_verbatim_impl
= false;
2455 parse_impl(input
, allow_verbatim_impl
).map(Option
::unwrap
)
2459 fn parse_impl(input
: ParseStream
, allow_verbatim_impl
: bool
) -> Result
<Option
<ItemImpl
>> {
2460 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2461 let has_visibility
= allow_verbatim_impl
&& input
.parse
::<Visibility
>()?
.is_some();
2462 let defaultness
: Option
<Token
![default]> = input
.parse()?
;
2463 let unsafety
: Option
<Token
![unsafe]> = input
.parse()?
;
2464 let impl_token
: Token
![impl] = input
.parse()?
;
2466 let has_generics
= input
.peek(Token
![<])
2467 && (input
.peek2(Token
![>])
2468 || input
.peek2(Token
![#])
2469 || (input
.peek2(Ident
) || input
.peek2(Lifetime
))
2470 && (input
.peek3(Token
![:])
2471 || input
.peek3(Token
![,])
2472 || input
.peek3(Token
![>])
2473 || input
.peek3(Token
![=]))
2474 || input
.peek2(Token
![const]));
2475 let mut generics
: Generics
= if has_generics
{
2481 let is_const_impl
= allow_verbatim_impl
2482 && (input
.peek(Token
![const]) || input
.peek(Token
![?
]) && input
.peek2(Token
![const]));
2484 input
.parse
::<Option
<Token
![?
]>>()?
;
2485 input
.parse
::<Token
![const]>()?
;
2488 let begin
= input
.fork();
2489 let polarity
= if input
.peek(Token
![!]) && !input
.peek2(token
::Brace
) {
2490 Some(input
.parse
::<Token
![!]>()?
)
2495 #[cfg(not(feature = "printing"))]
2496 let first_ty_span
= input
.span();
2497 let mut first_ty
: Type
= input
.parse()?
;
2501 let is_impl_for
= input
.peek(Token
![for]);
2503 let for_token
: Token
![for] = input
.parse()?
;
2504 let mut first_ty_ref
= &first_ty
;
2505 while let Type
::Group(ty
) = first_ty_ref
{
2506 first_ty_ref
= &ty
.elem
;
2508 if let Type
::Path(_
) = first_ty_ref
{
2509 while let Type
::Group(ty
) = first_ty
{
2510 first_ty
= *ty
.elem
;
2512 if let Type
::Path(TypePath { qself: None, path }
) = first_ty
{
2513 trait_
= Some((polarity
, path
, for_token
));
2517 } else if !allow_verbatim_impl
{
2518 #[cfg(feature = "printing")]
2519 return Err(Error
::new_spanned(first_ty_ref
, "expected trait path"));
2520 #[cfg(not(feature = "printing"))]
2521 return Err(Error
::new(first_ty_span
, "expected trait path"));
2525 self_ty
= input
.parse()?
;
2528 self_ty
= if polarity
.is_none() {
2531 Type
::Verbatim(verbatim
::between(begin
, input
))
2535 generics
.where_clause
= input
.parse()?
;
2538 let brace_token
= braced
!(content
in input
);
2539 attr
::parsing
::parse_inner(&content
, &mut attrs
)?
;
2541 let mut items
= Vec
::new();
2542 while !content
.is_empty() {
2543 items
.push(content
.parse()?
);
2546 if has_visibility
|| is_const_impl
|| is_impl_for
&& trait_
.is_none() {
2556 self_ty
: Box
::new(self_ty
),
2563 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2564 impl Parse
for ImplItem
{
2565 fn parse(input
: ParseStream
) -> Result
<Self> {
2566 let begin
= input
.fork();
2567 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2568 let ahead
= input
.fork();
2569 let vis
: Visibility
= ahead
.parse()?
;
2571 let mut lookahead
= ahead
.lookahead1();
2572 let defaultness
= if lookahead
.peek(Token
![default]) && !ahead
.peek2(Token
![!]) {
2573 let defaultness
: Token
![default] = ahead
.parse()?
;
2574 lookahead
= ahead
.lookahead1();
2580 let mut item
= if lookahead
.peek(Token
![fn]) || peek_signature(&ahead
) {
2581 input
.parse().map(ImplItem
::Method
)
2582 } else if lookahead
.peek(Token
![const]) {
2583 let const_token
: Token
![const] = ahead
.parse()?
;
2584 let lookahead
= ahead
.lookahead1();
2585 if lookahead
.peek(Ident
) || lookahead
.peek(Token
![_
]) {
2586 input
.advance_to(&ahead
);
2587 let ident
: Ident
= input
.call(Ident
::parse_any
)?
;
2588 let colon_token
: Token
![:] = input
.parse()?
;
2589 let ty
: Type
= input
.parse()?
;
2590 if let Some(eq_token
) = input
.parse()?
{
2591 return Ok(ImplItem
::Const(ImplItemConst
{
2600 expr
: input
.parse()?
,
2601 semi_token
: input
.parse()?
,
2604 input
.parse
::<Token
![;]>()?
;
2605 return Ok(ImplItem
::Verbatim(verbatim
::between(begin
, input
)));
2608 Err(lookahead
.error())
2610 } else if lookahead
.peek(Token
![type]) {
2611 parse_impl_item_type(begin
, input
)
2612 } else if vis
.is_inherited() && defaultness
.is_none() && lookahead
.peek(existential
) {
2613 input
.call(item_existential
).map(ImplItem
::Verbatim
)
2614 } else if vis
.is_inherited()
2615 && defaultness
.is_none()
2616 && (lookahead
.peek(Ident
)
2617 || lookahead
.peek(Token
![self])
2618 || lookahead
.peek(Token
![super])
2619 || lookahead
.peek(Token
![crate])
2620 || lookahead
.peek(Token
![::]))
2622 input
.parse().map(ImplItem
::Macro
)
2624 Err(lookahead
.error())
2628 let item_attrs
= match &mut item
{
2629 ImplItem
::Const(item
) => &mut item
.attrs
,
2630 ImplItem
::Method(item
) => &mut item
.attrs
,
2631 ImplItem
::Type(item
) => &mut item
.attrs
,
2632 ImplItem
::Macro(item
) => &mut item
.attrs
,
2633 ImplItem
::Verbatim(_
) => return Ok(item
),
2636 ImplItem
::__TestExhaustive(_
) => unimplemented
!(),
2638 _
=> unreachable
!(),
2640 attrs
.append(item_attrs
);
2641 *item_attrs
= attrs
;
2648 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2649 impl Parse
for ImplItemConst
{
2650 fn parse(input
: ParseStream
) -> Result
<Self> {
2652 attrs
: input
.call(Attribute
::parse_outer
)?
,
2653 vis
: input
.parse()?
,
2654 defaultness
: input
.parse()?
,
2655 const_token
: input
.parse()?
,
2657 let lookahead
= input
.lookahead1();
2658 if lookahead
.peek(Ident
) || lookahead
.peek(Token
![_
]) {
2659 input
.call(Ident
::parse_any
)?
2661 return Err(lookahead
.error());
2664 colon_token
: input
.parse()?
,
2666 eq_token
: input
.parse()?
,
2667 expr
: input
.parse()?
,
2668 semi_token
: input
.parse()?
,
2673 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2674 impl Parse
for ImplItemMethod
{
2675 fn parse(input
: ParseStream
) -> Result
<Self> {
2676 let mut attrs
= input
.call(Attribute
::parse_outer
)?
;
2677 let vis
: Visibility
= input
.parse()?
;
2678 let defaultness
: Option
<Token
![default]> = input
.parse()?
;
2679 let sig
: Signature
= input
.parse()?
;
2681 let block
= if let Some(semi
) = input
.parse
::<Option
<Token
![;]>>()?
{
2682 // Accept methods without a body in an impl block because
2683 // rustc's *parser* does not reject them (the compilation error
2684 // is emitted later than parsing) and it can be useful for macro
2686 let mut punct
= Punct
::new('
;'
, Spacing
::Alone
);
2687 punct
.set_span(semi
.span
);
2688 let tokens
= TokenStream
::from_iter(vec
![TokenTree
::Punct(punct
)]);
2690 brace_token
: Brace
::default(),
2691 stmts
: vec
![Stmt
::Item(Item
::Verbatim(tokens
))],
2695 let brace_token
= braced
!(content
in input
);
2696 attrs
.extend(content
.call(Attribute
::parse_inner
)?
);
2699 stmts
: content
.call(Block
::parse_within
)?
,
2713 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2714 impl Parse
for ImplItemType
{
2715 fn parse(input
: ParseStream
) -> Result
<Self> {
2717 attrs
: input
.call(Attribute
::parse_outer
)?
,
2718 vis
: input
.parse()?
,
2719 defaultness
: input
.parse()?
,
2720 type_token
: input
.parse()?
,
2721 ident
: input
.parse()?
,
2723 let mut generics
: Generics
= input
.parse()?
;
2724 generics
.where_clause
= input
.parse()?
;
2727 eq_token
: input
.parse()?
,
2729 semi_token
: input
.parse()?
,
2734 fn parse_impl_item_type(begin
: ParseBuffer
, input
: ParseStream
) -> Result
<ImplItem
> {
2735 let FlexibleItemType
{
2747 if colon_token
.is_some() || ty
.is_none() {
2748 Ok(ImplItem
::Verbatim(verbatim
::between(begin
, input
)))
2750 let (eq_token
, ty
) = ty
.unwrap();
2751 Ok(ImplItem
::Type(ImplItemType
{
2765 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
2766 impl Parse
for ImplItemMacro
{
2767 fn parse(input
: ParseStream
) -> Result
<Self> {
2768 let attrs
= input
.call(Attribute
::parse_outer
)?
;
2769 let mac
: Macro
= input
.parse()?
;
2770 let semi_token
: Option
<Token
![;]> = if mac
.delimiter
.is_brace() {
2773 Some(input
.parse()?
)
2784 fn is_inherited(&self) -> bool
{
2786 Visibility
::Inherited
=> true,
2792 impl MacroDelimiter
{
2793 fn is_brace(&self) -> bool
{
2795 MacroDelimiter
::Brace(_
) => true,
2796 MacroDelimiter
::Paren(_
) | MacroDelimiter
::Bracket(_
) => false,
2802 #[cfg(feature = "printing")]
2805 use crate::attr
::FilterAttrs
;
2806 use crate::print
::TokensOrDefault
;
2807 use crate::punctuated
::Pair
;
2808 use proc_macro2
::TokenStream
;
2809 use quote
::{ToTokens, TokenStreamExt}
;
2811 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2812 impl ToTokens
for ItemExternCrate
{
2813 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2814 tokens
.append_all(self.attrs
.outer());
2815 self.vis
.to_tokens(tokens
);
2816 self.extern_token
.to_tokens(tokens
);
2817 self.crate_token
.to_tokens(tokens
);
2818 self.ident
.to_tokens(tokens
);
2819 if let Some((as_token
, rename
)) = &self.rename
{
2820 as_token
.to_tokens(tokens
);
2821 rename
.to_tokens(tokens
);
2823 self.semi_token
.to_tokens(tokens
);
2827 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2828 impl ToTokens
for ItemUse
{
2829 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2830 tokens
.append_all(self.attrs
.outer());
2831 self.vis
.to_tokens(tokens
);
2832 self.use_token
.to_tokens(tokens
);
2833 self.leading_colon
.to_tokens(tokens
);
2834 self.tree
.to_tokens(tokens
);
2835 self.semi_token
.to_tokens(tokens
);
2839 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2840 impl ToTokens
for ItemStatic
{
2841 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2842 tokens
.append_all(self.attrs
.outer());
2843 self.vis
.to_tokens(tokens
);
2844 self.static_token
.to_tokens(tokens
);
2845 self.mutability
.to_tokens(tokens
);
2846 self.ident
.to_tokens(tokens
);
2847 self.colon_token
.to_tokens(tokens
);
2848 self.ty
.to_tokens(tokens
);
2849 self.eq_token
.to_tokens(tokens
);
2850 self.expr
.to_tokens(tokens
);
2851 self.semi_token
.to_tokens(tokens
);
2855 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2856 impl ToTokens
for ItemConst
{
2857 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2858 tokens
.append_all(self.attrs
.outer());
2859 self.vis
.to_tokens(tokens
);
2860 self.const_token
.to_tokens(tokens
);
2861 self.ident
.to_tokens(tokens
);
2862 self.colon_token
.to_tokens(tokens
);
2863 self.ty
.to_tokens(tokens
);
2864 self.eq_token
.to_tokens(tokens
);
2865 self.expr
.to_tokens(tokens
);
2866 self.semi_token
.to_tokens(tokens
);
2870 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2871 impl ToTokens
for ItemFn
{
2872 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2873 tokens
.append_all(self.attrs
.outer());
2874 self.vis
.to_tokens(tokens
);
2875 self.sig
.to_tokens(tokens
);
2876 self.block
.brace_token
.surround(tokens
, |tokens
| {
2877 tokens
.append_all(self.attrs
.inner());
2878 tokens
.append_all(&self.block
.stmts
);
2883 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2884 impl ToTokens
for ItemMod
{
2885 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2886 tokens
.append_all(self.attrs
.outer());
2887 self.vis
.to_tokens(tokens
);
2888 self.mod_token
.to_tokens(tokens
);
2889 self.ident
.to_tokens(tokens
);
2890 if let Some((brace
, items
)) = &self.content
{
2891 brace
.surround(tokens
, |tokens
| {
2892 tokens
.append_all(self.attrs
.inner());
2893 tokens
.append_all(items
);
2896 TokensOrDefault(&self.semi
).to_tokens(tokens
);
2901 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2902 impl ToTokens
for ItemForeignMod
{
2903 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2904 tokens
.append_all(self.attrs
.outer());
2905 self.abi
.to_tokens(tokens
);
2906 self.brace_token
.surround(tokens
, |tokens
| {
2907 tokens
.append_all(self.attrs
.inner());
2908 tokens
.append_all(&self.items
);
2913 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2914 impl ToTokens
for ItemType
{
2915 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2916 tokens
.append_all(self.attrs
.outer());
2917 self.vis
.to_tokens(tokens
);
2918 self.type_token
.to_tokens(tokens
);
2919 self.ident
.to_tokens(tokens
);
2920 self.generics
.to_tokens(tokens
);
2921 self.generics
.where_clause
.to_tokens(tokens
);
2922 self.eq_token
.to_tokens(tokens
);
2923 self.ty
.to_tokens(tokens
);
2924 self.semi_token
.to_tokens(tokens
);
2928 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2929 impl ToTokens
for ItemEnum
{
2930 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2931 tokens
.append_all(self.attrs
.outer());
2932 self.vis
.to_tokens(tokens
);
2933 self.enum_token
.to_tokens(tokens
);
2934 self.ident
.to_tokens(tokens
);
2935 self.generics
.to_tokens(tokens
);
2936 self.generics
.where_clause
.to_tokens(tokens
);
2937 self.brace_token
.surround(tokens
, |tokens
| {
2938 self.variants
.to_tokens(tokens
);
2943 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2944 impl ToTokens
for ItemStruct
{
2945 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2946 tokens
.append_all(self.attrs
.outer());
2947 self.vis
.to_tokens(tokens
);
2948 self.struct_token
.to_tokens(tokens
);
2949 self.ident
.to_tokens(tokens
);
2950 self.generics
.to_tokens(tokens
);
2951 match &self.fields
{
2952 Fields
::Named(fields
) => {
2953 self.generics
.where_clause
.to_tokens(tokens
);
2954 fields
.to_tokens(tokens
);
2956 Fields
::Unnamed(fields
) => {
2957 fields
.to_tokens(tokens
);
2958 self.generics
.where_clause
.to_tokens(tokens
);
2959 TokensOrDefault(&self.semi_token
).to_tokens(tokens
);
2962 self.generics
.where_clause
.to_tokens(tokens
);
2963 TokensOrDefault(&self.semi_token
).to_tokens(tokens
);
2969 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2970 impl ToTokens
for ItemUnion
{
2971 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2972 tokens
.append_all(self.attrs
.outer());
2973 self.vis
.to_tokens(tokens
);
2974 self.union_token
.to_tokens(tokens
);
2975 self.ident
.to_tokens(tokens
);
2976 self.generics
.to_tokens(tokens
);
2977 self.generics
.where_clause
.to_tokens(tokens
);
2978 self.fields
.to_tokens(tokens
);
2982 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
2983 impl ToTokens
for ItemTrait
{
2984 fn to_tokens(&self, tokens
: &mut TokenStream
) {
2985 tokens
.append_all(self.attrs
.outer());
2986 self.vis
.to_tokens(tokens
);
2987 self.unsafety
.to_tokens(tokens
);
2988 self.auto_token
.to_tokens(tokens
);
2989 self.trait_token
.to_tokens(tokens
);
2990 self.ident
.to_tokens(tokens
);
2991 self.generics
.to_tokens(tokens
);
2992 if !self.supertraits
.is_empty() {
2993 TokensOrDefault(&self.colon_token
).to_tokens(tokens
);
2994 self.supertraits
.to_tokens(tokens
);
2996 self.generics
.where_clause
.to_tokens(tokens
);
2997 self.brace_token
.surround(tokens
, |tokens
| {
2998 tokens
.append_all(self.attrs
.inner());
2999 tokens
.append_all(&self.items
);
3004 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3005 impl ToTokens
for ItemTraitAlias
{
3006 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3007 tokens
.append_all(self.attrs
.outer());
3008 self.vis
.to_tokens(tokens
);
3009 self.trait_token
.to_tokens(tokens
);
3010 self.ident
.to_tokens(tokens
);
3011 self.generics
.to_tokens(tokens
);
3012 self.eq_token
.to_tokens(tokens
);
3013 self.bounds
.to_tokens(tokens
);
3014 self.generics
.where_clause
.to_tokens(tokens
);
3015 self.semi_token
.to_tokens(tokens
);
3019 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3020 impl ToTokens
for ItemImpl
{
3021 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3022 tokens
.append_all(self.attrs
.outer());
3023 self.defaultness
.to_tokens(tokens
);
3024 self.unsafety
.to_tokens(tokens
);
3025 self.impl_token
.to_tokens(tokens
);
3026 self.generics
.to_tokens(tokens
);
3027 if let Some((polarity
, path
, for_token
)) = &self.trait_
{
3028 polarity
.to_tokens(tokens
);
3029 path
.to_tokens(tokens
);
3030 for_token
.to_tokens(tokens
);
3032 self.self_ty
.to_tokens(tokens
);
3033 self.generics
.where_clause
.to_tokens(tokens
);
3034 self.brace_token
.surround(tokens
, |tokens
| {
3035 tokens
.append_all(self.attrs
.inner());
3036 tokens
.append_all(&self.items
);
3041 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3042 impl ToTokens
for ItemMacro
{
3043 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3044 tokens
.append_all(self.attrs
.outer());
3045 self.mac
.path
.to_tokens(tokens
);
3046 self.mac
.bang_token
.to_tokens(tokens
);
3047 self.ident
.to_tokens(tokens
);
3048 match &self.mac
.delimiter
{
3049 MacroDelimiter
::Paren(paren
) => {
3050 paren
.surround(tokens
, |tokens
| self.mac
.tokens
.to_tokens(tokens
));
3052 MacroDelimiter
::Brace(brace
) => {
3053 brace
.surround(tokens
, |tokens
| self.mac
.tokens
.to_tokens(tokens
));
3055 MacroDelimiter
::Bracket(bracket
) => {
3056 bracket
.surround(tokens
, |tokens
| self.mac
.tokens
.to_tokens(tokens
));
3059 self.semi_token
.to_tokens(tokens
);
3063 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3064 impl ToTokens
for ItemMacro2
{
3065 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3066 tokens
.append_all(self.attrs
.outer());
3067 self.vis
.to_tokens(tokens
);
3068 self.macro_token
.to_tokens(tokens
);
3069 self.ident
.to_tokens(tokens
);
3070 self.rules
.to_tokens(tokens
);
3074 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3075 impl ToTokens
for UsePath
{
3076 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3077 self.ident
.to_tokens(tokens
);
3078 self.colon2_token
.to_tokens(tokens
);
3079 self.tree
.to_tokens(tokens
);
3083 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3084 impl ToTokens
for UseName
{
3085 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3086 self.ident
.to_tokens(tokens
);
3090 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3091 impl ToTokens
for UseRename
{
3092 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3093 self.ident
.to_tokens(tokens
);
3094 self.as_token
.to_tokens(tokens
);
3095 self.rename
.to_tokens(tokens
);
3099 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3100 impl ToTokens
for UseGlob
{
3101 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3102 self.star_token
.to_tokens(tokens
);
3106 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3107 impl ToTokens
for UseGroup
{
3108 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3109 self.brace_token
.surround(tokens
, |tokens
| {
3110 self.items
.to_tokens(tokens
);
3115 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3116 impl ToTokens
for TraitItemConst
{
3117 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3118 tokens
.append_all(self.attrs
.outer());
3119 self.const_token
.to_tokens(tokens
);
3120 self.ident
.to_tokens(tokens
);
3121 self.colon_token
.to_tokens(tokens
);
3122 self.ty
.to_tokens(tokens
);
3123 if let Some((eq_token
, default)) = &self.default {
3124 eq_token
.to_tokens(tokens
);
3125 default.to_tokens(tokens
);
3127 self.semi_token
.to_tokens(tokens
);
3131 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3132 impl ToTokens
for TraitItemMethod
{
3133 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3134 tokens
.append_all(self.attrs
.outer());
3135 self.sig
.to_tokens(tokens
);
3136 match &self.default {
3138 block
.brace_token
.surround(tokens
, |tokens
| {
3139 tokens
.append_all(self.attrs
.inner());
3140 tokens
.append_all(&block
.stmts
);
3144 TokensOrDefault(&self.semi_token
).to_tokens(tokens
);
3150 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3151 impl ToTokens
for TraitItemType
{
3152 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3153 tokens
.append_all(self.attrs
.outer());
3154 self.type_token
.to_tokens(tokens
);
3155 self.ident
.to_tokens(tokens
);
3156 self.generics
.to_tokens(tokens
);
3157 if !self.bounds
.is_empty() {
3158 TokensOrDefault(&self.colon_token
).to_tokens(tokens
);
3159 self.bounds
.to_tokens(tokens
);
3161 self.generics
.where_clause
.to_tokens(tokens
);
3162 if let Some((eq_token
, default)) = &self.default {
3163 eq_token
.to_tokens(tokens
);
3164 default.to_tokens(tokens
);
3166 self.semi_token
.to_tokens(tokens
);
3170 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3171 impl ToTokens
for TraitItemMacro
{
3172 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3173 tokens
.append_all(self.attrs
.outer());
3174 self.mac
.to_tokens(tokens
);
3175 self.semi_token
.to_tokens(tokens
);
3179 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3180 impl ToTokens
for ImplItemConst
{
3181 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3182 tokens
.append_all(self.attrs
.outer());
3183 self.vis
.to_tokens(tokens
);
3184 self.defaultness
.to_tokens(tokens
);
3185 self.const_token
.to_tokens(tokens
);
3186 self.ident
.to_tokens(tokens
);
3187 self.colon_token
.to_tokens(tokens
);
3188 self.ty
.to_tokens(tokens
);
3189 self.eq_token
.to_tokens(tokens
);
3190 self.expr
.to_tokens(tokens
);
3191 self.semi_token
.to_tokens(tokens
);
3195 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3196 impl ToTokens
for ImplItemMethod
{
3197 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3198 tokens
.append_all(self.attrs
.outer());
3199 self.vis
.to_tokens(tokens
);
3200 self.defaultness
.to_tokens(tokens
);
3201 self.sig
.to_tokens(tokens
);
3202 if self.block
.stmts
.len() == 1 {
3203 if let Stmt
::Item(Item
::Verbatim(verbatim
)) = &self.block
.stmts
[0] {
3204 if verbatim
.to_string() == ";" {
3205 verbatim
.to_tokens(tokens
);
3210 self.block
.brace_token
.surround(tokens
, |tokens
| {
3211 tokens
.append_all(self.attrs
.inner());
3212 tokens
.append_all(&self.block
.stmts
);
3217 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3218 impl ToTokens
for ImplItemType
{
3219 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3220 tokens
.append_all(self.attrs
.outer());
3221 self.vis
.to_tokens(tokens
);
3222 self.defaultness
.to_tokens(tokens
);
3223 self.type_token
.to_tokens(tokens
);
3224 self.ident
.to_tokens(tokens
);
3225 self.generics
.to_tokens(tokens
);
3226 self.generics
.where_clause
.to_tokens(tokens
);
3227 self.eq_token
.to_tokens(tokens
);
3228 self.ty
.to_tokens(tokens
);
3229 self.semi_token
.to_tokens(tokens
);
3233 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3234 impl ToTokens
for ImplItemMacro
{
3235 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3236 tokens
.append_all(self.attrs
.outer());
3237 self.mac
.to_tokens(tokens
);
3238 self.semi_token
.to_tokens(tokens
);
3242 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3243 impl ToTokens
for ForeignItemFn
{
3244 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3245 tokens
.append_all(self.attrs
.outer());
3246 self.vis
.to_tokens(tokens
);
3247 self.sig
.to_tokens(tokens
);
3248 self.semi_token
.to_tokens(tokens
);
3252 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3253 impl ToTokens
for ForeignItemStatic
{
3254 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3255 tokens
.append_all(self.attrs
.outer());
3256 self.vis
.to_tokens(tokens
);
3257 self.static_token
.to_tokens(tokens
);
3258 self.mutability
.to_tokens(tokens
);
3259 self.ident
.to_tokens(tokens
);
3260 self.colon_token
.to_tokens(tokens
);
3261 self.ty
.to_tokens(tokens
);
3262 self.semi_token
.to_tokens(tokens
);
3266 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3267 impl ToTokens
for ForeignItemType
{
3268 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3269 tokens
.append_all(self.attrs
.outer());
3270 self.vis
.to_tokens(tokens
);
3271 self.type_token
.to_tokens(tokens
);
3272 self.ident
.to_tokens(tokens
);
3273 self.semi_token
.to_tokens(tokens
);
3277 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3278 impl ToTokens
for ForeignItemMacro
{
3279 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3280 tokens
.append_all(self.attrs
.outer());
3281 self.mac
.to_tokens(tokens
);
3282 self.semi_token
.to_tokens(tokens
);
3286 fn maybe_variadic_to_tokens(arg
: &FnArg
, tokens
: &mut TokenStream
) -> bool
{
3287 let arg
= match arg
{
3288 FnArg
::Typed(arg
) => arg
,
3289 FnArg
::Receiver(receiver
) => {
3290 receiver
.to_tokens(tokens
);
3295 match arg
.ty
.as_ref() {
3296 Type
::Verbatim(ty
) if ty
.to_string() == "..." => {
3297 match arg
.pat
.as_ref() {
3298 Pat
::Verbatim(pat
) if pat
.to_string() == "..." => {
3299 tokens
.append_all(arg
.attrs
.outer());
3300 pat
.to_tokens(tokens
);
3302 _
=> arg
.to_tokens(tokens
),
3307 arg
.to_tokens(tokens
);
3313 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3314 impl ToTokens
for Signature
{
3315 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3316 self.constness
.to_tokens(tokens
);
3317 self.asyncness
.to_tokens(tokens
);
3318 self.unsafety
.to_tokens(tokens
);
3319 self.abi
.to_tokens(tokens
);
3320 self.fn_token
.to_tokens(tokens
);
3321 self.ident
.to_tokens(tokens
);
3322 self.generics
.to_tokens(tokens
);
3323 self.paren_token
.surround(tokens
, |tokens
| {
3324 let mut last_is_variadic
= false;
3325 for input
in self.inputs
.pairs() {
3327 Pair
::Punctuated(input
, comma
) => {
3328 maybe_variadic_to_tokens(input
, tokens
);
3329 comma
.to_tokens(tokens
);
3331 Pair
::End(input
) => {
3332 last_is_variadic
= maybe_variadic_to_tokens(input
, tokens
);
3336 if self.variadic
.is_some() && !last_is_variadic
{
3337 if !self.inputs
.empty_or_trailing() {
3338 <Token
![,]>::default().to_tokens(tokens
);
3340 self.variadic
.to_tokens(tokens
);
3343 self.output
.to_tokens(tokens
);
3344 self.generics
.where_clause
.to_tokens(tokens
);
3348 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
3349 impl ToTokens
for Receiver
{
3350 fn to_tokens(&self, tokens
: &mut TokenStream
) {
3351 tokens
.append_all(self.attrs
.outer());
3352 if let Some((ampersand
, lifetime
)) = &self.reference
{
3353 ampersand
.to_tokens(tokens
);
3354 lifetime
.to_tokens(tokens
);
3356 self.mutability
.to_tokens(tokens
);
3357 self.self_token
.to_tokens(tokens
);