1use crate::attr::Attribute;
2use crate::data::{Fields, FieldsNamed, Variant};
3use crate::derive::{Data, DataEnum, DataStruct, DataUnion, DeriveInput};
4use crate::expr::Expr;
5use crate::generics::{Generics, TypeParamBound};
6use crate::ident::Ident;
7use crate::lifetime::Lifetime;
8use crate::mac::Macro;
9use crate::pat::{Pat, PatType};
10use crate::path::Path;
11use crate::punctuated::Punctuated;
12use crate::restriction::Visibility;
13use crate::stmt::Block;
14use crate::token;
15use crate::ty::{Abi, ReturnType, Type};
16use alloc::boxed::Box;
17use alloc::vec::Vec;
18#[cfg(feature = "parsing")]
19use core::mem;
20use proc_macro2::TokenStream;
21
22#[doc = r" Things that can appear directly inside of a module or scope."]
#[doc = r""]
#[doc = r" # Syntax tree enum"]
#[doc = r""]
#[doc = r" This type is a [syntax tree enum]."]
#[doc = r""]
#[doc = r" [syntax tree enum]: crate::expr::Expr#syntax-tree-enums"]
#[non_exhaustive]
pub enum Item {
#[doc = r" A constant item: `const MAX: u16 = 65535`."]
Const(ItemConst),
#[doc = r" An enum definition: `enum Foo<A, B> { A(A), B(B) }`."]
Enum(ItemEnum),
#[doc = r" An `extern crate` item: `extern crate serde`."]
ExternCrate(ItemExternCrate),
#[doc =
r" A free-standing function: `fn process(n: usize) -> Result<()> { ..."]
#[doc = r" }`."]
Fn(ItemFn),
#[doc = r#" A block of foreign items: `extern "C" { ... }`."#]
ForeignMod(ItemForeignMod),
#[doc =
r" An impl block providing trait or associated items: `impl<A> Trait"]
#[doc = r" for Data<A> { ... }`."]
Impl(ItemImpl),
#[doc =
r" A macro invocation, which includes `macro_rules!` definitions."]
Macro(ItemMacro),
#[doc = r" A module or module declaration: `mod m` or `mod m { ... }`."]
Mod(ItemMod),
#[doc = r" A static item: `static BIKE: Shed = Shed(42)`."]
Static(ItemStatic),
#[doc = r" A struct definition: `struct Foo<A> { x: A }`."]
Struct(ItemStruct),
#[doc = r" A trait definition: `pub trait Iterator { ... }`."]
Trait(ItemTrait),
#[doc =
r" A trait alias: `pub trait SharableIterator = Iterator + Sync`."]
TraitAlias(ItemTraitAlias),
#[doc =
r" A type alias: `type Result<T> = core::result::Result<T, MyError>`."]
Type(ItemType),
#[doc = r" A union definition: `union Foo<A, B> { x: A, y: B }`."]
Union(ItemUnion),
#[doc = r" A use declaration: `use alloc::collections::HashMap`."]
Use(ItemUse),
#[doc = r" Tokens forming an item not interpreted by Syn."]
Verbatim(TokenStream),
}
impl From<ItemConst> for Item {
fn from(e: ItemConst) -> Item { Item::Const(e) }
}
impl From<ItemEnum> for Item {
fn from(e: ItemEnum) -> Item { Item::Enum(e) }
}
impl From<ItemExternCrate> for Item {
fn from(e: ItemExternCrate) -> Item { Item::ExternCrate(e) }
}
impl From<ItemFn> for Item {
fn from(e: ItemFn) -> Item { Item::Fn(e) }
}
impl From<ItemForeignMod> for Item {
fn from(e: ItemForeignMod) -> Item { Item::ForeignMod(e) }
}
impl From<ItemImpl> for Item {
fn from(e: ItemImpl) -> Item { Item::Impl(e) }
}
impl From<ItemMacro> for Item {
fn from(e: ItemMacro) -> Item { Item::Macro(e) }
}
impl From<ItemMod> for Item {
fn from(e: ItemMod) -> Item { Item::Mod(e) }
}
impl From<ItemStatic> for Item {
fn from(e: ItemStatic) -> Item { Item::Static(e) }
}
impl From<ItemStruct> for Item {
fn from(e: ItemStruct) -> Item { Item::Struct(e) }
}
impl From<ItemTrait> for Item {
fn from(e: ItemTrait) -> Item { Item::Trait(e) }
}
impl From<ItemTraitAlias> for Item {
fn from(e: ItemTraitAlias) -> Item { Item::TraitAlias(e) }
}
impl From<ItemType> for Item {
fn from(e: ItemType) -> Item { Item::Type(e) }
}
impl From<ItemUnion> for Item {
fn from(e: ItemUnion) -> Item { Item::Union(e) }
}
impl From<ItemUse> for Item {
fn from(e: ItemUse) -> Item { Item::Use(e) }
}
impl ::quote::ToTokens for Item {
fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
match self {
Item::Const(_e) => _e.to_tokens(tokens),
Item::Enum(_e) => _e.to_tokens(tokens),
Item::ExternCrate(_e) => _e.to_tokens(tokens),
Item::Fn(_e) => _e.to_tokens(tokens),
Item::ForeignMod(_e) => _e.to_tokens(tokens),
Item::Impl(_e) => _e.to_tokens(tokens),
Item::Macro(_e) => _e.to_tokens(tokens),
Item::Mod(_e) => _e.to_tokens(tokens),
Item::Static(_e) => _e.to_tokens(tokens),
Item::Struct(_e) => _e.to_tokens(tokens),
Item::Trait(_e) => _e.to_tokens(tokens),
Item::TraitAlias(_e) => _e.to_tokens(tokens),
Item::Type(_e) => _e.to_tokens(tokens),
Item::Union(_e) => _e.to_tokens(tokens),
Item::Use(_e) => _e.to_tokens(tokens),
Item::Verbatim(_e) => _e.to_tokens(tokens),
}
}
}ast_enum_of_structs! {
23 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
31 #[non_exhaustive]
32 pub enum Item {
33 Const(ItemConst),
35
36 Enum(ItemEnum),
38
39 ExternCrate(ItemExternCrate),
41
42 Fn(ItemFn),
45
46 ForeignMod(ItemForeignMod),
48
49 Impl(ItemImpl),
52
53 Macro(ItemMacro),
55
56 Mod(ItemMod),
58
59 Static(ItemStatic),
61
62 Struct(ItemStruct),
64
65 Trait(ItemTrait),
67
68 TraitAlias(ItemTraitAlias),
70
71 Type(ItemType),
73
74 Union(ItemUnion),
76
77 Use(ItemUse),
79
80 Verbatim(TokenStream),
82
83 }
101}
102
103#[doc = r" A constant item: `const MAX: u16 = 65535`."]
pub struct ItemConst {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub const_token: crate::token::Const,
pub ident: Ident,
pub generics: Generics,
pub colon_token: crate::token::Colon,
pub ty: Box<Type>,
pub eq_token: crate::token::Eq,
pub expr: Box<Expr>,
pub semi_token: crate::token::Semi,
}ast_struct! {
104 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
106 pub struct ItemConst {
107 pub attrs: Vec<Attribute>,
108 pub vis: Visibility,
109 pub const_token: Token![const],
110 pub ident: Ident,
111 pub generics: Generics,
112 pub colon_token: Token![:],
113 pub ty: Box<Type>,
114 pub eq_token: Token![=],
115 pub expr: Box<Expr>,
116 pub semi_token: Token![;],
117 }
118}
119
120#[doc = r" An enum definition: `enum Foo<A, B> { A(A), B(B) }`."]
pub struct ItemEnum {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub enum_token: crate::token::Enum,
pub ident: Ident,
pub generics: Generics,
pub brace_token: token::Brace,
pub variants: Punctuated<Variant, crate::token::Comma>,
}ast_struct! {
121 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
123 pub struct ItemEnum {
124 pub attrs: Vec<Attribute>,
125 pub vis: Visibility,
126 pub enum_token: Token![enum],
127 pub ident: Ident,
128 pub generics: Generics,
129 pub brace_token: token::Brace,
130 pub variants: Punctuated<Variant, Token![,]>,
131 }
132}
133
134#[doc = r" An `extern crate` item: `extern crate serde`."]
pub struct ItemExternCrate {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub extern_token: crate::token::Extern,
pub crate_token: crate::token::Crate,
pub ident: Ident,
pub rename: Option<(crate::token::As, Ident)>,
pub semi_token: crate::token::Semi,
}ast_struct! {
135 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
137 pub struct ItemExternCrate {
138 pub attrs: Vec<Attribute>,
139 pub vis: Visibility,
140 pub extern_token: Token![extern],
141 pub crate_token: Token![crate],
142 pub ident: Ident,
143 pub rename: Option<(Token![as], Ident)>,
144 pub semi_token: Token![;],
145 }
146}
147
148#[doc =
r" A free-standing function: `fn process(n: usize) -> Result<()> { ... }`."]
pub struct ItemFn {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub sig: Signature,
pub block: Box<Block>,
}ast_struct! {
149 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
151 pub struct ItemFn {
152 pub attrs: Vec<Attribute>,
153 pub vis: Visibility,
154 pub sig: Signature,
155 pub block: Box<Block>,
156 }
157}
158
159#[doc = r#" A block of foreign items: `extern "C" { ... }`."#]
pub struct ItemForeignMod {
pub attrs: Vec<Attribute>,
pub unsafety: Option<crate::token::Unsafe>,
pub abi: Abi,
pub brace_token: token::Brace,
pub items: Vec<ForeignItem>,
}ast_struct! {
160 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
162 pub struct ItemForeignMod {
163 pub attrs: Vec<Attribute>,
164 pub unsafety: Option<Token![unsafe]>,
165 pub abi: Abi,
166 pub brace_token: token::Brace,
167 pub items: Vec<ForeignItem>,
168 }
169}
170
171#[doc = r" An impl block providing trait or associated items: `impl<A> Trait"]
#[doc = r" for Data<A> { ... }`."]
pub struct ItemImpl {
pub attrs: Vec<Attribute>,
pub defaultness: Option<crate::token::Default>,
pub unsafety: Option<crate::token::Unsafe>,
pub impl_token: crate::token::Impl,
pub generics: Generics,
#[doc = r" Trait this impl implements."]
pub trait_: Option<(Option<crate::token::Not>, Path, crate::token::For)>,
#[doc = r" The Self type of the impl."]
pub self_ty: Box<Type>,
pub brace_token: token::Brace,
pub items: Vec<ImplItem>,
}ast_struct! {
172 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
175 pub struct ItemImpl {
176 pub attrs: Vec<Attribute>,
177 pub defaultness: Option<Token![default]>,
178 pub unsafety: Option<Token![unsafe]>,
179 pub impl_token: Token![impl],
180 pub generics: Generics,
181 pub trait_: Option<(Option<Token![!]>, Path, Token![for])>,
183 pub self_ty: Box<Type>,
185 pub brace_token: token::Brace,
186 pub items: Vec<ImplItem>,
187 }
188}
189
190#[doc = r" A macro invocation, which includes `macro_rules!` definitions."]
pub struct ItemMacro {
pub attrs: Vec<Attribute>,
#[doc = r" The `example` in `macro_rules! example { ... }`."]
pub ident: Option<Ident>,
pub mac: Macro,
pub semi_token: Option<crate::token::Semi>,
}ast_struct! {
191 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
193 pub struct ItemMacro {
194 pub attrs: Vec<Attribute>,
195 pub ident: Option<Ident>,
197 pub mac: Macro,
198 pub semi_token: Option<Token![;]>,
199 }
200}
201
202#[doc = r" A module or module declaration: `mod m` or `mod m { ... }`."]
pub struct ItemMod {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub unsafety: Option<crate::token::Unsafe>,
pub mod_token: crate::token::Mod,
pub ident: Ident,
pub content: Option<(token::Brace, Vec<Item>)>,
pub semi: Option<crate::token::Semi>,
}ast_struct! {
203 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
205 pub struct ItemMod {
206 pub attrs: Vec<Attribute>,
207 pub vis: Visibility,
208 pub unsafety: Option<Token![unsafe]>,
209 pub mod_token: Token![mod],
210 pub ident: Ident,
211 pub content: Option<(token::Brace, Vec<Item>)>,
212 pub semi: Option<Token![;]>,
213 }
214}
215
216#[doc = r" A static item: `static BIKE: Shed = Shed(42)`."]
pub struct ItemStatic {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub static_token: crate::token::Static,
pub mutability: StaticMutability,
pub ident: Ident,
pub colon_token: crate::token::Colon,
pub ty: Box<Type>,
pub eq_token: crate::token::Eq,
pub expr: Box<Expr>,
pub semi_token: crate::token::Semi,
}ast_struct! {
217 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
219 pub struct ItemStatic {
220 pub attrs: Vec<Attribute>,
221 pub vis: Visibility,
222 pub static_token: Token![static],
223 pub mutability: StaticMutability,
224 pub ident: Ident,
225 pub colon_token: Token![:],
226 pub ty: Box<Type>,
227 pub eq_token: Token![=],
228 pub expr: Box<Expr>,
229 pub semi_token: Token![;],
230 }
231}
232
233#[doc = r" A struct definition: `struct Foo<A> { x: A }`."]
pub struct ItemStruct {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub struct_token: crate::token::Struct,
pub ident: Ident,
pub generics: Generics,
pub fields: Fields,
pub semi_token: Option<crate::token::Semi>,
}ast_struct! {
234 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
236 pub struct ItemStruct {
237 pub attrs: Vec<Attribute>,
238 pub vis: Visibility,
239 pub struct_token: Token![struct],
240 pub ident: Ident,
241 pub generics: Generics,
242 pub fields: Fields,
243 pub semi_token: Option<Token![;]>,
244 }
245}
246
247#[doc = r" A trait definition: `pub trait Iterator { ... }`."]
pub struct ItemTrait {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub unsafety: Option<crate::token::Unsafe>,
pub auto_token: Option<crate::token::Auto>,
pub restriction: Option<ImplRestriction>,
pub trait_token: crate::token::Trait,
pub ident: Ident,
pub generics: Generics,
pub colon_token: Option<crate::token::Colon>,
pub supertraits: Punctuated<TypeParamBound, crate::token::Plus>,
pub brace_token: token::Brace,
pub items: Vec<TraitItem>,
}ast_struct! {
248 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
250 pub struct ItemTrait {
251 pub attrs: Vec<Attribute>,
252 pub vis: Visibility,
253 pub unsafety: Option<Token![unsafe]>,
254 pub auto_token: Option<Token![auto]>,
255 pub restriction: Option<ImplRestriction>,
256 pub trait_token: Token![trait],
257 pub ident: Ident,
258 pub generics: Generics,
259 pub colon_token: Option<Token![:]>,
260 pub supertraits: Punctuated<TypeParamBound, Token![+]>,
261 pub brace_token: token::Brace,
262 pub items: Vec<TraitItem>,
263 }
264}
265
266#[doc = r" A trait alias: `pub trait SharableIterator = Iterator + Sync`."]
pub struct ItemTraitAlias {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub trait_token: crate::token::Trait,
pub ident: Ident,
pub generics: Generics,
pub eq_token: crate::token::Eq,
pub bounds: Punctuated<TypeParamBound, crate::token::Plus>,
pub semi_token: crate::token::Semi,
}ast_struct! {
267 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
269 pub struct ItemTraitAlias {
270 pub attrs: Vec<Attribute>,
271 pub vis: Visibility,
272 pub trait_token: Token![trait],
273 pub ident: Ident,
274 pub generics: Generics,
275 pub eq_token: Token![=],
276 pub bounds: Punctuated<TypeParamBound, Token![+]>,
277 pub semi_token: Token![;],
278 }
279}
280
281#[doc =
r" A type alias: `type Result<T> = core::result::Result<T, MyError>`."]
pub struct ItemType {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub type_token: crate::token::Type,
pub ident: Ident,
pub generics: Generics,
pub eq_token: crate::token::Eq,
pub ty: Box<Type>,
pub semi_token: crate::token::Semi,
}ast_struct! {
282 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
284 pub struct ItemType {
285 pub attrs: Vec<Attribute>,
286 pub vis: Visibility,
287 pub type_token: Token![type],
288 pub ident: Ident,
289 pub generics: Generics,
290 pub eq_token: Token![=],
291 pub ty: Box<Type>,
292 pub semi_token: Token![;],
293 }
294}
295
296#[doc = r" A union definition: `union Foo<A, B> { x: A, y: B }`."]
pub struct ItemUnion {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub union_token: crate::token::Union,
pub ident: Ident,
pub generics: Generics,
pub fields: FieldsNamed,
}ast_struct! {
297 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
299 pub struct ItemUnion {
300 pub attrs: Vec<Attribute>,
301 pub vis: Visibility,
302 pub union_token: Token![union],
303 pub ident: Ident,
304 pub generics: Generics,
305 pub fields: FieldsNamed,
306 }
307}
308
309#[doc = r" A use declaration: `use alloc::collections::HashMap`."]
pub struct ItemUse {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub use_token: crate::token::Use,
pub leading_colon: Option<crate::token::PathSep>,
pub tree: UseTree,
pub semi_token: crate::token::Semi,
}ast_struct! {
310 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
312 pub struct ItemUse {
313 pub attrs: Vec<Attribute>,
314 pub vis: Visibility,
315 pub use_token: Token![use],
316 pub leading_colon: Option<Token![::]>,
317 pub tree: UseTree,
318 pub semi_token: Token![;],
319 }
320}
321
322impl Item {
323 #[cfg(feature = "parsing")]
324 pub(crate) fn replace_attrs(&mut self, new: Vec<Attribute>) -> Vec<Attribute> {
325 match self {
326 Item::Const(ItemConst { attrs, .. })
327 | Item::Enum(ItemEnum { attrs, .. })
328 | Item::ExternCrate(ItemExternCrate { attrs, .. })
329 | Item::Fn(ItemFn { attrs, .. })
330 | Item::ForeignMod(ItemForeignMod { attrs, .. })
331 | Item::Impl(ItemImpl { attrs, .. })
332 | Item::Macro(ItemMacro { attrs, .. })
333 | Item::Mod(ItemMod { attrs, .. })
334 | Item::Static(ItemStatic { attrs, .. })
335 | Item::Struct(ItemStruct { attrs, .. })
336 | Item::Trait(ItemTrait { attrs, .. })
337 | Item::TraitAlias(ItemTraitAlias { attrs, .. })
338 | Item::Type(ItemType { attrs, .. })
339 | Item::Union(ItemUnion { attrs, .. })
340 | Item::Use(ItemUse { attrs, .. }) => mem::replace(attrs, new),
341 Item::Verbatim(_) => Vec::new(),
342 }
343 }
344}
345
346impl From<DeriveInput> for Item {
347 fn from(input: DeriveInput) -> Item {
348 match input.data {
349 Data::Struct(data) => Item::Struct(ItemStruct {
350 attrs: input.attrs,
351 vis: input.vis,
352 struct_token: data.struct_token,
353 ident: input.ident,
354 generics: input.generics,
355 fields: data.fields,
356 semi_token: data.semi_token,
357 }),
358 Data::Enum(data) => Item::Enum(ItemEnum {
359 attrs: input.attrs,
360 vis: input.vis,
361 enum_token: data.enum_token,
362 ident: input.ident,
363 generics: input.generics,
364 brace_token: data.brace_token,
365 variants: data.variants,
366 }),
367 Data::Union(data) => Item::Union(ItemUnion {
368 attrs: input.attrs,
369 vis: input.vis,
370 union_token: data.union_token,
371 ident: input.ident,
372 generics: input.generics,
373 fields: data.fields,
374 }),
375 }
376 }
377}
378
379impl From<ItemStruct> for DeriveInput {
380 fn from(input: ItemStruct) -> DeriveInput {
381 DeriveInput {
382 attrs: input.attrs,
383 vis: input.vis,
384 ident: input.ident,
385 generics: input.generics,
386 data: Data::Struct(DataStruct {
387 struct_token: input.struct_token,
388 fields: input.fields,
389 semi_token: input.semi_token,
390 }),
391 }
392 }
393}
394
395impl From<ItemEnum> for DeriveInput {
396 fn from(input: ItemEnum) -> DeriveInput {
397 DeriveInput {
398 attrs: input.attrs,
399 vis: input.vis,
400 ident: input.ident,
401 generics: input.generics,
402 data: Data::Enum(DataEnum {
403 enum_token: input.enum_token,
404 brace_token: input.brace_token,
405 variants: input.variants,
406 }),
407 }
408 }
409}
410
411impl From<ItemUnion> for DeriveInput {
412 fn from(input: ItemUnion) -> DeriveInput {
413 DeriveInput {
414 attrs: input.attrs,
415 vis: input.vis,
416 ident: input.ident,
417 generics: input.generics,
418 data: Data::Union(DataUnion {
419 union_token: input.union_token,
420 fields: input.fields,
421 }),
422 }
423 }
424}
425
426#[doc =
r" A suffix of an import tree in a `use` item: `Type as Renamed` or `*`."]
#[doc = r""]
#[doc = r" # Syntax tree enum"]
#[doc = r""]
#[doc = r" This type is a [syntax tree enum]."]
#[doc = r""]
#[doc = r" [syntax tree enum]: crate::expr::Expr#syntax-tree-enums"]
pub enum UseTree {
#[doc = r" A path prefix of imports in a `use` item: `core::...`."]
Path(UsePath),
#[doc = r" An identifier imported by a `use` item: `HashMap`."]
Name(UseName),
#[doc =
r" An renamed identifier imported by a `use` item: `HashMap as Map`."]
Rename(UseRename),
#[doc = r" A glob import in a `use` item: `*`."]
Glob(UseGlob),
#[doc = r" A braced group of imports in a `use` item: `{A, B, C}`."]
Group(UseGroup),
}
impl From<UsePath> for UseTree {
fn from(e: UsePath) -> UseTree { UseTree::Path(e) }
}
impl From<UseName> for UseTree {
fn from(e: UseName) -> UseTree { UseTree::Name(e) }
}
impl From<UseRename> for UseTree {
fn from(e: UseRename) -> UseTree { UseTree::Rename(e) }
}
impl From<UseGlob> for UseTree {
fn from(e: UseGlob) -> UseTree { UseTree::Glob(e) }
}
impl From<UseGroup> for UseTree {
fn from(e: UseGroup) -> UseTree { UseTree::Group(e) }
}
impl ::quote::ToTokens for UseTree {
fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
match self {
UseTree::Path(_e) => _e.to_tokens(tokens),
UseTree::Name(_e) => _e.to_tokens(tokens),
UseTree::Rename(_e) => _e.to_tokens(tokens),
UseTree::Glob(_e) => _e.to_tokens(tokens),
UseTree::Group(_e) => _e.to_tokens(tokens),
}
}
}ast_enum_of_structs! {
427 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
435 pub enum UseTree {
436 Path(UsePath),
438
439 Name(UseName),
441
442 Rename(UseRename),
444
445 Glob(UseGlob),
447
448 Group(UseGroup),
450 }
451}
452
453#[doc = r" A path prefix of imports in a `use` item: `core::...`."]
pub struct UsePath {
pub ident: Ident,
pub colon2_token: crate::token::PathSep,
pub tree: Box<UseTree>,
}ast_struct! {
454 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
456 pub struct UsePath {
457 pub ident: Ident,
458 pub colon2_token: Token![::],
459 pub tree: Box<UseTree>,
460 }
461}
462
463#[doc = r" An identifier imported by a `use` item: `HashMap`."]
pub struct UseName {
pub ident: Ident,
}ast_struct! {
464 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
466 pub struct UseName {
467 pub ident: Ident,
468 }
469}
470
471#[doc = r" An renamed identifier imported by a `use` item: `HashMap as Map`."]
pub struct UseRename {
pub ident: Ident,
pub as_token: crate::token::As,
pub rename: Ident,
}ast_struct! {
472 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
474 pub struct UseRename {
475 pub ident: Ident,
476 pub as_token: Token![as],
477 pub rename: Ident,
478 }
479}
480
481#[doc = r" A glob import in a `use` item: `*`."]
pub struct UseGlob {
pub star_token: crate::token::Star,
}ast_struct! {
482 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
484 pub struct UseGlob {
485 pub star_token: Token![*],
486 }
487}
488
489#[doc = r" A braced group of imports in a `use` item: `{A, B, C}`."]
pub struct UseGroup {
pub brace_token: token::Brace,
pub items: Punctuated<UseTree, crate::token::Comma>,
}ast_struct! {
490 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
492 pub struct UseGroup {
493 pub brace_token: token::Brace,
494 pub items: Punctuated<UseTree, Token![,]>,
495 }
496}
497
498#[doc = r" An item within an `extern` block."]
#[doc = r""]
#[doc = r" # Syntax tree enum"]
#[doc = r""]
#[doc = r" This type is a [syntax tree enum]."]
#[doc = r""]
#[doc = r" [syntax tree enum]: crate::expr::Expr#syntax-tree-enums"]
#[non_exhaustive]
pub enum ForeignItem {
#[doc = r" A foreign function in an `extern` block."]
Fn(ForeignItemFn),
#[doc = r" A foreign static item in an `extern` block: `static ext: u8`."]
Static(ForeignItemStatic),
#[doc = r" A foreign type in an `extern` block: `type void`."]
Type(ForeignItemType),
#[doc = r" A macro invocation within an extern block."]
Macro(ForeignItemMacro),
#[doc = r" Tokens in an `extern` block not interpreted by Syn."]
Verbatim(TokenStream),
}
impl From<ForeignItemFn> for ForeignItem {
fn from(e: ForeignItemFn) -> ForeignItem { ForeignItem::Fn(e) }
}
impl From<ForeignItemStatic> for ForeignItem {
fn from(e: ForeignItemStatic) -> ForeignItem { ForeignItem::Static(e) }
}
impl From<ForeignItemType> for ForeignItem {
fn from(e: ForeignItemType) -> ForeignItem { ForeignItem::Type(e) }
}
impl From<ForeignItemMacro> for ForeignItem {
fn from(e: ForeignItemMacro) -> ForeignItem { ForeignItem::Macro(e) }
}
impl ::quote::ToTokens for ForeignItem {
fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
match self {
ForeignItem::Fn(_e) => _e.to_tokens(tokens),
ForeignItem::Static(_e) => _e.to_tokens(tokens),
ForeignItem::Type(_e) => _e.to_tokens(tokens),
ForeignItem::Macro(_e) => _e.to_tokens(tokens),
ForeignItem::Verbatim(_e) => _e.to_tokens(tokens),
}
}
}ast_enum_of_structs! {
499 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
507 #[non_exhaustive]
508 pub enum ForeignItem {
509 Fn(ForeignItemFn),
511
512 Static(ForeignItemStatic),
514
515 Type(ForeignItemType),
517
518 Macro(ForeignItemMacro),
520
521 Verbatim(TokenStream),
523
524 }
542}
543
544#[doc = r" A foreign function in an `extern` block."]
pub struct ForeignItemFn {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub sig: Signature,
pub semi_token: crate::token::Semi,
}ast_struct! {
545 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
547 pub struct ForeignItemFn {
548 pub attrs: Vec<Attribute>,
549 pub vis: Visibility,
550 pub sig: Signature,
551 pub semi_token: Token![;],
552 }
553}
554
555#[doc = r" A foreign static item in an `extern` block: `static ext: u8`."]
pub struct ForeignItemStatic {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub static_token: crate::token::Static,
pub mutability: StaticMutability,
pub ident: Ident,
pub colon_token: crate::token::Colon,
pub ty: Box<Type>,
pub semi_token: crate::token::Semi,
}ast_struct! {
556 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
558 pub struct ForeignItemStatic {
559 pub attrs: Vec<Attribute>,
560 pub vis: Visibility,
561 pub static_token: Token![static],
562 pub mutability: StaticMutability,
563 pub ident: Ident,
564 pub colon_token: Token![:],
565 pub ty: Box<Type>,
566 pub semi_token: Token![;],
567 }
568}
569
570#[doc = r" A foreign type in an `extern` block: `type void`."]
pub struct ForeignItemType {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub type_token: crate::token::Type,
pub ident: Ident,
pub generics: Generics,
pub semi_token: crate::token::Semi,
}ast_struct! {
571 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
573 pub struct ForeignItemType {
574 pub attrs: Vec<Attribute>,
575 pub vis: Visibility,
576 pub type_token: Token![type],
577 pub ident: Ident,
578 pub generics: Generics,
579 pub semi_token: Token![;],
580 }
581}
582
583#[doc = r" A macro invocation within an extern block."]
pub struct ForeignItemMacro {
pub attrs: Vec<Attribute>,
pub mac: Macro,
pub semi_token: Option<crate::token::Semi>,
}ast_struct! {
584 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
586 pub struct ForeignItemMacro {
587 pub attrs: Vec<Attribute>,
588 pub mac: Macro,
589 pub semi_token: Option<Token![;]>,
590 }
591}
592
593#[doc = r" An item declaration within the definition of a trait."]
#[doc = r""]
#[doc = r" # Syntax tree enum"]
#[doc = r""]
#[doc = r" This type is a [syntax tree enum]."]
#[doc = r""]
#[doc = r" [syntax tree enum]: crate::expr::Expr#syntax-tree-enums"]
#[non_exhaustive]
pub enum TraitItem {
#[doc = r" An associated constant within the definition of a trait."]
Const(TraitItemConst),
#[doc = r" An associated function within the definition of a trait."]
Fn(TraitItemFn),
#[doc = r" An associated type within the definition of a trait."]
Type(TraitItemType),
#[doc = r" A macro invocation within the definition of a trait."]
Macro(TraitItemMacro),
#[doc =
r" Tokens within the definition of a trait not interpreted by Syn."]
Verbatim(TokenStream),
}
impl From<TraitItemConst> for TraitItem {
fn from(e: TraitItemConst) -> TraitItem { TraitItem::Const(e) }
}
impl From<TraitItemFn> for TraitItem {
fn from(e: TraitItemFn) -> TraitItem { TraitItem::Fn(e) }
}
impl From<TraitItemType> for TraitItem {
fn from(e: TraitItemType) -> TraitItem { TraitItem::Type(e) }
}
impl From<TraitItemMacro> for TraitItem {
fn from(e: TraitItemMacro) -> TraitItem { TraitItem::Macro(e) }
}
impl ::quote::ToTokens for TraitItem {
fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
match self {
TraitItem::Const(_e) => _e.to_tokens(tokens),
TraitItem::Fn(_e) => _e.to_tokens(tokens),
TraitItem::Type(_e) => _e.to_tokens(tokens),
TraitItem::Macro(_e) => _e.to_tokens(tokens),
TraitItem::Verbatim(_e) => _e.to_tokens(tokens),
}
}
}ast_enum_of_structs! {
594 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
602 #[non_exhaustive]
603 pub enum TraitItem {
604 Const(TraitItemConst),
606
607 Fn(TraitItemFn),
609
610 Type(TraitItemType),
612
613 Macro(TraitItemMacro),
615
616 Verbatim(TokenStream),
618
619 }
637}
638
639#[doc = r" An associated constant within the definition of a trait."]
pub struct TraitItemConst {
pub attrs: Vec<Attribute>,
pub const_token: crate::token::Const,
pub ident: Ident,
pub generics: Generics,
pub colon_token: crate::token::Colon,
pub ty: Type,
pub default: Option<(crate::token::Eq, Expr)>,
pub semi_token: crate::token::Semi,
}ast_struct! {
640 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
642 pub struct TraitItemConst {
643 pub attrs: Vec<Attribute>,
644 pub const_token: Token![const],
645 pub ident: Ident,
646 pub generics: Generics,
647 pub colon_token: Token![:],
648 pub ty: Type,
649 pub default: Option<(Token![=], Expr)>,
650 pub semi_token: Token![;],
651 }
652}
653
654#[doc = r" An associated function within the definition of a trait."]
pub struct TraitItemFn {
pub attrs: Vec<Attribute>,
pub sig: Signature,
pub default: Option<Block>,
pub semi_token: Option<crate::token::Semi>,
}ast_struct! {
655 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
657 pub struct TraitItemFn {
658 pub attrs: Vec<Attribute>,
659 pub sig: Signature,
660 pub default: Option<Block>,
661 pub semi_token: Option<Token![;]>,
662 }
663}
664
665#[doc = r" An associated type within the definition of a trait."]
pub struct TraitItemType {
pub attrs: Vec<Attribute>,
pub type_token: crate::token::Type,
pub ident: Ident,
pub generics: Generics,
pub colon_token: Option<crate::token::Colon>,
pub bounds: Punctuated<TypeParamBound, crate::token::Plus>,
pub default: Option<(crate::token::Eq, Type)>,
pub semi_token: crate::token::Semi,
}ast_struct! {
666 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
668 pub struct TraitItemType {
669 pub attrs: Vec<Attribute>,
670 pub type_token: Token![type],
671 pub ident: Ident,
672 pub generics: Generics,
673 pub colon_token: Option<Token![:]>,
674 pub bounds: Punctuated<TypeParamBound, Token![+]>,
675 pub default: Option<(Token![=], Type)>,
676 pub semi_token: Token![;],
677 }
678}
679
680#[doc = r" A macro invocation within the definition of a trait."]
pub struct TraitItemMacro {
pub attrs: Vec<Attribute>,
pub mac: Macro,
pub semi_token: Option<crate::token::Semi>,
}ast_struct! {
681 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
683 pub struct TraitItemMacro {
684 pub attrs: Vec<Attribute>,
685 pub mac: Macro,
686 pub semi_token: Option<Token![;]>,
687 }
688}
689
690#[doc = r" An item within an impl block."]
#[doc = r""]
#[doc = r" # Syntax tree enum"]
#[doc = r""]
#[doc = r" This type is a [syntax tree enum]."]
#[doc = r""]
#[doc = r" [syntax tree enum]: crate::expr::Expr#syntax-tree-enums"]
#[non_exhaustive]
pub enum ImplItem {
#[doc = r" An associated constant within an impl block."]
Const(ImplItemConst),
#[doc = r" An associated function within an impl block."]
Fn(ImplItemFn),
#[doc = r" An associated type within an impl block."]
Type(ImplItemType),
#[doc = r" A macro invocation within an impl block."]
Macro(ImplItemMacro),
#[doc = r" Tokens within an impl block not interpreted by Syn."]
Verbatim(TokenStream),
}
impl From<ImplItemConst> for ImplItem {
fn from(e: ImplItemConst) -> ImplItem { ImplItem::Const(e) }
}
impl From<ImplItemFn> for ImplItem {
fn from(e: ImplItemFn) -> ImplItem { ImplItem::Fn(e) }
}
impl From<ImplItemType> for ImplItem {
fn from(e: ImplItemType) -> ImplItem { ImplItem::Type(e) }
}
impl From<ImplItemMacro> for ImplItem {
fn from(e: ImplItemMacro) -> ImplItem { ImplItem::Macro(e) }
}
impl ::quote::ToTokens for ImplItem {
fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
match self {
ImplItem::Const(_e) => _e.to_tokens(tokens),
ImplItem::Fn(_e) => _e.to_tokens(tokens),
ImplItem::Type(_e) => _e.to_tokens(tokens),
ImplItem::Macro(_e) => _e.to_tokens(tokens),
ImplItem::Verbatim(_e) => _e.to_tokens(tokens),
}
}
}ast_enum_of_structs! {
691 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
699 #[non_exhaustive]
700 pub enum ImplItem {
701 Const(ImplItemConst),
703
704 Fn(ImplItemFn),
706
707 Type(ImplItemType),
709
710 Macro(ImplItemMacro),
712
713 Verbatim(TokenStream),
715
716 }
734}
735
736#[doc = r" An associated constant within an impl block."]
pub struct ImplItemConst {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub defaultness: Option<crate::token::Default>,
pub const_token: crate::token::Const,
pub ident: Ident,
pub generics: Generics,
pub colon_token: crate::token::Colon,
pub ty: Type,
pub eq_token: crate::token::Eq,
pub expr: Expr,
pub semi_token: crate::token::Semi,
}ast_struct! {
737 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
739 pub struct ImplItemConst {
740 pub attrs: Vec<Attribute>,
741 pub vis: Visibility,
742 pub defaultness: Option<Token![default]>,
743 pub const_token: Token![const],
744 pub ident: Ident,
745 pub generics: Generics,
746 pub colon_token: Token![:],
747 pub ty: Type,
748 pub eq_token: Token![=],
749 pub expr: Expr,
750 pub semi_token: Token![;],
751 }
752}
753
754#[doc = r" An associated function within an impl block."]
pub struct ImplItemFn {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub defaultness: Option<crate::token::Default>,
pub sig: Signature,
pub block: Block,
}ast_struct! {
755 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
757 pub struct ImplItemFn {
758 pub attrs: Vec<Attribute>,
759 pub vis: Visibility,
760 pub defaultness: Option<Token![default]>,
761 pub sig: Signature,
762 pub block: Block,
763 }
764}
765
766#[doc = r" An associated type within an impl block."]
pub struct ImplItemType {
pub attrs: Vec<Attribute>,
pub vis: Visibility,
pub defaultness: Option<crate::token::Default>,
pub type_token: crate::token::Type,
pub ident: Ident,
pub generics: Generics,
pub eq_token: crate::token::Eq,
pub ty: Type,
pub semi_token: crate::token::Semi,
}ast_struct! {
767 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
769 pub struct ImplItemType {
770 pub attrs: Vec<Attribute>,
771 pub vis: Visibility,
772 pub defaultness: Option<Token![default]>,
773 pub type_token: Token![type],
774 pub ident: Ident,
775 pub generics: Generics,
776 pub eq_token: Token![=],
777 pub ty: Type,
778 pub semi_token: Token![;],
779 }
780}
781
782#[doc = r" A macro invocation within an impl block."]
pub struct ImplItemMacro {
pub attrs: Vec<Attribute>,
pub mac: Macro,
pub semi_token: Option<crate::token::Semi>,
}ast_struct! {
783 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
785 pub struct ImplItemMacro {
786 pub attrs: Vec<Attribute>,
787 pub mac: Macro,
788 pub semi_token: Option<Token![;]>,
789 }
790}
791
792#[doc = r" A function signature in a trait or implementation: `unsafe fn"]
#[doc = r" initialize(&self)`."]
pub struct Signature {
pub constness: Option<crate::token::Const>,
pub asyncness: Option<crate::token::Async>,
pub unsafety: Option<crate::token::Unsafe>,
pub abi: Option<Abi>,
pub fn_token: crate::token::Fn,
pub ident: Ident,
pub generics: Generics,
pub paren_token: token::Paren,
pub inputs: Punctuated<FnArg, crate::token::Comma>,
pub variadic: Option<Variadic>,
pub output: ReturnType,
}ast_struct! {
793 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
796 pub struct Signature {
797 pub constness: Option<Token![const]>,
798 pub asyncness: Option<Token![async]>,
799 pub unsafety: Option<Token![unsafe]>,
800 pub abi: Option<Abi>,
801 pub fn_token: Token![fn],
802 pub ident: Ident,
803 pub generics: Generics,
804 pub paren_token: token::Paren,
805 pub inputs: Punctuated<FnArg, Token![,]>,
806 pub variadic: Option<Variadic>,
807 pub output: ReturnType,
808 }
809}
810
811impl Signature {
812 pub fn receiver(&self) -> Option<&Receiver> {
814 let arg = self.inputs.first()?;
815 match arg {
816 FnArg::Receiver(receiver) => Some(receiver),
817 FnArg::Typed(_) => None,
818 }
819 }
820}
821
822#[doc =
r" An argument in a function signature: the `n: usize` in `fn f(n: usize)`."]
pub enum FnArg {
#[doc = r" The `self` argument of an associated method."]
Receiver(Receiver),
#[doc = r" A function argument accepted by pattern and type."]
Typed(PatType),
}
impl From<Receiver> for FnArg {
fn from(e: Receiver) -> FnArg { FnArg::Receiver(e) }
}
impl From<PatType> for FnArg {
fn from(e: PatType) -> FnArg { FnArg::Typed(e) }
}
impl ::quote::ToTokens for FnArg {
fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) {
match self {
FnArg::Receiver(_e) => _e.to_tokens(tokens),
FnArg::Typed(_e) => _e.to_tokens(tokens),
}
}
}ast_enum_of_structs! {
823 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
825 pub enum FnArg {
826 Receiver(Receiver),
828
829 Typed(PatType),
831 }
832}
833
834#[doc = r" The `self` argument of an associated method."]
#[doc = r""]
#[doc =
r" If `colon_token` is present, the receiver is written with an explicit"]
#[doc =
r" type such as `self: Box<Self>`. If `colon_token` is absent, the receiver"]
#[doc =
r" is written in shorthand such as `self` or `&self` or `&mut self`. In the"]
#[doc =
r" shorthand case, the type in `ty` is reconstructed as one of `Self`,"]
#[doc = r" `&Self`, or `&mut Self`."]
pub struct Receiver {
pub attrs: Vec<Attribute>,
pub reference: Option<(crate::token::And, Option<Lifetime>)>,
pub mutability: Option<crate::token::Mut>,
pub self_token: crate::token::SelfValue,
pub colon_token: Option<crate::token::Colon>,
pub ty: Box<Type>,
}ast_struct! {
835 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
843 pub struct Receiver {
844 pub attrs: Vec<Attribute>,
845 pub reference: Option<(Token![&], Option<Lifetime>)>,
846 pub mutability: Option<Token![mut]>,
847 pub self_token: Token![self],
848 pub colon_token: Option<Token![:]>,
849 pub ty: Box<Type>,
850 }
851}
852
853impl Receiver {
854 pub fn lifetime(&self) -> Option<&Lifetime> {
855 self.reference.as_ref()?.1.as_ref()
856 }
857}
858
859#[doc = r" The variadic argument of a foreign function."]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" # struct c_char;"]
#[doc = r" # struct c_int;"]
#[doc = r" #"]
#[doc = r#" extern "C" {"#]
#[doc = r" fn printf(format: *const c_char, ...) -> c_int;"]
#[doc = r" // ^^^"]
#[doc = r" }"]
#[doc = r" ```"]
pub struct Variadic {
pub attrs: Vec<Attribute>,
pub pat: Option<(Box<Pat>, crate::token::Colon)>,
pub dots: crate::token::DotDotDot,
pub comma: Option<crate::token::Comma>,
}ast_struct! {
860 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
872 pub struct Variadic {
873 pub attrs: Vec<Attribute>,
874 pub pat: Option<(Box<Pat>, Token![:])>,
875 pub dots: Token![...],
876 pub comma: Option<Token![,]>,
877 }
878}
879
880#[doc = r" The mutability of an `Item::Static` or `ForeignItem::Static`."]
#[non_exhaustive]
pub enum StaticMutability { Mut(crate::token::Mut), None, }ast_enum! {
881 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
883 #[non_exhaustive]
884 pub enum StaticMutability {
885 Mut(Token![mut]),
886 None,
887 }
888}
889
890#[doc = r" Unused, but reserved for RFC 3323 restrictions."]
#[non_exhaustive]
pub enum ImplRestriction {}ast_enum! {
891 #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
893 #[non_exhaustive]
894 pub enum ImplRestriction {}
895
896
897 }
906
907#[cfg(feature = "parsing")]
908pub(crate) mod parsing {
909 use crate::attr::{self, Attribute};
910 use crate::derive;
911 use crate::error::{Error, Result};
912 use crate::expr::Expr;
913 use crate::ext::IdentExt as _;
914 use crate::generics::{self, Generics, TypeParamBound};
915 use crate::ident::Ident;
916 use crate::item::{
917 FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType,
918 ImplItem, ImplItemConst, ImplItemFn, ImplItemMacro, ImplItemType, Item, ItemConst,
919 ItemEnum, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod,
920 ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Receiver,
921 Signature, StaticMutability, TraitItem, TraitItemConst, TraitItemFn, TraitItemMacro,
922 TraitItemType, UseGlob, UseGroup, UseName, UsePath, UseRename, UseTree, Variadic,
923 };
924 use crate::lifetime::Lifetime;
925 use crate::lit::LitStr;
926 use crate::mac::{self, Macro};
927 use crate::parse::discouraged::Speculative as _;
928 use crate::parse::{Parse, ParseBuffer, ParseStream};
929 use crate::pat::{Pat, PatType, PatWild};
930 use crate::path::Path;
931 use crate::punctuated::Punctuated;
932 use crate::restriction::Visibility;
933 use crate::stmt::Block;
934 use crate::token;
935 use crate::ty::{Abi, ReturnType, Type, TypePath, TypeReference};
936 use crate::verbatim;
937 use alloc::boxed::Box;
938 use alloc::vec::Vec;
939 use proc_macro2::TokenStream;
940
941 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
942 impl Parse for Item {
943 fn parse(input: ParseStream) -> Result<Self> {
944 let begin = input.fork();
945 let attrs = input.call(Attribute::parse_outer)?;
946 parse_rest_of_item(begin, attrs, input)
947 }
948 }
949
950 pub(crate) fn parse_rest_of_item(
951 begin: ParseBuffer,
952 mut attrs: Vec<Attribute>,
953 input: ParseStream,
954 ) -> Result<Item> {
955 let ahead = input.fork();
956 let vis: Visibility = ahead.parse()?;
957
958 let lookahead = ahead.lookahead1();
959 let allow_safe = false;
960 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
961 let vis: Visibility = input.parse()?;
962 let sig: Signature = input.parse()?;
963 if input.peek(crate::token::SemiToken![;]) {
964 input.parse::<crate::token::SemiToken![;]>()?;
965 Ok(Item::Verbatim(verbatim::between(&begin, input)))
966 } else {
967 parse_rest_of_fn(input, Vec::new(), vis, sig).map(Item::Fn)
968 }
969 } else if lookahead.peek(crate::token::ExternToken![extern]) {
970 ahead.parse::<crate::token::ExternToken![extern]>()?;
971 let lookahead = ahead.lookahead1();
972 if lookahead.peek(crate::token::CrateToken![crate]) {
973 input.parse().map(Item::ExternCrate)
974 } else if lookahead.peek(token::Brace) {
975 input.parse().map(Item::ForeignMod)
976 } else if lookahead.peek(LitStr) {
977 ahead.parse::<LitStr>()?;
978 let lookahead = ahead.lookahead1();
979 if lookahead.peek(token::Brace) {
980 input.parse().map(Item::ForeignMod)
981 } else {
982 Err(lookahead.error())
983 }
984 } else {
985 Err(lookahead.error())
986 }
987 } else if lookahead.peek(crate::token::UseToken![use]) {
988 let allow_crate_root_in_path = true;
989 match parse_item_use(input, allow_crate_root_in_path)? {
990 Some(item_use) => Ok(Item::Use(item_use)),
991 None => Ok(Item::Verbatim(verbatim::between(&begin, input))),
992 }
993 } else if lookahead.peek(crate::token::StaticToken![static]) {
994 let vis = input.parse()?;
995 let static_token = input.parse()?;
996 let mutability = input.parse()?;
997 let ident = input.parse()?;
998 if input.peek(crate::token::EqToken![=]) {
999 input.parse::<crate::token::EqToken![=]>()?;
1000 input.parse::<Expr>()?;
1001 input.parse::<crate::token::SemiToken![;]>()?;
1002 Ok(Item::Verbatim(verbatim::between(&begin, input)))
1003 } else {
1004 let colon_token = input.parse()?;
1005 let ty = input.parse()?;
1006 if input.peek(crate::token::SemiToken![;]) {
1007 input.parse::<crate::token::SemiToken![;]>()?;
1008 Ok(Item::Verbatim(verbatim::between(&begin, input)))
1009 } else {
1010 Ok(Item::Static(ItemStatic {
1011 attrs: Vec::new(),
1012 vis,
1013 static_token,
1014 mutability,
1015 ident,
1016 colon_token,
1017 ty,
1018 eq_token: input.parse()?,
1019 expr: input.parse()?,
1020 semi_token: input.parse()?,
1021 }))
1022 }
1023 }
1024 } else if lookahead.peek(crate::token::ConstToken![const]) {
1025 let vis = input.parse()?;
1026 let const_token: crate::token::ConstToken![const] = input.parse()?;
1027 let lookahead = input.lookahead1();
1028 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
1029 input.call(Ident::parse_any)?
1030 } else {
1031 return Err(lookahead.error());
1032 };
1033 let mut generics: Generics = input.parse()?;
1034 let colon_token = input.parse()?;
1035 let ty = input.parse()?;
1036 let value = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
1037 let expr: Expr = input.parse()?;
1038 Some((eq_token, expr))
1039 } else {
1040 None
1041 };
1042 generics.where_clause = input.parse()?;
1043 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1044 match value {
1045 Some((eq_token, expr))
1046 if generics.lt_token.is_none() && generics.where_clause.is_none() =>
1047 {
1048 Ok(Item::Const(ItemConst {
1049 attrs: Vec::new(),
1050 vis,
1051 const_token,
1052 ident,
1053 generics,
1054 colon_token,
1055 ty,
1056 eq_token,
1057 expr: Box::new(expr),
1058 semi_token,
1059 }))
1060 }
1061 _ => Ok(Item::Verbatim(verbatim::between(&begin, input))),
1062 }
1063 } else if lookahead.peek(crate::token::UnsafeToken![unsafe]) {
1064 ahead.parse::<crate::token::UnsafeToken![unsafe]>()?;
1065 let lookahead = ahead.lookahead1();
1066 if lookahead.peek(crate::token::TraitToken![trait])
1067 || lookahead.peek(crate::token::AutoToken![auto]) && ahead.peek2(crate::token::TraitToken![trait])
1068 {
1069 input.parse().map(Item::Trait)
1070 } else if lookahead.peek(crate::token::ImplToken![impl]) {
1071 let allow_verbatim_impl = true;
1072 if let Some(item) = parse_impl(input, allow_verbatim_impl)? {
1073 Ok(Item::Impl(item))
1074 } else {
1075 Ok(Item::Verbatim(verbatim::between(&begin, input)))
1076 }
1077 } else if lookahead.peek(crate::token::ExternToken![extern]) {
1078 input.parse().map(Item::ForeignMod)
1079 } else if lookahead.peek(crate::token::ModToken![mod]) {
1080 input.parse().map(Item::Mod)
1081 } else {
1082 Err(lookahead.error())
1083 }
1084 } else if lookahead.peek(crate::token::ModToken![mod]) {
1085 input.parse().map(Item::Mod)
1086 } else if lookahead.peek(crate::token::TypeToken![type]) {
1087 parse_item_type(begin, input)
1088 } else if lookahead.peek(crate::token::StructToken![struct]) {
1089 input.parse().map(Item::Struct)
1090 } else if lookahead.peek(crate::token::EnumToken![enum]) {
1091 input.parse().map(Item::Enum)
1092 } else if lookahead.peek(crate::token::UnionToken![union]) && ahead.peek2(Ident) {
1093 input.parse().map(Item::Union)
1094 } else if lookahead.peek(crate::token::TraitToken![trait]) {
1095 input.call(parse_trait_or_trait_alias)
1096 } else if lookahead.peek(crate::token::AutoToken![auto]) && ahead.peek2(crate::token::TraitToken![trait]) {
1097 input.parse().map(Item::Trait)
1098 } else if lookahead.peek(crate::token::ImplToken![impl])
1099 || lookahead.peek(crate::token::DefaultToken![default]) && !ahead.peek2(crate::token::NotToken![!])
1100 {
1101 let allow_verbatim_impl = true;
1102 if let Some(item) = parse_impl(input, allow_verbatim_impl)? {
1103 Ok(Item::Impl(item))
1104 } else {
1105 Ok(Item::Verbatim(verbatim::between(&begin, input)))
1106 }
1107 } else if lookahead.peek(crate::token::MacroToken![macro]) {
1108 input.advance_to(&ahead);
1109 parse_macro2(begin, vis, input)
1110 } else if vis.is_inherited()
1111 && (lookahead.peek(Ident)
1112 || lookahead.peek(crate::token::SelfValueToken![self])
1113 || lookahead.peek(crate::token::SuperToken![super])
1114 || lookahead.peek(crate::token::CrateToken![crate])
1115 || lookahead.peek(crate::token::PathSepToken![::]))
1116 {
1117 input.parse().map(Item::Macro)
1118 } else {
1119 Err(lookahead.error())
1120 }?;
1121
1122 attrs.extend(item.replace_attrs(Vec::new()));
1123 item.replace_attrs(attrs);
1124 Ok(item)
1125 }
1126
1127 struct FlexibleItemType {
1128 vis: Visibility,
1129 defaultness: Option<crate::token::DefaultToken![default]>,
1130 type_token: crate::token::TypeToken![type],
1131 ident: Ident,
1132 generics: Generics,
1133 colon_token: Option<crate::token::ColonToken![:]>,
1134 bounds: Punctuated<TypeParamBound, crate::token::PlusToken![+]>,
1135 ty: Option<(crate::token::EqToken![=], Type)>,
1136 semi_token: crate::token::SemiToken![;],
1137 }
1138
1139 enum TypeDefaultness {
1140 Optional,
1141 Disallowed,
1142 }
1143
1144 enum WhereClauseLocation {
1145 BeforeEq,
1147 AfterEq,
1149 Both,
1151 }
1152
1153 impl FlexibleItemType {
1154 fn parse(
1155 input: ParseStream,
1156 allow_defaultness: TypeDefaultness,
1157 where_clause_location: WhereClauseLocation,
1158 ) -> Result<Self> {
1159 let vis: Visibility = input.parse()?;
1160 let defaultness: Option<crate::token::DefaultToken![default]> = match allow_defaultness {
1161 TypeDefaultness::Optional => input.parse()?,
1162 TypeDefaultness::Disallowed => None,
1163 };
1164 let type_token: crate::token::TypeToken![type] = input.parse()?;
1165 let ident: Ident = input.parse()?;
1166 let mut generics: Generics = input.parse()?;
1167 let (colon_token, bounds) = Self::parse_optional_bounds(input)?;
1168
1169 match where_clause_location {
1170 WhereClauseLocation::BeforeEq | WhereClauseLocation::Both => {
1171 generics.where_clause = input.parse()?;
1172 }
1173 WhereClauseLocation::AfterEq => {}
1174 }
1175
1176 let ty = Self::parse_optional_definition(input)?;
1177
1178 match where_clause_location {
1179 WhereClauseLocation::AfterEq | WhereClauseLocation::Both
1180 if generics.where_clause.is_none() =>
1181 {
1182 generics.where_clause = input.parse()?;
1183 }
1184 _ => {}
1185 }
1186
1187 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1188
1189 Ok(FlexibleItemType {
1190 vis,
1191 defaultness,
1192 type_token,
1193 ident,
1194 generics,
1195 colon_token,
1196 bounds,
1197 ty,
1198 semi_token,
1199 })
1200 }
1201
1202 fn parse_optional_bounds(
1203 input: ParseStream,
1204 ) -> Result<(Option<crate::token::ColonToken![:]>, Punctuated<TypeParamBound, crate::token::PlusToken![+]>)> {
1205 let colon_token: Option<crate::token::ColonToken![:]> = input.parse()?;
1206
1207 let mut bounds = Punctuated::new();
1208 if colon_token.is_some() {
1209 loop {
1210 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::EqToken![=]) || input.peek(crate::token::SemiToken![;]) {
1211 break;
1212 }
1213 bounds.push_value({
1214 let allow_precise_capture = false;
1215 let allow_const = true;
1216 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
1217 });
1218 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::EqToken![=]) || input.peek(crate::token::SemiToken![;]) {
1219 break;
1220 }
1221 bounds.push_punct(input.parse::<crate::token::PlusToken![+]>()?);
1222 }
1223 }
1224
1225 Ok((colon_token, bounds))
1226 }
1227
1228 fn parse_optional_definition(input: ParseStream) -> Result<Option<(crate::token::EqToken![=], Type)>> {
1229 let eq_token: Option<crate::token::EqToken![=]> = input.parse()?;
1230 if let Some(eq_token) = eq_token {
1231 let definition: Type = input.parse()?;
1232 Ok(Some((eq_token, definition)))
1233 } else {
1234 Ok(None)
1235 }
1236 }
1237 }
1238
1239 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1240 impl Parse for ItemMacro {
1241 fn parse(input: ParseStream) -> Result<Self> {
1242 let attrs = input.call(Attribute::parse_outer)?;
1243 let path = input.call(Path::parse_mod_style)?;
1244 let bang_token: crate::token::NotToken![!] = input.parse()?;
1245 let ident: Option<Ident> = if input.peek(crate::token::TryToken![try]) {
1246 input.call(Ident::parse_any).map(Some)
1247 } else {
1248 input.parse()
1249 }?;
1250 let (delimiter, tokens) = input.call(mac::parse_delimiter)?;
1251 let semi_token: Option<crate::token::SemiToken![;]> = if !delimiter.is_brace() {
1252 Some(input.parse()?)
1253 } else {
1254 None
1255 };
1256 Ok(ItemMacro {
1257 attrs,
1258 ident,
1259 mac: Macro {
1260 path,
1261 bang_token,
1262 delimiter,
1263 tokens,
1264 },
1265 semi_token,
1266 })
1267 }
1268 }
1269
1270 fn parse_macro2(begin: ParseBuffer, _vis: Visibility, input: ParseStream) -> Result<Item> {
1271 input.parse::<crate::token::MacroToken![macro]>()?;
1272 input.parse::<Ident>()?;
1273
1274 let mut lookahead = input.lookahead1();
1275 if lookahead.peek(token::Paren) {
1276 let paren_content;
1277 match crate::__private::parse_parens(&input) {
crate::__private::Ok(parens) => {
paren_content = parens.content;
_ = paren_content;
parens.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
};parenthesized!(paren_content in input);
1278 paren_content.parse::<TokenStream>()?;
1279 lookahead = input.lookahead1();
1280 }
1281
1282 if lookahead.peek(token::Brace) {
1283 let brace_content;
1284 match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
brace_content = braces.content;
_ = brace_content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
};braced!(brace_content in input);
1285 brace_content.parse::<TokenStream>()?;
1286 } else {
1287 return Err(lookahead.error());
1288 }
1289
1290 Ok(Item::Verbatim(verbatim::between(&begin, input)))
1291 }
1292
1293 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1294 impl Parse for ItemExternCrate {
1295 fn parse(input: ParseStream) -> Result<Self> {
1296 Ok(ItemExternCrate {
1297 attrs: input.call(Attribute::parse_outer)?,
1298 vis: input.parse()?,
1299 extern_token: input.parse()?,
1300 crate_token: input.parse()?,
1301 ident: {
1302 if input.peek(crate::token::SelfValueToken![self]) {
1303 input.call(Ident::parse_any)?
1304 } else {
1305 input.parse()?
1306 }
1307 },
1308 rename: {
1309 if input.peek(crate::token::AsToken![as]) {
1310 let as_token: crate::token::AsToken![as] = input.parse()?;
1311 let rename: Ident = if input.peek(crate::token::UnderscoreToken![_]) {
1312 Ident::from(input.parse::<crate::token::UnderscoreToken![_]>()?)
1313 } else {
1314 input.parse()?
1315 };
1316 Some((as_token, rename))
1317 } else {
1318 None
1319 }
1320 },
1321 semi_token: input.parse()?,
1322 })
1323 }
1324 }
1325
1326 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1327 impl Parse for ItemUse {
1328 fn parse(input: ParseStream) -> Result<Self> {
1329 let allow_crate_root_in_path = false;
1330 parse_item_use(input, allow_crate_root_in_path).map(Option::unwrap)
1331 }
1332 }
1333
1334 fn parse_item_use(
1335 input: ParseStream,
1336 allow_crate_root_in_path: bool,
1337 ) -> Result<Option<ItemUse>> {
1338 let attrs = input.call(Attribute::parse_outer)?;
1339 let vis: Visibility = input.parse()?;
1340 let use_token: crate::token::UseToken![use] = input.parse()?;
1341 let leading_colon: Option<crate::token::PathSepToken![::]> = input.parse()?;
1342 let tree = parse_use_tree(input, allow_crate_root_in_path && leading_colon.is_none())?;
1343 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1344
1345 let tree = match tree {
1346 Some(tree) => tree,
1347 None => return Ok(None),
1348 };
1349
1350 Ok(Some(ItemUse {
1351 attrs,
1352 vis,
1353 use_token,
1354 leading_colon,
1355 tree,
1356 semi_token,
1357 }))
1358 }
1359
1360 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1361 impl Parse for UseTree {
1362 fn parse(input: ParseStream) -> Result<UseTree> {
1363 let allow_crate_root_in_path = false;
1364 parse_use_tree(input, allow_crate_root_in_path).map(Option::unwrap)
1365 }
1366 }
1367
1368 fn parse_use_tree(
1369 input: ParseStream,
1370 allow_crate_root_in_path: bool,
1371 ) -> Result<Option<UseTree>> {
1372 let lookahead = input.lookahead1();
1373 if lookahead.peek(Ident)
1374 || lookahead.peek(crate::token::SelfValueToken![self])
1375 || lookahead.peek(crate::token::SuperToken![super])
1376 || lookahead.peek(crate::token::CrateToken![crate])
1377 || lookahead.peek(crate::token::TryToken![try])
1378 {
1379 let ident = input.call(Ident::parse_any)?;
1380 if input.peek(crate::token::PathSepToken![::]) {
1381 Ok(Some(UseTree::Path(UsePath {
1382 ident,
1383 colon2_token: input.parse()?,
1384 tree: Box::new(input.parse()?),
1385 })))
1386 } else if input.peek(crate::token::AsToken![as]) {
1387 Ok(Some(UseTree::Rename(UseRename {
1388 ident,
1389 as_token: input.parse()?,
1390 rename: {
1391 if input.peek(Ident) {
1392 input.parse()?
1393 } else if input.peek(crate::token::UnderscoreToken![_]) {
1394 Ident::from(input.parse::<crate::token::UnderscoreToken![_]>()?)
1395 } else {
1396 return Err(input.error("expected identifier or underscore"));
1397 }
1398 },
1399 })))
1400 } else {
1401 Ok(Some(UseTree::Name(UseName { ident })))
1402 }
1403 } else if lookahead.peek(crate::token::StarToken![*]) {
1404 Ok(Some(UseTree::Glob(UseGlob {
1405 star_token: input.parse()?,
1406 })))
1407 } else if lookahead.peek(token::Brace) {
1408 let content;
1409 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
1410 let mut items = Punctuated::new();
1411 let mut has_any_crate_root_in_path = false;
1412 loop {
1413 if content.is_empty() {
1414 break;
1415 }
1416 let this_tree_starts_with_crate_root =
1417 allow_crate_root_in_path && content.parse::<Option<crate::token::PathSepToken![::]>>()?.is_some();
1418 has_any_crate_root_in_path |= this_tree_starts_with_crate_root;
1419 match parse_use_tree(
1420 &content,
1421 allow_crate_root_in_path && !this_tree_starts_with_crate_root,
1422 )? {
1423 Some(tree) if !has_any_crate_root_in_path => items.push_value(tree),
1424 _ => has_any_crate_root_in_path = true,
1425 }
1426 if content.is_empty() {
1427 break;
1428 }
1429 let comma: crate::token::CommaToken![,] = content.parse()?;
1430 if !has_any_crate_root_in_path {
1431 items.push_punct(comma);
1432 }
1433 }
1434 if has_any_crate_root_in_path {
1435 Ok(None)
1436 } else {
1437 Ok(Some(UseTree::Group(UseGroup { brace_token, items })))
1438 }
1439 } else {
1440 Err(lookahead.error())
1441 }
1442 }
1443
1444 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1445 impl Parse for ItemStatic {
1446 fn parse(input: ParseStream) -> Result<Self> {
1447 Ok(ItemStatic {
1448 attrs: input.call(Attribute::parse_outer)?,
1449 vis: input.parse()?,
1450 static_token: input.parse()?,
1451 mutability: input.parse()?,
1452 ident: input.parse()?,
1453 colon_token: input.parse()?,
1454 ty: input.parse()?,
1455 eq_token: input.parse()?,
1456 expr: input.parse()?,
1457 semi_token: input.parse()?,
1458 })
1459 }
1460 }
1461
1462 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1463 impl Parse for ItemConst {
1464 fn parse(input: ParseStream) -> Result<Self> {
1465 let attrs = input.call(Attribute::parse_outer)?;
1466 let vis: Visibility = input.parse()?;
1467 let const_token: crate::token::ConstToken![const] = input.parse()?;
1468
1469 let lookahead = input.lookahead1();
1470 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
1471 input.call(Ident::parse_any)?
1472 } else {
1473 return Err(lookahead.error());
1474 };
1475
1476 let colon_token: crate::token::ColonToken![:] = input.parse()?;
1477 let ty: Type = input.parse()?;
1478 let eq_token: crate::token::EqToken![=] = input.parse()?;
1479 let expr: Expr = input.parse()?;
1480 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1481
1482 Ok(ItemConst {
1483 attrs,
1484 vis,
1485 const_token,
1486 ident,
1487 generics: Generics::default(),
1488 colon_token,
1489 ty: Box::new(ty),
1490 eq_token,
1491 expr: Box::new(expr),
1492 semi_token,
1493 })
1494 }
1495 }
1496
1497 fn peek_signature(input: ParseStream, allow_safe: bool) -> bool {
1498 let fork = input.fork();
1499 fork.parse::<Option<crate::token::ConstToken![const]>>().is_ok()
1500 && fork.parse::<Option<crate::token::AsyncToken![async]>>().is_ok()
1501 && ((allow_safe
1502 && token::parsing::peek_keyword(fork.cursor(), "safe")
1503 && token::parsing::keyword(&fork, "safe").is_ok())
1504 || fork.parse::<Option<crate::token::UnsafeToken![unsafe]>>().is_ok())
1505 && fork.parse::<Option<Abi>>().is_ok()
1506 && fork.peek(crate::token::FnToken![fn])
1507 }
1508
1509 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1510 impl Parse for Signature {
1511 fn parse(input: ParseStream) -> Result<Self> {
1512 let allow_safe = false;
1513 parse_signature(input, allow_safe).map(Option::unwrap)
1514 }
1515 }
1516
1517 fn parse_signature(input: ParseStream, allow_safe: bool) -> Result<Option<Signature>> {
1518 let constness: Option<crate::token::ConstToken![const]> = input.parse()?;
1519 let asyncness: Option<crate::token::AsyncToken![async]> = input.parse()?;
1520 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1521 let safe = allow_safe
1522 && unsafety.is_none()
1523 && token::parsing::peek_keyword(input.cursor(), "safe");
1524 if safe {
1525 token::parsing::keyword(input, "safe")?;
1526 }
1527 let abi: Option<Abi> = input.parse()?;
1528 let fn_token: crate::token::FnToken![fn] = input.parse()?;
1529 let ident: Ident = input.parse()?;
1530 let mut generics: Generics = input.parse()?;
1531
1532 let content;
1533 let paren_token = match crate::__private::parse_parens(&input) {
crate::__private::Ok(parens) => {
content = parens.content;
_ = content;
parens.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}parenthesized!(content in input);
1534 let (inputs, variadic) = parse_fn_args(&content)?;
1535
1536 let output: ReturnType = input.parse()?;
1537 generics.where_clause = input.parse()?;
1538
1539 Ok(if safe {
1540 None
1541 } else {
1542 Some(Signature {
1543 constness,
1544 asyncness,
1545 unsafety,
1546 abi,
1547 fn_token,
1548 ident,
1549 generics,
1550 paren_token,
1551 inputs,
1552 variadic,
1553 output,
1554 })
1555 })
1556 }
1557
1558 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1559 impl Parse for ItemFn {
1560 fn parse(input: ParseStream) -> Result<Self> {
1561 let outer_attrs = input.call(Attribute::parse_outer)?;
1562 let vis: Visibility = input.parse()?;
1563 let sig: Signature = input.parse()?;
1564 parse_rest_of_fn(input, outer_attrs, vis, sig)
1565 }
1566 }
1567
1568 fn parse_rest_of_fn(
1569 input: ParseStream,
1570 mut attrs: Vec<Attribute>,
1571 vis: Visibility,
1572 sig: Signature,
1573 ) -> Result<ItemFn> {
1574 let content;
1575 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
1576 attr::parsing::parse_inner(&content, &mut attrs)?;
1577 let stmts = content.call(Block::parse_within)?;
1578
1579 Ok(ItemFn {
1580 attrs,
1581 vis,
1582 sig,
1583 block: Box::new(Block { brace_token, stmts }),
1584 })
1585 }
1586
1587 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1588 impl Parse for FnArg {
1589 fn parse(input: ParseStream) -> Result<Self> {
1590 let allow_variadic = false;
1591 let attrs = input.call(Attribute::parse_outer)?;
1592 match parse_fn_arg_or_variadic(input, attrs, allow_variadic)? {
1593 FnArgOrVariadic::FnArg(arg) => Ok(arg),
1594 FnArgOrVariadic::Variadic(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1595 }
1596 }
1597 }
1598
1599 enum FnArgOrVariadic {
1600 FnArg(FnArg),
1601 Variadic(Variadic),
1602 }
1603
1604 fn parse_fn_arg_or_variadic(
1605 input: ParseStream,
1606 attrs: Vec<Attribute>,
1607 allow_variadic: bool,
1608 ) -> Result<FnArgOrVariadic> {
1609 let ahead = input.fork();
1610 if let Ok((reference, mutability, self_token)) = parse_receiver_begin(&ahead) {
1611 input.advance_to(&ahead);
1612 let mut receiver = parse_rest_of_receiver(reference, mutability, self_token, input)?;
1613 receiver.attrs = attrs;
1614 return Ok(FnArgOrVariadic::FnArg(FnArg::Receiver(receiver)));
1615 }
1616
1617 if input.peek(Ident) && input.peek2(crate::token::LtToken![<]) {
1621 let span = input.span();
1622 return Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType {
1623 attrs,
1624 pat: Box::new(Pat::Wild(PatWild {
1625 attrs: Vec::new(),
1626 underscore_token: crate::token::UnderscoreToken,
1627 })),
1628 colon_token: crate::token::ColonToken,
1629 ty: input.parse()?,
1630 })));
1631 }
1632
1633 let pat = Box::new(Pat::parse_single(input)?);
1634 let colon_token: crate::token::ColonToken![:] = input.parse()?;
1635
1636 if allow_variadic {
1637 if let Some(dots) = input.parse::<Option<crate::token::DotDotDotToken![...]>>()? {
1638 return Ok(FnArgOrVariadic::Variadic(Variadic {
1639 attrs,
1640 pat: Some((pat, colon_token)),
1641 dots,
1642 comma: None,
1643 }));
1644 }
1645 }
1646
1647 Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType {
1648 attrs,
1649 pat,
1650 colon_token,
1651 ty: input.parse()?,
1652 })))
1653 }
1654
1655 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1656 impl Parse for Receiver {
1657 fn parse(input: ParseStream) -> Result<Self> {
1658 let (reference, mutability, self_token) = parse_receiver_begin(input)?;
1659 parse_rest_of_receiver(reference, mutability, self_token, input)
1660 }
1661 }
1662
1663 fn parse_receiver_begin(
1664 input: ParseStream,
1665 ) -> Result<(
1666 Option<(crate::token::AndToken![&], Option<Lifetime>)>,
1667 Option<crate::token::MutToken![mut]>,
1668 crate::token::SelfValueToken![self],
1669 )> {
1670 let reference = if input.peek(crate::token::AndToken![&]) {
1671 let ampersand: crate::token::AndToken![&] = input.parse()?;
1672 let lifetime: Option<Lifetime> = input.parse()?;
1673 Some((ampersand, lifetime))
1674 } else {
1675 None
1676 };
1677 let mutability: Option<crate::token::MutToken![mut]> = input.parse()?;
1678 let self_token: crate::token::SelfValueToken![self] = input.parse()?;
1679 if input.peek(crate::token::PathSepToken![::]) {
1680 return Err(input.error("expected `:`"));
1681 }
1682 Ok((reference, mutability, self_token))
1683 }
1684
1685 fn parse_rest_of_receiver(
1686 reference: Option<(crate::token::AndToken![&], Option<Lifetime>)>,
1687 mutability: Option<crate::token::MutToken![mut]>,
1688 self_token: crate::token::SelfValueToken![self],
1689 input: ParseStream,
1690 ) -> Result<Receiver> {
1691 let colon_token: Option<crate::token::ColonToken![:]> = if reference.is_some() {
1692 None
1693 } else {
1694 input.parse()?
1695 };
1696 let ty: Type = if colon_token.is_some() {
1697 input.parse()?
1698 } else {
1699 let mut ty = Type::Path(TypePath {
1700 qself: None,
1701 path: Path::from(Ident::new("Self", self_token.span)),
1702 });
1703 if let Some((ampersand, lifetime)) = reference.as_ref() {
1704 ty = Type::Reference(TypeReference {
1705 and_token: crate::token::AndToken,
1706 lifetime: lifetime.clone(),
1707 mutability: mutability.as_ref().map(|m| crate::token::MutToken),
1708 elem: Box::new(ty),
1709 });
1710 }
1711 ty
1712 };
1713 Ok(Receiver {
1714 attrs: Vec::new(),
1715 reference,
1716 mutability,
1717 self_token,
1718 colon_token,
1719 ty: Box::new(ty),
1720 })
1721 }
1722
1723 fn parse_fn_args(
1724 input: ParseStream,
1725 ) -> Result<(Punctuated<FnArg, crate::token::CommaToken![,]>, Option<Variadic>)> {
1726 let mut args = Punctuated::new();
1727 let mut variadic = None;
1728 let mut has_receiver = false;
1729
1730 while !input.is_empty() {
1731 let attrs = input.call(Attribute::parse_outer)?;
1732
1733 if let Some(dots) = input.parse::<Option<crate::token::DotDotDotToken![...]>>()? {
1734 variadic = Some(Variadic {
1735 attrs,
1736 pat: None,
1737 dots,
1738 comma: if input.is_empty() {
1739 None
1740 } else {
1741 Some(input.parse()?)
1742 },
1743 });
1744 break;
1745 }
1746
1747 let allow_variadic = true;
1748 let arg = match parse_fn_arg_or_variadic(input, attrs, allow_variadic)? {
1749 FnArgOrVariadic::FnArg(arg) => arg,
1750 FnArgOrVariadic::Variadic(arg) => {
1751 variadic = Some(Variadic {
1752 comma: if input.is_empty() {
1753 None
1754 } else {
1755 Some(input.parse()?)
1756 },
1757 ..arg
1758 });
1759 break;
1760 }
1761 };
1762
1763 match &arg {
1764 FnArg::Receiver(receiver) if has_receiver => {
1765 return Err(Error::new(
1766 receiver.self_token.span,
1767 "unexpected second method receiver",
1768 ));
1769 }
1770 FnArg::Receiver(receiver) if !args.is_empty() => {
1771 return Err(Error::new(
1772 receiver.self_token.span,
1773 "unexpected method receiver",
1774 ));
1775 }
1776 FnArg::Receiver(_) => has_receiver = true,
1777 FnArg::Typed(_) => {}
1778 }
1779 args.push_value(arg);
1780
1781 if input.is_empty() {
1782 break;
1783 }
1784
1785 let comma: crate::token::CommaToken![,] = input.parse()?;
1786 args.push_punct(comma);
1787 }
1788
1789 Ok((args, variadic))
1790 }
1791
1792 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1793 impl Parse for ItemMod {
1794 fn parse(input: ParseStream) -> Result<Self> {
1795 let mut attrs = input.call(Attribute::parse_outer)?;
1796 let vis: Visibility = input.parse()?;
1797 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1798 let mod_token: crate::token::ModToken![mod] = input.parse()?;
1799 let ident: Ident = if input.peek(crate::token::TryToken![try]) {
1800 input.call(Ident::parse_any)
1801 } else {
1802 input.parse()
1803 }?;
1804
1805 let lookahead = input.lookahead1();
1806 if lookahead.peek(crate::token::SemiToken![;]) {
1807 Ok(ItemMod {
1808 attrs,
1809 vis,
1810 unsafety,
1811 mod_token,
1812 ident,
1813 content: None,
1814 semi: Some(input.parse()?),
1815 })
1816 } else if lookahead.peek(token::Brace) {
1817 let content;
1818 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
1819 attr::parsing::parse_inner(&content, &mut attrs)?;
1820
1821 let mut items = Vec::new();
1822 while !content.is_empty() {
1823 items.push(content.parse()?);
1824 }
1825
1826 Ok(ItemMod {
1827 attrs,
1828 vis,
1829 unsafety,
1830 mod_token,
1831 ident,
1832 content: Some((brace_token, items)),
1833 semi: None,
1834 })
1835 } else {
1836 Err(lookahead.error())
1837 }
1838 }
1839 }
1840
1841 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1842 impl Parse for ItemForeignMod {
1843 fn parse(input: ParseStream) -> Result<Self> {
1844 let mut attrs = input.call(Attribute::parse_outer)?;
1845 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1846 let abi: Abi = input.parse()?;
1847
1848 let content;
1849 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
1850 attr::parsing::parse_inner(&content, &mut attrs)?;
1851 let mut items = Vec::new();
1852 while !content.is_empty() {
1853 items.push(content.parse()?);
1854 }
1855
1856 Ok(ItemForeignMod {
1857 attrs,
1858 unsafety,
1859 abi,
1860 brace_token,
1861 items,
1862 })
1863 }
1864 }
1865
1866 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1867 impl Parse for ForeignItem {
1868 fn parse(input: ParseStream) -> Result<Self> {
1869 let begin = input.fork();
1870 let mut attrs = input.call(Attribute::parse_outer)?;
1871 let ahead = input.fork();
1872 let vis: Visibility = ahead.parse()?;
1873
1874 let lookahead = ahead.lookahead1();
1875 let allow_safe = true;
1876 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
1877 let vis: Visibility = input.parse()?;
1878 let sig = parse_signature(input, allow_safe)?;
1879 let has_safe = sig.is_none();
1880 let has_body = input.peek(token::Brace);
1881 let semi_token: Option<crate::token::SemiToken![;]> = if has_body {
1882 let content;
1883 match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
};braced!(content in input);
1884 content.call(Attribute::parse_inner)?;
1885 content.call(Block::parse_within)?;
1886 None
1887 } else {
1888 Some(input.parse()?)
1889 };
1890 if has_safe || has_body {
1891 Ok(ForeignItem::Verbatim(verbatim::between(&begin, input)))
1892 } else {
1893 Ok(ForeignItem::Fn(ForeignItemFn {
1894 attrs: Vec::new(),
1895 vis,
1896 sig: sig.unwrap(),
1897 semi_token: semi_token.unwrap(),
1898 }))
1899 }
1900 } else if lookahead.peek(crate::token::StaticToken![static])
1901 || ((ahead.peek(crate::token::UnsafeToken![unsafe])
1902 || token::parsing::peek_keyword(ahead.cursor(), "safe"))
1903 && ahead.peek2(crate::token::StaticToken![static]))
1904 {
1905 let vis = input.parse()?;
1906 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1907 let safe =
1908 unsafety.is_none() && token::parsing::peek_keyword(input.cursor(), "safe");
1909 if safe {
1910 token::parsing::keyword(input, "safe")?;
1911 }
1912 let static_token = input.parse()?;
1913 let mutability = input.parse()?;
1914 let ident = input.parse()?;
1915 let colon_token = input.parse()?;
1916 let ty = input.parse()?;
1917 let has_value = input.peek(crate::token::EqToken![=]);
1918 if has_value {
1919 input.parse::<crate::token::EqToken![=]>()?;
1920 input.parse::<Expr>()?;
1921 }
1922 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1923 if unsafety.is_some() || safe || has_value {
1924 Ok(ForeignItem::Verbatim(verbatim::between(&begin, input)))
1925 } else {
1926 Ok(ForeignItem::Static(ForeignItemStatic {
1927 attrs: Vec::new(),
1928 vis,
1929 static_token,
1930 mutability,
1931 ident,
1932 colon_token,
1933 ty,
1934 semi_token,
1935 }))
1936 }
1937 } else if lookahead.peek(crate::token::TypeToken![type]) {
1938 parse_foreign_item_type(begin, input)
1939 } else if vis.is_inherited()
1940 && (lookahead.peek(Ident)
1941 || lookahead.peek(crate::token::SelfValueToken![self])
1942 || lookahead.peek(crate::token::SuperToken![super])
1943 || lookahead.peek(crate::token::CrateToken![crate])
1944 || lookahead.peek(crate::token::PathSepToken![::]))
1945 {
1946 input.parse().map(ForeignItem::Macro)
1947 } else {
1948 Err(lookahead.error())
1949 }?;
1950
1951 let item_attrs = match &mut item {
1952 ForeignItem::Fn(item) => &mut item.attrs,
1953 ForeignItem::Static(item) => &mut item.attrs,
1954 ForeignItem::Type(item) => &mut item.attrs,
1955 ForeignItem::Macro(item) => &mut item.attrs,
1956 ForeignItem::Verbatim(_) => return Ok(item),
1957 };
1958 attrs.append(item_attrs);
1959 *item_attrs = attrs;
1960
1961 Ok(item)
1962 }
1963 }
1964
1965 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1966 impl Parse for ForeignItemFn {
1967 fn parse(input: ParseStream) -> Result<Self> {
1968 let attrs = input.call(Attribute::parse_outer)?;
1969 let vis: Visibility = input.parse()?;
1970 let sig: Signature = input.parse()?;
1971 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1972 Ok(ForeignItemFn {
1973 attrs,
1974 vis,
1975 sig,
1976 semi_token,
1977 })
1978 }
1979 }
1980
1981 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1982 impl Parse for ForeignItemStatic {
1983 fn parse(input: ParseStream) -> Result<Self> {
1984 Ok(ForeignItemStatic {
1985 attrs: input.call(Attribute::parse_outer)?,
1986 vis: input.parse()?,
1987 static_token: input.parse()?,
1988 mutability: input.parse()?,
1989 ident: input.parse()?,
1990 colon_token: input.parse()?,
1991 ty: input.parse()?,
1992 semi_token: input.parse()?,
1993 })
1994 }
1995 }
1996
1997 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1998 impl Parse for ForeignItemType {
1999 fn parse(input: ParseStream) -> Result<Self> {
2000 Ok(ForeignItemType {
2001 attrs: input.call(Attribute::parse_outer)?,
2002 vis: input.parse()?,
2003 type_token: input.parse()?,
2004 ident: input.parse()?,
2005 generics: {
2006 let mut generics: Generics = input.parse()?;
2007 generics.where_clause = input.parse()?;
2008 generics
2009 },
2010 semi_token: input.parse()?,
2011 })
2012 }
2013 }
2014
2015 fn parse_foreign_item_type(begin: ParseBuffer, input: ParseStream) -> Result<ForeignItem> {
2016 let FlexibleItemType {
2017 vis,
2018 defaultness: _,
2019 type_token,
2020 ident,
2021 generics,
2022 colon_token,
2023 bounds: _,
2024 ty,
2025 semi_token,
2026 } = FlexibleItemType::parse(
2027 input,
2028 TypeDefaultness::Disallowed,
2029 WhereClauseLocation::Both,
2030 )?;
2031
2032 if colon_token.is_some() || ty.is_some() {
2033 Ok(ForeignItem::Verbatim(verbatim::between(&begin, input)))
2034 } else {
2035 Ok(ForeignItem::Type(ForeignItemType {
2036 attrs: Vec::new(),
2037 vis,
2038 type_token,
2039 ident,
2040 generics,
2041 semi_token,
2042 }))
2043 }
2044 }
2045
2046 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2047 impl Parse for ForeignItemMacro {
2048 fn parse(input: ParseStream) -> Result<Self> {
2049 let attrs = input.call(Attribute::parse_outer)?;
2050 let mac: Macro = input.parse()?;
2051 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2052 None
2053 } else {
2054 Some(input.parse()?)
2055 };
2056 Ok(ForeignItemMacro {
2057 attrs,
2058 mac,
2059 semi_token,
2060 })
2061 }
2062 }
2063
2064 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2065 impl Parse for ItemType {
2066 fn parse(input: ParseStream) -> Result<Self> {
2067 Ok(ItemType {
2068 attrs: input.call(Attribute::parse_outer)?,
2069 vis: input.parse()?,
2070 type_token: input.parse()?,
2071 ident: input.parse()?,
2072 generics: {
2073 let mut generics: Generics = input.parse()?;
2074 generics.where_clause = input.parse()?;
2075 generics
2076 },
2077 eq_token: input.parse()?,
2078 ty: input.parse()?,
2079 semi_token: input.parse()?,
2080 })
2081 }
2082 }
2083
2084 fn parse_item_type(begin: ParseBuffer, input: ParseStream) -> Result<Item> {
2085 let FlexibleItemType {
2086 vis,
2087 defaultness: _,
2088 type_token,
2089 ident,
2090 generics,
2091 colon_token,
2092 bounds: _,
2093 ty,
2094 semi_token,
2095 } = FlexibleItemType::parse(
2096 input,
2097 TypeDefaultness::Disallowed,
2098 WhereClauseLocation::BeforeEq,
2099 )?;
2100
2101 let (eq_token, ty) = match ty {
2102 Some(ty) if colon_token.is_none() => ty,
2103 _ => return Ok(Item::Verbatim(verbatim::between(&begin, input))),
2104 };
2105
2106 Ok(Item::Type(ItemType {
2107 attrs: Vec::new(),
2108 vis,
2109 type_token,
2110 ident,
2111 generics,
2112 eq_token,
2113 ty: Box::new(ty),
2114 semi_token,
2115 }))
2116 }
2117
2118 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2119 impl Parse for ItemStruct {
2120 fn parse(input: ParseStream) -> Result<Self> {
2121 let attrs = input.call(Attribute::parse_outer)?;
2122 let vis = input.parse::<Visibility>()?;
2123 let struct_token = input.parse::<crate::token::StructToken![struct]>()?;
2124 let ident = input.parse::<Ident>()?;
2125 let generics = input.parse::<Generics>()?;
2126 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
2127 Ok(ItemStruct {
2128 attrs,
2129 vis,
2130 struct_token,
2131 ident,
2132 generics: Generics {
2133 where_clause,
2134 ..generics
2135 },
2136 fields,
2137 semi_token,
2138 })
2139 }
2140 }
2141
2142 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2143 impl Parse for ItemEnum {
2144 fn parse(input: ParseStream) -> Result<Self> {
2145 let attrs = input.call(Attribute::parse_outer)?;
2146 let vis = input.parse::<Visibility>()?;
2147 let enum_token = input.parse::<crate::token::EnumToken![enum]>()?;
2148 let ident = input.parse::<Ident>()?;
2149 let generics = input.parse::<Generics>()?;
2150 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
2151 Ok(ItemEnum {
2152 attrs,
2153 vis,
2154 enum_token,
2155 ident,
2156 generics: Generics {
2157 where_clause,
2158 ..generics
2159 },
2160 brace_token,
2161 variants,
2162 })
2163 }
2164 }
2165
2166 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2167 impl Parse for ItemUnion {
2168 fn parse(input: ParseStream) -> Result<Self> {
2169 let attrs = input.call(Attribute::parse_outer)?;
2170 let vis = input.parse::<Visibility>()?;
2171 let union_token = input.parse::<crate::token::UnionToken![union]>()?;
2172 let ident = input.parse::<Ident>()?;
2173 let generics = input.parse::<Generics>()?;
2174 let (where_clause, fields) = derive::parsing::data_union(input)?;
2175 Ok(ItemUnion {
2176 attrs,
2177 vis,
2178 union_token,
2179 ident,
2180 generics: Generics {
2181 where_clause,
2182 ..generics
2183 },
2184 fields,
2185 })
2186 }
2187 }
2188
2189 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
2190 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
2191 let lookahead = input.lookahead1();
2192 if lookahead.peek(token::Brace)
2193 || lookahead.peek(crate::token::ColonToken![:])
2194 || lookahead.peek(crate::token::WhereToken![where])
2195 {
2196 let unsafety = None;
2197 let auto_token = None;
2198 parse_rest_of_trait(
2199 input,
2200 attrs,
2201 vis,
2202 unsafety,
2203 auto_token,
2204 trait_token,
2205 ident,
2206 generics,
2207 )
2208 .map(Item::Trait)
2209 } else if lookahead.peek(crate::token::EqToken![=]) {
2210 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
2211 .map(Item::TraitAlias)
2212 } else {
2213 Err(lookahead.error())
2214 }
2215 }
2216
2217 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2218 impl Parse for ItemTrait {
2219 fn parse(input: ParseStream) -> Result<Self> {
2220 let outer_attrs = input.call(Attribute::parse_outer)?;
2221 let vis: Visibility = input.parse()?;
2222 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
2223 let auto_token: Option<crate::token::AutoToken![auto]> = input.parse()?;
2224 let trait_token: crate::token::TraitToken![trait] = input.parse()?;
2225 let ident: Ident = input.parse()?;
2226 let generics: Generics = input.parse()?;
2227 parse_rest_of_trait(
2228 input,
2229 outer_attrs,
2230 vis,
2231 unsafety,
2232 auto_token,
2233 trait_token,
2234 ident,
2235 generics,
2236 )
2237 }
2238 }
2239
2240 fn parse_rest_of_trait(
2241 input: ParseStream,
2242 mut attrs: Vec<Attribute>,
2243 vis: Visibility,
2244 unsafety: Option<crate::token::UnsafeToken![unsafe]>,
2245 auto_token: Option<crate::token::AutoToken![auto]>,
2246 trait_token: crate::token::TraitToken![trait],
2247 ident: Ident,
2248 mut generics: Generics,
2249 ) -> Result<ItemTrait> {
2250 let colon_token: Option<crate::token::ColonToken![:]> = input.parse()?;
2251
2252 let mut supertraits = Punctuated::new();
2253 if colon_token.is_some() {
2254 loop {
2255 if input.peek(crate::token::WhereToken![where]) || input.peek(token::Brace) {
2256 break;
2257 }
2258 supertraits.push_value({
2259 let allow_precise_capture = false;
2260 let allow_const = true;
2261 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
2262 });
2263 if input.peek(crate::token::WhereToken![where]) || input.peek(token::Brace) {
2264 break;
2265 }
2266 supertraits.push_punct(input.parse()?);
2267 }
2268 }
2269
2270 generics.where_clause = input.parse()?;
2271
2272 let content;
2273 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
2274 attr::parsing::parse_inner(&content, &mut attrs)?;
2275 let mut items = Vec::new();
2276 while !content.is_empty() {
2277 items.push(content.parse()?);
2278 }
2279
2280 Ok(ItemTrait {
2281 attrs,
2282 vis,
2283 unsafety,
2284 auto_token,
2285 restriction: None,
2286 trait_token,
2287 ident,
2288 generics,
2289 colon_token,
2290 supertraits,
2291 brace_token,
2292 items,
2293 })
2294 }
2295
2296 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2297 impl Parse for ItemTraitAlias {
2298 fn parse(input: ParseStream) -> Result<Self> {
2299 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
2300 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
2301 }
2302 }
2303
2304 fn parse_start_of_trait_alias(
2305 input: ParseStream,
2306 ) -> Result<(Vec<Attribute>, Visibility, crate::token::TraitToken![trait], Ident, Generics)> {
2307 let attrs = input.call(Attribute::parse_outer)?;
2308 let vis: Visibility = input.parse()?;
2309 let trait_token: crate::token::TraitToken![trait] = input.parse()?;
2310 let ident: Ident = input.parse()?;
2311 let generics: Generics = input.parse()?;
2312 Ok((attrs, vis, trait_token, ident, generics))
2313 }
2314
2315 fn parse_rest_of_trait_alias(
2316 input: ParseStream,
2317 attrs: Vec<Attribute>,
2318 vis: Visibility,
2319 trait_token: crate::token::TraitToken![trait],
2320 ident: Ident,
2321 mut generics: Generics,
2322 ) -> Result<ItemTraitAlias> {
2323 let eq_token: crate::token::EqToken![=] = input.parse()?;
2324
2325 let mut bounds = Punctuated::new();
2326 loop {
2327 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::SemiToken![;]) {
2328 break;
2329 }
2330 bounds.push_value({
2331 let allow_precise_capture = false;
2332 let allow_const = false;
2333 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
2334 });
2335 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::SemiToken![;]) {
2336 break;
2337 }
2338 bounds.push_punct(input.parse()?);
2339 }
2340
2341 generics.where_clause = input.parse()?;
2342 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2343
2344 Ok(ItemTraitAlias {
2345 attrs,
2346 vis,
2347 trait_token,
2348 ident,
2349 generics,
2350 eq_token,
2351 bounds,
2352 semi_token,
2353 })
2354 }
2355
2356 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2357 impl Parse for TraitItem {
2358 fn parse(input: ParseStream) -> Result<Self> {
2359 let begin = input.fork();
2360 let mut attrs = input.call(Attribute::parse_outer)?;
2361 let vis: Visibility = input.parse()?;
2362 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2363 let ahead = input.fork();
2364
2365 let lookahead = ahead.lookahead1();
2366 let allow_safe = false;
2367 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
2368 input.parse().map(TraitItem::Fn)
2369 } else if lookahead.peek(crate::token::ConstToken![const]) {
2370 let const_token: crate::token::ConstToken![const] = ahead.parse()?;
2371 let lookahead = ahead.lookahead1();
2372 if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2373 input.advance_to(&ahead);
2374 let ident = input.call(Ident::parse_any)?;
2375 let mut generics: Generics = input.parse()?;
2376 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2377 let ty: Type = input.parse()?;
2378 let default = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
2379 let expr: Expr = input.parse()?;
2380 Some((eq_token, expr))
2381 } else {
2382 None
2383 };
2384 generics.where_clause = input.parse()?;
2385 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2386 if generics.lt_token.is_none() && generics.where_clause.is_none() {
2387 Ok(TraitItem::Const(TraitItemConst {
2388 attrs: Vec::new(),
2389 const_token,
2390 ident,
2391 generics,
2392 colon_token,
2393 ty,
2394 default,
2395 semi_token,
2396 }))
2397 } else {
2398 return Ok(TraitItem::Verbatim(verbatim::between(&begin, input)));
2399 }
2400 } else if lookahead.peek(crate::token::AsyncToken![async])
2401 || lookahead.peek(crate::token::UnsafeToken![unsafe])
2402 || lookahead.peek(crate::token::ExternToken![extern])
2403 || lookahead.peek(crate::token::FnToken![fn])
2404 {
2405 input.parse().map(TraitItem::Fn)
2406 } else {
2407 Err(lookahead.error())
2408 }
2409 } else if lookahead.peek(crate::token::TypeToken![type]) {
2410 parse_trait_item_type(begin.fork(), input)
2411 } else if vis.is_inherited()
2412 && defaultness.is_none()
2413 && (lookahead.peek(Ident)
2414 || lookahead.peek(crate::token::SelfValueToken![self])
2415 || lookahead.peek(crate::token::SuperToken![super])
2416 || lookahead.peek(crate::token::CrateToken![crate])
2417 || lookahead.peek(crate::token::PathSepToken![::]))
2418 {
2419 input.parse().map(TraitItem::Macro)
2420 } else {
2421 Err(lookahead.error())
2422 }?;
2423
2424 match (vis, defaultness) {
2425 (Visibility::Inherited, None) => {}
2426 _ => return Ok(TraitItem::Verbatim(verbatim::between(&begin, input))),
2427 }
2428
2429 let item_attrs = match &mut item {
2430 TraitItem::Const(item) => &mut item.attrs,
2431 TraitItem::Fn(item) => &mut item.attrs,
2432 TraitItem::Type(item) => &mut item.attrs,
2433 TraitItem::Macro(item) => &mut item.attrs,
2434 TraitItem::Verbatim(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
2435 };
2436 attrs.append(item_attrs);
2437 *item_attrs = attrs;
2438 Ok(item)
2439 }
2440 }
2441
2442 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2443 impl Parse for TraitItemConst {
2444 fn parse(input: ParseStream) -> Result<Self> {
2445 let attrs = input.call(Attribute::parse_outer)?;
2446 let const_token: crate::token::ConstToken![const] = input.parse()?;
2447
2448 let lookahead = input.lookahead1();
2449 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2450 input.call(Ident::parse_any)?
2451 } else {
2452 return Err(lookahead.error());
2453 };
2454
2455 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2456 let ty: Type = input.parse()?;
2457 let default = if input.peek(crate::token::EqToken![=]) {
2458 let eq_token: crate::token::EqToken![=] = input.parse()?;
2459 let default: Expr = input.parse()?;
2460 Some((eq_token, default))
2461 } else {
2462 None
2463 };
2464 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2465
2466 Ok(TraitItemConst {
2467 attrs,
2468 const_token,
2469 ident,
2470 generics: Generics::default(),
2471 colon_token,
2472 ty,
2473 default,
2474 semi_token,
2475 })
2476 }
2477 }
2478
2479 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2480 impl Parse for TraitItemFn {
2481 fn parse(input: ParseStream) -> Result<Self> {
2482 let mut attrs = input.call(Attribute::parse_outer)?;
2483 let sig: Signature = input.parse()?;
2484
2485 let lookahead = input.lookahead1();
2486 let (brace_token, stmts, semi_token) = if lookahead.peek(token::Brace) {
2487 let content;
2488 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
2489 attr::parsing::parse_inner(&content, &mut attrs)?;
2490 let stmts = content.call(Block::parse_within)?;
2491 (Some(brace_token), stmts, None)
2492 } else if lookahead.peek(crate::token::SemiToken![;]) {
2493 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2494 (None, Vec::new(), Some(semi_token))
2495 } else {
2496 return Err(lookahead.error());
2497 };
2498
2499 Ok(TraitItemFn {
2500 attrs,
2501 sig,
2502 default: brace_token.map(|brace_token| Block { brace_token, stmts }),
2503 semi_token,
2504 })
2505 }
2506 }
2507
2508 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2509 impl Parse for TraitItemType {
2510 fn parse(input: ParseStream) -> Result<Self> {
2511 let attrs = input.call(Attribute::parse_outer)?;
2512 let type_token: crate::token::TypeToken![type] = input.parse()?;
2513 let ident: Ident = input.parse()?;
2514 let mut generics: Generics = input.parse()?;
2515 let (colon_token, bounds) = FlexibleItemType::parse_optional_bounds(input)?;
2516 let default = FlexibleItemType::parse_optional_definition(input)?;
2517 generics.where_clause = input.parse()?;
2518 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2519 Ok(TraitItemType {
2520 attrs,
2521 type_token,
2522 ident,
2523 generics,
2524 colon_token,
2525 bounds,
2526 default,
2527 semi_token,
2528 })
2529 }
2530 }
2531
2532 fn parse_trait_item_type(begin: ParseBuffer, input: ParseStream) -> Result<TraitItem> {
2533 let FlexibleItemType {
2534 vis,
2535 defaultness: _,
2536 type_token,
2537 ident,
2538 generics,
2539 colon_token,
2540 bounds,
2541 ty,
2542 semi_token,
2543 } = FlexibleItemType::parse(
2544 input,
2545 TypeDefaultness::Disallowed,
2546 WhereClauseLocation::AfterEq,
2547 )?;
2548
2549 if vis.is_some() {
2550 Ok(TraitItem::Verbatim(verbatim::between(&begin, input)))
2551 } else {
2552 Ok(TraitItem::Type(TraitItemType {
2553 attrs: Vec::new(),
2554 type_token,
2555 ident,
2556 generics,
2557 colon_token,
2558 bounds,
2559 default: ty,
2560 semi_token,
2561 }))
2562 }
2563 }
2564
2565 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2566 impl Parse for TraitItemMacro {
2567 fn parse(input: ParseStream) -> Result<Self> {
2568 let attrs = input.call(Attribute::parse_outer)?;
2569 let mac: Macro = input.parse()?;
2570 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2571 None
2572 } else {
2573 Some(input.parse()?)
2574 };
2575 Ok(TraitItemMacro {
2576 attrs,
2577 mac,
2578 semi_token,
2579 })
2580 }
2581 }
2582
2583 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2584 impl Parse for ItemImpl {
2585 fn parse(input: ParseStream) -> Result<Self> {
2586 let allow_verbatim_impl = false;
2587 parse_impl(input, allow_verbatim_impl).map(Option::unwrap)
2588 }
2589 }
2590
2591 fn parse_impl(input: ParseStream, allow_verbatim_impl: bool) -> Result<Option<ItemImpl>> {
2592 let mut attrs = input.call(Attribute::parse_outer)?;
2593 let has_visibility = allow_verbatim_impl && input.parse::<Visibility>()?.is_some();
2594 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2595 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
2596 let impl_token: crate::token::ImplToken![impl] = input.parse()?;
2597
2598 let has_generics = generics::parsing::choose_generics_over_qpath(input);
2599 let mut generics: Generics = if has_generics {
2600 input.parse()?
2601 } else {
2602 Generics::default()
2603 };
2604
2605 let is_const_impl = allow_verbatim_impl
2606 && (input.peek(crate::token::ConstToken![const]) || input.peek(crate::token::QuestionToken![?]) && input.peek2(crate::token::ConstToken![const]));
2607 if is_const_impl {
2608 input.parse::<Option<crate::token::QuestionToken![?]>>()?;
2609 input.parse::<crate::token::ConstToken![const]>()?;
2610 }
2611
2612 let polarity = if input.peek(crate::token::NotToken![!]) && !input.peek2(token::Brace) {
2613 Some(input.parse::<crate::token::NotToken![!]>()?)
2614 } else {
2615 None
2616 };
2617
2618 #[cfg(not(feature = "printing"))]
2619 let first_ty_span = input.span();
2620 let mut first_ty: Type = input.parse()?;
2621 let self_ty: Type;
2622 let trait_;
2623
2624 let is_impl_for = input.peek(crate::token::ForToken![for]);
2625 if is_impl_for {
2626 let for_token: crate::token::ForToken![for] = input.parse()?;
2627 let mut first_ty_ref = &first_ty;
2628 while let Type::Group(ty) = first_ty_ref {
2629 first_ty_ref = &ty.elem;
2630 }
2631 if let Type::Path(TypePath { qself: None, .. }) = first_ty_ref {
2632 while let Type::Group(ty) = first_ty {
2633 first_ty = *ty.elem;
2634 }
2635 if let Type::Path(TypePath { qself: None, path }) = first_ty {
2636 trait_ = Some((polarity, path, for_token));
2637 } else {
2638 ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
2639 }
2640 } else if !allow_verbatim_impl {
2641 #[cfg(feature = "printing")]
2642 return Err(Error::new_spanned(first_ty_ref, "expected trait path"));
2643 #[cfg(not(feature = "printing"))]
2644 return Err(Error::new(first_ty_span, "expected trait path"));
2645 } else {
2646 trait_ = None;
2647 }
2648 self_ty = input.parse()?;
2649 } else if let Some(polarity) = polarity {
2650 return Err(Error::new(
2651 polarity.span,
2652 "inherent impls cannot be negative",
2653 ));
2654 } else {
2655 trait_ = None;
2656 self_ty = first_ty;
2657 }
2658
2659 generics.where_clause = input.parse()?;
2660
2661 let content;
2662 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
2663 attr::parsing::parse_inner(&content, &mut attrs)?;
2664
2665 let mut items = Vec::new();
2666 while !content.is_empty() {
2667 items.push(content.parse()?);
2668 }
2669
2670 if has_visibility || is_const_impl || is_impl_for && trait_.is_none() {
2671 Ok(None)
2672 } else {
2673 Ok(Some(ItemImpl {
2674 attrs,
2675 defaultness,
2676 unsafety,
2677 impl_token,
2678 generics,
2679 trait_,
2680 self_ty: Box::new(self_ty),
2681 brace_token,
2682 items,
2683 }))
2684 }
2685 }
2686
2687 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2688 impl Parse for ImplItem {
2689 fn parse(input: ParseStream) -> Result<Self> {
2690 let begin = input.fork();
2691 let mut attrs = input.call(Attribute::parse_outer)?;
2692 let ahead = input.fork();
2693 let vis: Visibility = ahead.parse()?;
2694
2695 let mut lookahead = ahead.lookahead1();
2696 let defaultness = if lookahead.peek(crate::token::DefaultToken![default]) && !ahead.peek2(crate::token::NotToken![!]) {
2697 let defaultness: crate::token::DefaultToken![default] = ahead.parse()?;
2698 lookahead = ahead.lookahead1();
2699 Some(defaultness)
2700 } else {
2701 None
2702 };
2703
2704 let allow_safe = false;
2705 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
2706 let allow_omitted_body = true;
2707 if let Some(item) = parse_impl_item_fn(input, allow_omitted_body)? {
2708 Ok(ImplItem::Fn(item))
2709 } else {
2710 Ok(ImplItem::Verbatim(verbatim::between(&begin, input)))
2711 }
2712 } else if lookahead.peek(crate::token::ConstToken![const]) {
2713 input.advance_to(&ahead);
2714 let const_token: crate::token::ConstToken![const] = input.parse()?;
2715 let lookahead = input.lookahead1();
2716 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2717 input.call(Ident::parse_any)?
2718 } else {
2719 return Err(lookahead.error());
2720 };
2721 let mut generics: Generics = input.parse()?;
2722 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2723 let ty: Type = input.parse()?;
2724 let value = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
2725 let expr: Expr = input.parse()?;
2726 Some((eq_token, expr))
2727 } else {
2728 None
2729 };
2730 generics.where_clause = input.parse()?;
2731 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2732 return match value {
2733 Some((eq_token, expr))
2734 if generics.lt_token.is_none() && generics.where_clause.is_none() =>
2735 {
2736 Ok(ImplItem::Const(ImplItemConst {
2737 attrs,
2738 vis,
2739 defaultness,
2740 const_token,
2741 ident,
2742 generics,
2743 colon_token,
2744 ty,
2745 eq_token,
2746 expr,
2747 semi_token,
2748 }))
2749 }
2750 _ => Ok(ImplItem::Verbatim(verbatim::between(&begin, input))),
2751 };
2752 } else if lookahead.peek(crate::token::TypeToken![type]) {
2753 parse_impl_item_type(begin, input)
2754 } else if vis.is_inherited()
2755 && defaultness.is_none()
2756 && (lookahead.peek(Ident)
2757 || lookahead.peek(crate::token::SelfValueToken![self])
2758 || lookahead.peek(crate::token::SuperToken![super])
2759 || lookahead.peek(crate::token::CrateToken![crate])
2760 || lookahead.peek(crate::token::PathSepToken![::]))
2761 {
2762 input.parse().map(ImplItem::Macro)
2763 } else {
2764 Err(lookahead.error())
2765 }?;
2766
2767 {
2768 let item_attrs = match &mut item {
2769 ImplItem::Const(item) => &mut item.attrs,
2770 ImplItem::Fn(item) => &mut item.attrs,
2771 ImplItem::Type(item) => &mut item.attrs,
2772 ImplItem::Macro(item) => &mut item.attrs,
2773 ImplItem::Verbatim(_) => return Ok(item),
2774 };
2775 attrs.append(item_attrs);
2776 *item_attrs = attrs;
2777 }
2778
2779 Ok(item)
2780 }
2781 }
2782
2783 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2784 impl Parse for ImplItemConst {
2785 fn parse(input: ParseStream) -> Result<Self> {
2786 let attrs = input.call(Attribute::parse_outer)?;
2787 let vis: Visibility = input.parse()?;
2788 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2789 let const_token: crate::token::ConstToken![const] = input.parse()?;
2790
2791 let lookahead = input.lookahead1();
2792 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2793 input.call(Ident::parse_any)?
2794 } else {
2795 return Err(lookahead.error());
2796 };
2797
2798 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2799 let ty: Type = input.parse()?;
2800 let eq_token: crate::token::EqToken![=] = input.parse()?;
2801 let expr: Expr = input.parse()?;
2802 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2803
2804 Ok(ImplItemConst {
2805 attrs,
2806 vis,
2807 defaultness,
2808 const_token,
2809 ident,
2810 generics: Generics::default(),
2811 colon_token,
2812 ty,
2813 eq_token,
2814 expr,
2815 semi_token,
2816 })
2817 }
2818 }
2819
2820 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2821 impl Parse for ImplItemFn {
2822 fn parse(input: ParseStream) -> Result<Self> {
2823 let allow_omitted_body = false;
2824 parse_impl_item_fn(input, allow_omitted_body).map(Option::unwrap)
2825 }
2826 }
2827
2828 fn parse_impl_item_fn(
2829 input: ParseStream,
2830 allow_omitted_body: bool,
2831 ) -> Result<Option<ImplItemFn>> {
2832 let mut attrs = input.call(Attribute::parse_outer)?;
2833 let vis: Visibility = input.parse()?;
2834 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2835 let sig: Signature = input.parse()?;
2836
2837 if allow_omitted_body && input.parse::<Option<crate::token::SemiToken![;]>>()?.is_some() {
2841 return Ok(None);
2842 }
2843
2844 let content;
2845 let brace_token = match crate::__private::parse_braces(&input) {
crate::__private::Ok(braces) => {
content = braces.content;
_ = content;
braces.token
}
crate::__private::Err(error) => { return crate::__private::Err(error); }
}braced!(content in input);
2846 attrs.extend(content.call(Attribute::parse_inner)?);
2847 let block = Block {
2848 brace_token,
2849 stmts: content.call(Block::parse_within)?,
2850 };
2851
2852 Ok(Some(ImplItemFn {
2853 attrs,
2854 vis,
2855 defaultness,
2856 sig,
2857 block,
2858 }))
2859 }
2860
2861 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2862 impl Parse for ImplItemType {
2863 fn parse(input: ParseStream) -> Result<Self> {
2864 let attrs = input.call(Attribute::parse_outer)?;
2865 let vis: Visibility = input.parse()?;
2866 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2867 let type_token: crate::token::TypeToken![type] = input.parse()?;
2868 let ident: Ident = input.parse()?;
2869 let mut generics: Generics = input.parse()?;
2870 let eq_token: crate::token::EqToken![=] = input.parse()?;
2871 let ty: Type = input.parse()?;
2872 generics.where_clause = input.parse()?;
2873 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2874 Ok(ImplItemType {
2875 attrs,
2876 vis,
2877 defaultness,
2878 type_token,
2879 ident,
2880 generics,
2881 eq_token,
2882 ty,
2883 semi_token,
2884 })
2885 }
2886 }
2887
2888 fn parse_impl_item_type(begin: ParseBuffer, input: ParseStream) -> Result<ImplItem> {
2889 let FlexibleItemType {
2890 vis,
2891 defaultness,
2892 type_token,
2893 ident,
2894 generics,
2895 colon_token,
2896 bounds: _,
2897 ty,
2898 semi_token,
2899 } = FlexibleItemType::parse(
2900 input,
2901 TypeDefaultness::Optional,
2902 WhereClauseLocation::AfterEq,
2903 )?;
2904
2905 let (eq_token, ty) = match ty {
2906 Some(ty) if colon_token.is_none() => ty,
2907 _ => return Ok(ImplItem::Verbatim(verbatim::between(&begin, input))),
2908 };
2909
2910 Ok(ImplItem::Type(ImplItemType {
2911 attrs: Vec::new(),
2912 vis,
2913 defaultness,
2914 type_token,
2915 ident,
2916 generics,
2917 eq_token,
2918 ty,
2919 semi_token,
2920 }))
2921 }
2922
2923 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2924 impl Parse for ImplItemMacro {
2925 fn parse(input: ParseStream) -> Result<Self> {
2926 let attrs = input.call(Attribute::parse_outer)?;
2927 let mac: Macro = input.parse()?;
2928 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2929 None
2930 } else {
2931 Some(input.parse()?)
2932 };
2933 Ok(ImplItemMacro {
2934 attrs,
2935 mac,
2936 semi_token,
2937 })
2938 }
2939 }
2940
2941 impl Visibility {
2942 fn is_inherited(&self) -> bool {
2943 match self {
2944 Visibility::Inherited => true,
2945 _ => false,
2946 }
2947 }
2948 }
2949
2950 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2951 impl Parse for StaticMutability {
2952 fn parse(input: ParseStream) -> Result<Self> {
2953 let mut_token: Option<crate::token::MutToken![mut]> = input.parse()?;
2954 Ok(mut_token.map_or(StaticMutability::None, StaticMutability::Mut))
2955 }
2956 }
2957}
2958
2959#[cfg(feature = "printing")]
2960mod printing {
2961 use crate::attr::FilterAttrs;
2962 use crate::data::Fields;
2963 use crate::item::{
2964 ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType, ImplItemConst,
2965 ImplItemFn, ImplItemMacro, ImplItemType, ItemConst, ItemEnum, ItemExternCrate, ItemFn,
2966 ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, ItemTrait,
2967 ItemTraitAlias, ItemType, ItemUnion, ItemUse, Receiver, Signature, StaticMutability,
2968 TraitItemConst, TraitItemFn, TraitItemMacro, TraitItemType, UseGlob, UseGroup, UseName,
2969 UsePath, UseRename, Variadic,
2970 };
2971 use crate::mac::MacroDelimiter;
2972 use crate::path;
2973 use crate::path::printing::PathStyle;
2974 use crate::print::TokensOrDefault;
2975 use crate::ty::Type;
2976 use proc_macro2::TokenStream;
2977 use quote::{ToTokens, TokenStreamExt as _};
2978
2979 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
2980 impl ToTokens for ItemExternCrate {
2981 fn to_tokens(&self, tokens: &mut TokenStream) {
2982 tokens.append_all(self.attrs.outer());
2983 self.vis.to_tokens(tokens);
2984 self.extern_token.to_tokens(tokens);
2985 self.crate_token.to_tokens(tokens);
2986 self.ident.to_tokens(tokens);
2987 if let Some((as_token, rename)) = &self.rename {
2988 as_token.to_tokens(tokens);
2989 rename.to_tokens(tokens);
2990 }
2991 self.semi_token.to_tokens(tokens);
2992 }
2993 }
2994
2995 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
2996 impl ToTokens for ItemUse {
2997 fn to_tokens(&self, tokens: &mut TokenStream) {
2998 tokens.append_all(self.attrs.outer());
2999 self.vis.to_tokens(tokens);
3000 self.use_token.to_tokens(tokens);
3001 self.leading_colon.to_tokens(tokens);
3002 self.tree.to_tokens(tokens);
3003 self.semi_token.to_tokens(tokens);
3004 }
3005 }
3006
3007 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3008 impl ToTokens for ItemStatic {
3009 fn to_tokens(&self, tokens: &mut TokenStream) {
3010 tokens.append_all(self.attrs.outer());
3011 self.vis.to_tokens(tokens);
3012 self.static_token.to_tokens(tokens);
3013 self.mutability.to_tokens(tokens);
3014 self.ident.to_tokens(tokens);
3015 self.colon_token.to_tokens(tokens);
3016 self.ty.to_tokens(tokens);
3017 self.eq_token.to_tokens(tokens);
3018 self.expr.to_tokens(tokens);
3019 self.semi_token.to_tokens(tokens);
3020 }
3021 }
3022
3023 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3024 impl ToTokens for ItemConst {
3025 fn to_tokens(&self, tokens: &mut TokenStream) {
3026 tokens.append_all(self.attrs.outer());
3027 self.vis.to_tokens(tokens);
3028 self.const_token.to_tokens(tokens);
3029 self.ident.to_tokens(tokens);
3030 self.colon_token.to_tokens(tokens);
3031 self.ty.to_tokens(tokens);
3032 self.eq_token.to_tokens(tokens);
3033 self.expr.to_tokens(tokens);
3034 self.semi_token.to_tokens(tokens);
3035 }
3036 }
3037
3038 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3039 impl ToTokens for ItemFn {
3040 fn to_tokens(&self, tokens: &mut TokenStream) {
3041 tokens.append_all(self.attrs.outer());
3042 self.vis.to_tokens(tokens);
3043 self.sig.to_tokens(tokens);
3044 self.block.brace_token.surround(tokens, |tokens| {
3045 tokens.append_all(self.attrs.inner());
3046 tokens.append_all(&self.block.stmts);
3047 });
3048 }
3049 }
3050
3051 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3052 impl ToTokens for ItemMod {
3053 fn to_tokens(&self, tokens: &mut TokenStream) {
3054 tokens.append_all(self.attrs.outer());
3055 self.vis.to_tokens(tokens);
3056 self.unsafety.to_tokens(tokens);
3057 self.mod_token.to_tokens(tokens);
3058 self.ident.to_tokens(tokens);
3059 if let Some((brace, items)) = &self.content {
3060 brace.surround(tokens, |tokens| {
3061 tokens.append_all(self.attrs.inner());
3062 tokens.append_all(items);
3063 });
3064 } else {
3065 TokensOrDefault(&self.semi).to_tokens(tokens);
3066 }
3067 }
3068 }
3069
3070 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3071 impl ToTokens for ItemForeignMod {
3072 fn to_tokens(&self, tokens: &mut TokenStream) {
3073 tokens.append_all(self.attrs.outer());
3074 self.unsafety.to_tokens(tokens);
3075 self.abi.to_tokens(tokens);
3076 self.brace_token.surround(tokens, |tokens| {
3077 tokens.append_all(self.attrs.inner());
3078 tokens.append_all(&self.items);
3079 });
3080 }
3081 }
3082
3083 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3084 impl ToTokens for ItemType {
3085 fn to_tokens(&self, tokens: &mut TokenStream) {
3086 tokens.append_all(self.attrs.outer());
3087 self.vis.to_tokens(tokens);
3088 self.type_token.to_tokens(tokens);
3089 self.ident.to_tokens(tokens);
3090 self.generics.to_tokens(tokens);
3091 self.generics.where_clause.to_tokens(tokens);
3092 self.eq_token.to_tokens(tokens);
3093 self.ty.to_tokens(tokens);
3094 self.semi_token.to_tokens(tokens);
3095 }
3096 }
3097
3098 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3099 impl ToTokens for ItemEnum {
3100 fn to_tokens(&self, tokens: &mut TokenStream) {
3101 tokens.append_all(self.attrs.outer());
3102 self.vis.to_tokens(tokens);
3103 self.enum_token.to_tokens(tokens);
3104 self.ident.to_tokens(tokens);
3105 self.generics.to_tokens(tokens);
3106 self.generics.where_clause.to_tokens(tokens);
3107 self.brace_token.surround(tokens, |tokens| {
3108 self.variants.to_tokens(tokens);
3109 });
3110 }
3111 }
3112
3113 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3114 impl ToTokens for ItemStruct {
3115 fn to_tokens(&self, tokens: &mut TokenStream) {
3116 tokens.append_all(self.attrs.outer());
3117 self.vis.to_tokens(tokens);
3118 self.struct_token.to_tokens(tokens);
3119 self.ident.to_tokens(tokens);
3120 self.generics.to_tokens(tokens);
3121 match &self.fields {
3122 Fields::Named(fields) => {
3123 self.generics.where_clause.to_tokens(tokens);
3124 fields.to_tokens(tokens);
3125 }
3126 Fields::Unnamed(fields) => {
3127 fields.to_tokens(tokens);
3128 self.generics.where_clause.to_tokens(tokens);
3129 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3130 }
3131 Fields::Unit => {
3132 self.generics.where_clause.to_tokens(tokens);
3133 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3134 }
3135 }
3136 }
3137 }
3138
3139 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3140 impl ToTokens for ItemUnion {
3141 fn to_tokens(&self, tokens: &mut TokenStream) {
3142 tokens.append_all(self.attrs.outer());
3143 self.vis.to_tokens(tokens);
3144 self.union_token.to_tokens(tokens);
3145 self.ident.to_tokens(tokens);
3146 self.generics.to_tokens(tokens);
3147 self.generics.where_clause.to_tokens(tokens);
3148 self.fields.to_tokens(tokens);
3149 }
3150 }
3151
3152 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3153 impl ToTokens for ItemTrait {
3154 fn to_tokens(&self, tokens: &mut TokenStream) {
3155 tokens.append_all(self.attrs.outer());
3156 self.vis.to_tokens(tokens);
3157 self.unsafety.to_tokens(tokens);
3158 self.auto_token.to_tokens(tokens);
3159 self.trait_token.to_tokens(tokens);
3160 self.ident.to_tokens(tokens);
3161 self.generics.to_tokens(tokens);
3162 if !self.supertraits.is_empty() {
3163 TokensOrDefault(&self.colon_token).to_tokens(tokens);
3164 self.supertraits.to_tokens(tokens);
3165 }
3166 self.generics.where_clause.to_tokens(tokens);
3167 self.brace_token.surround(tokens, |tokens| {
3168 tokens.append_all(self.attrs.inner());
3169 tokens.append_all(&self.items);
3170 });
3171 }
3172 }
3173
3174 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3175 impl ToTokens for ItemTraitAlias {
3176 fn to_tokens(&self, tokens: &mut TokenStream) {
3177 tokens.append_all(self.attrs.outer());
3178 self.vis.to_tokens(tokens);
3179 self.trait_token.to_tokens(tokens);
3180 self.ident.to_tokens(tokens);
3181 self.generics.to_tokens(tokens);
3182 self.eq_token.to_tokens(tokens);
3183 self.bounds.to_tokens(tokens);
3184 self.generics.where_clause.to_tokens(tokens);
3185 self.semi_token.to_tokens(tokens);
3186 }
3187 }
3188
3189 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3190 impl ToTokens for ItemImpl {
3191 fn to_tokens(&self, tokens: &mut TokenStream) {
3192 tokens.append_all(self.attrs.outer());
3193 self.defaultness.to_tokens(tokens);
3194 self.unsafety.to_tokens(tokens);
3195 self.impl_token.to_tokens(tokens);
3196 self.generics.to_tokens(tokens);
3197 if let Some((polarity, path, for_token)) = &self.trait_ {
3198 polarity.to_tokens(tokens);
3199 path.to_tokens(tokens);
3200 for_token.to_tokens(tokens);
3201 }
3202 self.self_ty.to_tokens(tokens);
3203 self.generics.where_clause.to_tokens(tokens);
3204 self.brace_token.surround(tokens, |tokens| {
3205 tokens.append_all(self.attrs.inner());
3206 tokens.append_all(&self.items);
3207 });
3208 }
3209 }
3210
3211 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3212 impl ToTokens for ItemMacro {
3213 fn to_tokens(&self, tokens: &mut TokenStream) {
3214 tokens.append_all(self.attrs.outer());
3215 path::printing::print_path(tokens, &self.mac.path, PathStyle::Mod);
3216 self.mac.bang_token.to_tokens(tokens);
3217 self.ident.to_tokens(tokens);
3218 match &self.mac.delimiter {
3219 MacroDelimiter::Paren(paren) => {
3220 paren.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3221 }
3222 MacroDelimiter::Brace(brace) => {
3223 brace.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3224 }
3225 MacroDelimiter::Bracket(bracket) => {
3226 bracket.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3227 }
3228 }
3229 self.semi_token.to_tokens(tokens);
3230 }
3231 }
3232
3233 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3234 impl ToTokens for UsePath {
3235 fn to_tokens(&self, tokens: &mut TokenStream) {
3236 self.ident.to_tokens(tokens);
3237 self.colon2_token.to_tokens(tokens);
3238 self.tree.to_tokens(tokens);
3239 }
3240 }
3241
3242 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3243 impl ToTokens for UseName {
3244 fn to_tokens(&self, tokens: &mut TokenStream) {
3245 self.ident.to_tokens(tokens);
3246 }
3247 }
3248
3249 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3250 impl ToTokens for UseRename {
3251 fn to_tokens(&self, tokens: &mut TokenStream) {
3252 self.ident.to_tokens(tokens);
3253 self.as_token.to_tokens(tokens);
3254 self.rename.to_tokens(tokens);
3255 }
3256 }
3257
3258 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3259 impl ToTokens for UseGlob {
3260 fn to_tokens(&self, tokens: &mut TokenStream) {
3261 self.star_token.to_tokens(tokens);
3262 }
3263 }
3264
3265 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3266 impl ToTokens for UseGroup {
3267 fn to_tokens(&self, tokens: &mut TokenStream) {
3268 self.brace_token.surround(tokens, |tokens| {
3269 self.items.to_tokens(tokens);
3270 });
3271 }
3272 }
3273
3274 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3275 impl ToTokens for TraitItemConst {
3276 fn to_tokens(&self, tokens: &mut TokenStream) {
3277 tokens.append_all(self.attrs.outer());
3278 self.const_token.to_tokens(tokens);
3279 self.ident.to_tokens(tokens);
3280 self.colon_token.to_tokens(tokens);
3281 self.ty.to_tokens(tokens);
3282 if let Some((eq_token, default)) = &self.default {
3283 eq_token.to_tokens(tokens);
3284 default.to_tokens(tokens);
3285 }
3286 self.semi_token.to_tokens(tokens);
3287 }
3288 }
3289
3290 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3291 impl ToTokens for TraitItemFn {
3292 fn to_tokens(&self, tokens: &mut TokenStream) {
3293 tokens.append_all(self.attrs.outer());
3294 self.sig.to_tokens(tokens);
3295 match &self.default {
3296 Some(block) => {
3297 block.brace_token.surround(tokens, |tokens| {
3298 tokens.append_all(self.attrs.inner());
3299 tokens.append_all(&block.stmts);
3300 });
3301 }
3302 None => {
3303 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3304 }
3305 }
3306 }
3307 }
3308
3309 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3310 impl ToTokens for TraitItemType {
3311 fn to_tokens(&self, tokens: &mut TokenStream) {
3312 tokens.append_all(self.attrs.outer());
3313 self.type_token.to_tokens(tokens);
3314 self.ident.to_tokens(tokens);
3315 self.generics.to_tokens(tokens);
3316 if !self.bounds.is_empty() {
3317 TokensOrDefault(&self.colon_token).to_tokens(tokens);
3318 self.bounds.to_tokens(tokens);
3319 }
3320 if let Some((eq_token, default)) = &self.default {
3321 eq_token.to_tokens(tokens);
3322 default.to_tokens(tokens);
3323 }
3324 self.generics.where_clause.to_tokens(tokens);
3325 self.semi_token.to_tokens(tokens);
3326 }
3327 }
3328
3329 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3330 impl ToTokens for TraitItemMacro {
3331 fn to_tokens(&self, tokens: &mut TokenStream) {
3332 tokens.append_all(self.attrs.outer());
3333 self.mac.to_tokens(tokens);
3334 self.semi_token.to_tokens(tokens);
3335 }
3336 }
3337
3338 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3339 impl ToTokens for ImplItemConst {
3340 fn to_tokens(&self, tokens: &mut TokenStream) {
3341 tokens.append_all(self.attrs.outer());
3342 self.vis.to_tokens(tokens);
3343 self.defaultness.to_tokens(tokens);
3344 self.const_token.to_tokens(tokens);
3345 self.ident.to_tokens(tokens);
3346 self.colon_token.to_tokens(tokens);
3347 self.ty.to_tokens(tokens);
3348 self.eq_token.to_tokens(tokens);
3349 self.expr.to_tokens(tokens);
3350 self.semi_token.to_tokens(tokens);
3351 }
3352 }
3353
3354 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3355 impl ToTokens for ImplItemFn {
3356 fn to_tokens(&self, tokens: &mut TokenStream) {
3357 tokens.append_all(self.attrs.outer());
3358 self.vis.to_tokens(tokens);
3359 self.defaultness.to_tokens(tokens);
3360 self.sig.to_tokens(tokens);
3361 self.block.brace_token.surround(tokens, |tokens| {
3362 tokens.append_all(self.attrs.inner());
3363 tokens.append_all(&self.block.stmts);
3364 });
3365 }
3366 }
3367
3368 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3369 impl ToTokens for ImplItemType {
3370 fn to_tokens(&self, tokens: &mut TokenStream) {
3371 tokens.append_all(self.attrs.outer());
3372 self.vis.to_tokens(tokens);
3373 self.defaultness.to_tokens(tokens);
3374 self.type_token.to_tokens(tokens);
3375 self.ident.to_tokens(tokens);
3376 self.generics.to_tokens(tokens);
3377 self.eq_token.to_tokens(tokens);
3378 self.ty.to_tokens(tokens);
3379 self.generics.where_clause.to_tokens(tokens);
3380 self.semi_token.to_tokens(tokens);
3381 }
3382 }
3383
3384 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3385 impl ToTokens for ImplItemMacro {
3386 fn to_tokens(&self, tokens: &mut TokenStream) {
3387 tokens.append_all(self.attrs.outer());
3388 self.mac.to_tokens(tokens);
3389 self.semi_token.to_tokens(tokens);
3390 }
3391 }
3392
3393 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3394 impl ToTokens for ForeignItemFn {
3395 fn to_tokens(&self, tokens: &mut TokenStream) {
3396 tokens.append_all(self.attrs.outer());
3397 self.vis.to_tokens(tokens);
3398 self.sig.to_tokens(tokens);
3399 self.semi_token.to_tokens(tokens);
3400 }
3401 }
3402
3403 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3404 impl ToTokens for ForeignItemStatic {
3405 fn to_tokens(&self, tokens: &mut TokenStream) {
3406 tokens.append_all(self.attrs.outer());
3407 self.vis.to_tokens(tokens);
3408 self.static_token.to_tokens(tokens);
3409 self.mutability.to_tokens(tokens);
3410 self.ident.to_tokens(tokens);
3411 self.colon_token.to_tokens(tokens);
3412 self.ty.to_tokens(tokens);
3413 self.semi_token.to_tokens(tokens);
3414 }
3415 }
3416
3417 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3418 impl ToTokens for ForeignItemType {
3419 fn to_tokens(&self, tokens: &mut TokenStream) {
3420 tokens.append_all(self.attrs.outer());
3421 self.vis.to_tokens(tokens);
3422 self.type_token.to_tokens(tokens);
3423 self.ident.to_tokens(tokens);
3424 self.generics.to_tokens(tokens);
3425 self.generics.where_clause.to_tokens(tokens);
3426 self.semi_token.to_tokens(tokens);
3427 }
3428 }
3429
3430 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3431 impl ToTokens for ForeignItemMacro {
3432 fn to_tokens(&self, tokens: &mut TokenStream) {
3433 tokens.append_all(self.attrs.outer());
3434 self.mac.to_tokens(tokens);
3435 self.semi_token.to_tokens(tokens);
3436 }
3437 }
3438
3439 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3440 impl ToTokens for Signature {
3441 fn to_tokens(&self, tokens: &mut TokenStream) {
3442 self.constness.to_tokens(tokens);
3443 self.asyncness.to_tokens(tokens);
3444 self.unsafety.to_tokens(tokens);
3445 self.abi.to_tokens(tokens);
3446 self.fn_token.to_tokens(tokens);
3447 self.ident.to_tokens(tokens);
3448 self.generics.to_tokens(tokens);
3449 self.paren_token.surround(tokens, |tokens| {
3450 self.inputs.to_tokens(tokens);
3451 if let Some(variadic) = &self.variadic {
3452 if !self.inputs.empty_or_trailing() {
3453 <crate::token::CommaToken![,]>::default().to_tokens(tokens);
3454 }
3455 variadic.to_tokens(tokens);
3456 }
3457 });
3458 self.output.to_tokens(tokens);
3459 self.generics.where_clause.to_tokens(tokens);
3460 }
3461 }
3462
3463 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3464 impl ToTokens for Receiver {
3465 fn to_tokens(&self, tokens: &mut TokenStream) {
3466 tokens.append_all(self.attrs.outer());
3467 if let Some((ampersand, lifetime)) = &self.reference {
3468 ampersand.to_tokens(tokens);
3469 lifetime.to_tokens(tokens);
3470 }
3471 self.mutability.to_tokens(tokens);
3472 self.self_token.to_tokens(tokens);
3473 if let Some(colon_token) = &self.colon_token {
3474 colon_token.to_tokens(tokens);
3475 self.ty.to_tokens(tokens);
3476 } else {
3477 let consistent = match (&self.reference, &self.mutability, &*self.ty) {
3478 (Some(_), mutability, Type::Reference(ty)) => {
3479 mutability.is_some() == ty.mutability.is_some()
3480 && match &*ty.elem {
3481 Type::Path(ty) => ty.qself.is_none() && ty.path.is_ident("Self"),
3482 _ => false,
3483 }
3484 }
3485 (None, _, Type::Path(ty)) => ty.qself.is_none() && ty.path.is_ident("Self"),
3486 _ => false,
3487 };
3488 if !consistent {
3489 <crate::token::ColonToken![:]>::default().to_tokens(tokens);
3490 self.ty.to_tokens(tokens);
3491 }
3492 }
3493 }
3494 }
3495
3496 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3497 impl ToTokens for Variadic {
3498 fn to_tokens(&self, tokens: &mut TokenStream) {
3499 tokens.append_all(self.attrs.outer());
3500 if let Some((pat, colon)) = &self.pat {
3501 pat.to_tokens(tokens);
3502 colon.to_tokens(tokens);
3503 }
3504 self.dots.to_tokens(tokens);
3505 self.comma.to_tokens(tokens);
3506 }
3507 }
3508
3509 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3510 impl ToTokens for StaticMutability {
3511 fn to_tokens(&self, tokens: &mut TokenStream) {
3512 match self {
3513 StaticMutability::None => {}
3514 StaticMutability::Mut(mut_token) => mut_token.to_tokens(tokens),
3515 }
3516 }
3517 }
3518}