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(mut receiver) = ahead.parse::<Receiver>() {
1611 input.advance_to(&ahead);
1612 receiver.attrs = attrs;
1613 return Ok(FnArgOrVariadic::FnArg(FnArg::Receiver(receiver)));
1614 }
1615
1616 if input.peek(Ident) && input.peek2(crate::token::LtToken![<]) {
1620 let span = input.span();
1621 return Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType {
1622 attrs,
1623 pat: Box::new(Pat::Wild(PatWild {
1624 attrs: Vec::new(),
1625 underscore_token: crate::token::UnderscoreToken,
1626 })),
1627 colon_token: crate::token::ColonToken,
1628 ty: input.parse()?,
1629 })));
1630 }
1631
1632 let pat = Box::new(Pat::parse_single(input)?);
1633 let colon_token: crate::token::ColonToken![:] = input.parse()?;
1634
1635 if allow_variadic {
1636 if let Some(dots) = input.parse::<Option<crate::token::DotDotDotToken![...]>>()? {
1637 return Ok(FnArgOrVariadic::Variadic(Variadic {
1638 attrs,
1639 pat: Some((pat, colon_token)),
1640 dots,
1641 comma: None,
1642 }));
1643 }
1644 }
1645
1646 Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType {
1647 attrs,
1648 pat,
1649 colon_token,
1650 ty: input.parse()?,
1651 })))
1652 }
1653
1654 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1655 impl Parse for Receiver {
1656 fn parse(input: ParseStream) -> Result<Self> {
1657 let reference = if input.peek(crate::token::AndToken![&]) {
1658 let ampersand: crate::token::AndToken![&] = input.parse()?;
1659 let lifetime: Option<Lifetime> = input.parse()?;
1660 Some((ampersand, lifetime))
1661 } else {
1662 None
1663 };
1664 let mutability: Option<crate::token::MutToken![mut]> = input.parse()?;
1665 let self_token: crate::token::SelfValueToken![self] = input.parse()?;
1666 let colon_token: Option<crate::token::ColonToken![:]> = if reference.is_some() {
1667 None
1668 } else {
1669 input.parse()?
1670 };
1671 let ty: Type = if colon_token.is_some() {
1672 input.parse()?
1673 } else {
1674 let mut ty = Type::Path(TypePath {
1675 qself: None,
1676 path: Path::from(Ident::new("Self", self_token.span)),
1677 });
1678 if let Some((ampersand, lifetime)) = reference.as_ref() {
1679 ty = Type::Reference(TypeReference {
1680 and_token: crate::token::AndToken,
1681 lifetime: lifetime.clone(),
1682 mutability: mutability.as_ref().map(|m| crate::token::MutToken),
1683 elem: Box::new(ty),
1684 });
1685 }
1686 ty
1687 };
1688 Ok(Receiver {
1689 attrs: Vec::new(),
1690 reference,
1691 mutability,
1692 self_token,
1693 colon_token,
1694 ty: Box::new(ty),
1695 })
1696 }
1697 }
1698
1699 fn parse_fn_args(
1700 input: ParseStream,
1701 ) -> Result<(Punctuated<FnArg, crate::token::CommaToken![,]>, Option<Variadic>)> {
1702 let mut args = Punctuated::new();
1703 let mut variadic = None;
1704 let mut has_receiver = false;
1705
1706 while !input.is_empty() {
1707 let attrs = input.call(Attribute::parse_outer)?;
1708
1709 if let Some(dots) = input.parse::<Option<crate::token::DotDotDotToken![...]>>()? {
1710 variadic = Some(Variadic {
1711 attrs,
1712 pat: None,
1713 dots,
1714 comma: if input.is_empty() {
1715 None
1716 } else {
1717 Some(input.parse()?)
1718 },
1719 });
1720 break;
1721 }
1722
1723 let allow_variadic = true;
1724 let arg = match parse_fn_arg_or_variadic(input, attrs, allow_variadic)? {
1725 FnArgOrVariadic::FnArg(arg) => arg,
1726 FnArgOrVariadic::Variadic(arg) => {
1727 variadic = Some(Variadic {
1728 comma: if input.is_empty() {
1729 None
1730 } else {
1731 Some(input.parse()?)
1732 },
1733 ..arg
1734 });
1735 break;
1736 }
1737 };
1738
1739 match &arg {
1740 FnArg::Receiver(receiver) if has_receiver => {
1741 return Err(Error::new(
1742 receiver.self_token.span,
1743 "unexpected second method receiver",
1744 ));
1745 }
1746 FnArg::Receiver(receiver) if !args.is_empty() => {
1747 return Err(Error::new(
1748 receiver.self_token.span,
1749 "unexpected method receiver",
1750 ));
1751 }
1752 FnArg::Receiver(_) => has_receiver = true,
1753 FnArg::Typed(_) => {}
1754 }
1755 args.push_value(arg);
1756
1757 if input.is_empty() {
1758 break;
1759 }
1760
1761 let comma: crate::token::CommaToken![,] = input.parse()?;
1762 args.push_punct(comma);
1763 }
1764
1765 Ok((args, variadic))
1766 }
1767
1768 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1769 impl Parse for ItemMod {
1770 fn parse(input: ParseStream) -> Result<Self> {
1771 let mut attrs = input.call(Attribute::parse_outer)?;
1772 let vis: Visibility = input.parse()?;
1773 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1774 let mod_token: crate::token::ModToken![mod] = input.parse()?;
1775 let ident: Ident = if input.peek(crate::token::TryToken![try]) {
1776 input.call(Ident::parse_any)
1777 } else {
1778 input.parse()
1779 }?;
1780
1781 let lookahead = input.lookahead1();
1782 if lookahead.peek(crate::token::SemiToken![;]) {
1783 Ok(ItemMod {
1784 attrs,
1785 vis,
1786 unsafety,
1787 mod_token,
1788 ident,
1789 content: None,
1790 semi: Some(input.parse()?),
1791 })
1792 } else if lookahead.peek(token::Brace) {
1793 let content;
1794 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);
1795 attr::parsing::parse_inner(&content, &mut attrs)?;
1796
1797 let mut items = Vec::new();
1798 while !content.is_empty() {
1799 items.push(content.parse()?);
1800 }
1801
1802 Ok(ItemMod {
1803 attrs,
1804 vis,
1805 unsafety,
1806 mod_token,
1807 ident,
1808 content: Some((brace_token, items)),
1809 semi: None,
1810 })
1811 } else {
1812 Err(lookahead.error())
1813 }
1814 }
1815 }
1816
1817 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1818 impl Parse for ItemForeignMod {
1819 fn parse(input: ParseStream) -> Result<Self> {
1820 let mut attrs = input.call(Attribute::parse_outer)?;
1821 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1822 let abi: Abi = input.parse()?;
1823
1824 let content;
1825 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);
1826 attr::parsing::parse_inner(&content, &mut attrs)?;
1827 let mut items = Vec::new();
1828 while !content.is_empty() {
1829 items.push(content.parse()?);
1830 }
1831
1832 Ok(ItemForeignMod {
1833 attrs,
1834 unsafety,
1835 abi,
1836 brace_token,
1837 items,
1838 })
1839 }
1840 }
1841
1842 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1843 impl Parse for ForeignItem {
1844 fn parse(input: ParseStream) -> Result<Self> {
1845 let begin = input.fork();
1846 let mut attrs = input.call(Attribute::parse_outer)?;
1847 let ahead = input.fork();
1848 let vis: Visibility = ahead.parse()?;
1849
1850 let lookahead = ahead.lookahead1();
1851 let allow_safe = true;
1852 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
1853 let vis: Visibility = input.parse()?;
1854 let sig = parse_signature(input, allow_safe)?;
1855 let has_safe = sig.is_none();
1856 let has_body = input.peek(token::Brace);
1857 let semi_token: Option<crate::token::SemiToken![;]> = if has_body {
1858 let content;
1859 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);
1860 content.call(Attribute::parse_inner)?;
1861 content.call(Block::parse_within)?;
1862 None
1863 } else {
1864 Some(input.parse()?)
1865 };
1866 if has_safe || has_body {
1867 Ok(ForeignItem::Verbatim(verbatim::between(&begin, input)))
1868 } else {
1869 Ok(ForeignItem::Fn(ForeignItemFn {
1870 attrs: Vec::new(),
1871 vis,
1872 sig: sig.unwrap(),
1873 semi_token: semi_token.unwrap(),
1874 }))
1875 }
1876 } else if lookahead.peek(crate::token::StaticToken![static])
1877 || ((ahead.peek(crate::token::UnsafeToken![unsafe])
1878 || token::parsing::peek_keyword(ahead.cursor(), "safe"))
1879 && ahead.peek2(crate::token::StaticToken![static]))
1880 {
1881 let vis = input.parse()?;
1882 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1883 let safe =
1884 unsafety.is_none() && token::parsing::peek_keyword(input.cursor(), "safe");
1885 if safe {
1886 token::parsing::keyword(input, "safe")?;
1887 }
1888 let static_token = input.parse()?;
1889 let mutability = input.parse()?;
1890 let ident = input.parse()?;
1891 let colon_token = input.parse()?;
1892 let ty = input.parse()?;
1893 let has_value = input.peek(crate::token::EqToken![=]);
1894 if has_value {
1895 input.parse::<crate::token::EqToken![=]>()?;
1896 input.parse::<Expr>()?;
1897 }
1898 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1899 if unsafety.is_some() || safe || has_value {
1900 Ok(ForeignItem::Verbatim(verbatim::between(&begin, input)))
1901 } else {
1902 Ok(ForeignItem::Static(ForeignItemStatic {
1903 attrs: Vec::new(),
1904 vis,
1905 static_token,
1906 mutability,
1907 ident,
1908 colon_token,
1909 ty,
1910 semi_token,
1911 }))
1912 }
1913 } else if lookahead.peek(crate::token::TypeToken![type]) {
1914 parse_foreign_item_type(begin, input)
1915 } else if vis.is_inherited()
1916 && (lookahead.peek(Ident)
1917 || lookahead.peek(crate::token::SelfValueToken![self])
1918 || lookahead.peek(crate::token::SuperToken![super])
1919 || lookahead.peek(crate::token::CrateToken![crate])
1920 || lookahead.peek(crate::token::PathSepToken![::]))
1921 {
1922 input.parse().map(ForeignItem::Macro)
1923 } else {
1924 Err(lookahead.error())
1925 }?;
1926
1927 let item_attrs = match &mut item {
1928 ForeignItem::Fn(item) => &mut item.attrs,
1929 ForeignItem::Static(item) => &mut item.attrs,
1930 ForeignItem::Type(item) => &mut item.attrs,
1931 ForeignItem::Macro(item) => &mut item.attrs,
1932 ForeignItem::Verbatim(_) => return Ok(item),
1933 };
1934 attrs.append(item_attrs);
1935 *item_attrs = attrs;
1936
1937 Ok(item)
1938 }
1939 }
1940
1941 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1942 impl Parse for ForeignItemFn {
1943 fn parse(input: ParseStream) -> Result<Self> {
1944 let attrs = input.call(Attribute::parse_outer)?;
1945 let vis: Visibility = input.parse()?;
1946 let sig: Signature = input.parse()?;
1947 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1948 Ok(ForeignItemFn {
1949 attrs,
1950 vis,
1951 sig,
1952 semi_token,
1953 })
1954 }
1955 }
1956
1957 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1958 impl Parse for ForeignItemStatic {
1959 fn parse(input: ParseStream) -> Result<Self> {
1960 Ok(ForeignItemStatic {
1961 attrs: input.call(Attribute::parse_outer)?,
1962 vis: input.parse()?,
1963 static_token: input.parse()?,
1964 mutability: input.parse()?,
1965 ident: input.parse()?,
1966 colon_token: input.parse()?,
1967 ty: input.parse()?,
1968 semi_token: input.parse()?,
1969 })
1970 }
1971 }
1972
1973 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1974 impl Parse for ForeignItemType {
1975 fn parse(input: ParseStream) -> Result<Self> {
1976 Ok(ForeignItemType {
1977 attrs: input.call(Attribute::parse_outer)?,
1978 vis: input.parse()?,
1979 type_token: input.parse()?,
1980 ident: input.parse()?,
1981 generics: {
1982 let mut generics: Generics = input.parse()?;
1983 generics.where_clause = input.parse()?;
1984 generics
1985 },
1986 semi_token: input.parse()?,
1987 })
1988 }
1989 }
1990
1991 fn parse_foreign_item_type(begin: ParseBuffer, input: ParseStream) -> Result<ForeignItem> {
1992 let FlexibleItemType {
1993 vis,
1994 defaultness: _,
1995 type_token,
1996 ident,
1997 generics,
1998 colon_token,
1999 bounds: _,
2000 ty,
2001 semi_token,
2002 } = FlexibleItemType::parse(
2003 input,
2004 TypeDefaultness::Disallowed,
2005 WhereClauseLocation::Both,
2006 )?;
2007
2008 if colon_token.is_some() || ty.is_some() {
2009 Ok(ForeignItem::Verbatim(verbatim::between(&begin, input)))
2010 } else {
2011 Ok(ForeignItem::Type(ForeignItemType {
2012 attrs: Vec::new(),
2013 vis,
2014 type_token,
2015 ident,
2016 generics,
2017 semi_token,
2018 }))
2019 }
2020 }
2021
2022 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2023 impl Parse for ForeignItemMacro {
2024 fn parse(input: ParseStream) -> Result<Self> {
2025 let attrs = input.call(Attribute::parse_outer)?;
2026 let mac: Macro = input.parse()?;
2027 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2028 None
2029 } else {
2030 Some(input.parse()?)
2031 };
2032 Ok(ForeignItemMacro {
2033 attrs,
2034 mac,
2035 semi_token,
2036 })
2037 }
2038 }
2039
2040 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2041 impl Parse for ItemType {
2042 fn parse(input: ParseStream) -> Result<Self> {
2043 Ok(ItemType {
2044 attrs: input.call(Attribute::parse_outer)?,
2045 vis: input.parse()?,
2046 type_token: input.parse()?,
2047 ident: input.parse()?,
2048 generics: {
2049 let mut generics: Generics = input.parse()?;
2050 generics.where_clause = input.parse()?;
2051 generics
2052 },
2053 eq_token: input.parse()?,
2054 ty: input.parse()?,
2055 semi_token: input.parse()?,
2056 })
2057 }
2058 }
2059
2060 fn parse_item_type(begin: ParseBuffer, input: ParseStream) -> Result<Item> {
2061 let FlexibleItemType {
2062 vis,
2063 defaultness: _,
2064 type_token,
2065 ident,
2066 generics,
2067 colon_token,
2068 bounds: _,
2069 ty,
2070 semi_token,
2071 } = FlexibleItemType::parse(
2072 input,
2073 TypeDefaultness::Disallowed,
2074 WhereClauseLocation::BeforeEq,
2075 )?;
2076
2077 let (eq_token, ty) = match ty {
2078 Some(ty) if colon_token.is_none() => ty,
2079 _ => return Ok(Item::Verbatim(verbatim::between(&begin, input))),
2080 };
2081
2082 Ok(Item::Type(ItemType {
2083 attrs: Vec::new(),
2084 vis,
2085 type_token,
2086 ident,
2087 generics,
2088 eq_token,
2089 ty: Box::new(ty),
2090 semi_token,
2091 }))
2092 }
2093
2094 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2095 impl Parse for ItemStruct {
2096 fn parse(input: ParseStream) -> Result<Self> {
2097 let attrs = input.call(Attribute::parse_outer)?;
2098 let vis = input.parse::<Visibility>()?;
2099 let struct_token = input.parse::<crate::token::StructToken![struct]>()?;
2100 let ident = input.parse::<Ident>()?;
2101 let generics = input.parse::<Generics>()?;
2102 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
2103 Ok(ItemStruct {
2104 attrs,
2105 vis,
2106 struct_token,
2107 ident,
2108 generics: Generics {
2109 where_clause,
2110 ..generics
2111 },
2112 fields,
2113 semi_token,
2114 })
2115 }
2116 }
2117
2118 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2119 impl Parse for ItemEnum {
2120 fn parse(input: ParseStream) -> Result<Self> {
2121 let attrs = input.call(Attribute::parse_outer)?;
2122 let vis = input.parse::<Visibility>()?;
2123 let enum_token = input.parse::<crate::token::EnumToken![enum]>()?;
2124 let ident = input.parse::<Ident>()?;
2125 let generics = input.parse::<Generics>()?;
2126 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
2127 Ok(ItemEnum {
2128 attrs,
2129 vis,
2130 enum_token,
2131 ident,
2132 generics: Generics {
2133 where_clause,
2134 ..generics
2135 },
2136 brace_token,
2137 variants,
2138 })
2139 }
2140 }
2141
2142 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2143 impl Parse for ItemUnion {
2144 fn parse(input: ParseStream) -> Result<Self> {
2145 let attrs = input.call(Attribute::parse_outer)?;
2146 let vis = input.parse::<Visibility>()?;
2147 let union_token = input.parse::<crate::token::UnionToken![union]>()?;
2148 let ident = input.parse::<Ident>()?;
2149 let generics = input.parse::<Generics>()?;
2150 let (where_clause, fields) = derive::parsing::data_union(input)?;
2151 Ok(ItemUnion {
2152 attrs,
2153 vis,
2154 union_token,
2155 ident,
2156 generics: Generics {
2157 where_clause,
2158 ..generics
2159 },
2160 fields,
2161 })
2162 }
2163 }
2164
2165 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
2166 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
2167 let lookahead = input.lookahead1();
2168 if lookahead.peek(token::Brace)
2169 || lookahead.peek(crate::token::ColonToken![:])
2170 || lookahead.peek(crate::token::WhereToken![where])
2171 {
2172 let unsafety = None;
2173 let auto_token = None;
2174 parse_rest_of_trait(
2175 input,
2176 attrs,
2177 vis,
2178 unsafety,
2179 auto_token,
2180 trait_token,
2181 ident,
2182 generics,
2183 )
2184 .map(Item::Trait)
2185 } else if lookahead.peek(crate::token::EqToken![=]) {
2186 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
2187 .map(Item::TraitAlias)
2188 } else {
2189 Err(lookahead.error())
2190 }
2191 }
2192
2193 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2194 impl Parse for ItemTrait {
2195 fn parse(input: ParseStream) -> Result<Self> {
2196 let outer_attrs = input.call(Attribute::parse_outer)?;
2197 let vis: Visibility = input.parse()?;
2198 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
2199 let auto_token: Option<crate::token::AutoToken![auto]> = input.parse()?;
2200 let trait_token: crate::token::TraitToken![trait] = input.parse()?;
2201 let ident: Ident = input.parse()?;
2202 let generics: Generics = input.parse()?;
2203 parse_rest_of_trait(
2204 input,
2205 outer_attrs,
2206 vis,
2207 unsafety,
2208 auto_token,
2209 trait_token,
2210 ident,
2211 generics,
2212 )
2213 }
2214 }
2215
2216 fn parse_rest_of_trait(
2217 input: ParseStream,
2218 mut attrs: Vec<Attribute>,
2219 vis: Visibility,
2220 unsafety: Option<crate::token::UnsafeToken![unsafe]>,
2221 auto_token: Option<crate::token::AutoToken![auto]>,
2222 trait_token: crate::token::TraitToken![trait],
2223 ident: Ident,
2224 mut generics: Generics,
2225 ) -> Result<ItemTrait> {
2226 let colon_token: Option<crate::token::ColonToken![:]> = input.parse()?;
2227
2228 let mut supertraits = Punctuated::new();
2229 if colon_token.is_some() {
2230 loop {
2231 if input.peek(crate::token::WhereToken![where]) || input.peek(token::Brace) {
2232 break;
2233 }
2234 supertraits.push_value({
2235 let allow_precise_capture = false;
2236 let allow_const = true;
2237 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
2238 });
2239 if input.peek(crate::token::WhereToken![where]) || input.peek(token::Brace) {
2240 break;
2241 }
2242 supertraits.push_punct(input.parse()?);
2243 }
2244 }
2245
2246 generics.where_clause = input.parse()?;
2247
2248 let content;
2249 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);
2250 attr::parsing::parse_inner(&content, &mut attrs)?;
2251 let mut items = Vec::new();
2252 while !content.is_empty() {
2253 items.push(content.parse()?);
2254 }
2255
2256 Ok(ItemTrait {
2257 attrs,
2258 vis,
2259 unsafety,
2260 auto_token,
2261 restriction: None,
2262 trait_token,
2263 ident,
2264 generics,
2265 colon_token,
2266 supertraits,
2267 brace_token,
2268 items,
2269 })
2270 }
2271
2272 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2273 impl Parse for ItemTraitAlias {
2274 fn parse(input: ParseStream) -> Result<Self> {
2275 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
2276 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
2277 }
2278 }
2279
2280 fn parse_start_of_trait_alias(
2281 input: ParseStream,
2282 ) -> Result<(Vec<Attribute>, Visibility, crate::token::TraitToken![trait], Ident, Generics)> {
2283 let attrs = input.call(Attribute::parse_outer)?;
2284 let vis: Visibility = input.parse()?;
2285 let trait_token: crate::token::TraitToken![trait] = input.parse()?;
2286 let ident: Ident = input.parse()?;
2287 let generics: Generics = input.parse()?;
2288 Ok((attrs, vis, trait_token, ident, generics))
2289 }
2290
2291 fn parse_rest_of_trait_alias(
2292 input: ParseStream,
2293 attrs: Vec<Attribute>,
2294 vis: Visibility,
2295 trait_token: crate::token::TraitToken![trait],
2296 ident: Ident,
2297 mut generics: Generics,
2298 ) -> Result<ItemTraitAlias> {
2299 let eq_token: crate::token::EqToken![=] = input.parse()?;
2300
2301 let mut bounds = Punctuated::new();
2302 loop {
2303 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::SemiToken![;]) {
2304 break;
2305 }
2306 bounds.push_value({
2307 let allow_precise_capture = false;
2308 let allow_const = false;
2309 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
2310 });
2311 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::SemiToken![;]) {
2312 break;
2313 }
2314 bounds.push_punct(input.parse()?);
2315 }
2316
2317 generics.where_clause = input.parse()?;
2318 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2319
2320 Ok(ItemTraitAlias {
2321 attrs,
2322 vis,
2323 trait_token,
2324 ident,
2325 generics,
2326 eq_token,
2327 bounds,
2328 semi_token,
2329 })
2330 }
2331
2332 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2333 impl Parse for TraitItem {
2334 fn parse(input: ParseStream) -> Result<Self> {
2335 let begin = input.fork();
2336 let mut attrs = input.call(Attribute::parse_outer)?;
2337 let vis: Visibility = input.parse()?;
2338 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2339 let ahead = input.fork();
2340
2341 let lookahead = ahead.lookahead1();
2342 let allow_safe = false;
2343 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
2344 input.parse().map(TraitItem::Fn)
2345 } else if lookahead.peek(crate::token::ConstToken![const]) {
2346 let const_token: crate::token::ConstToken![const] = ahead.parse()?;
2347 let lookahead = ahead.lookahead1();
2348 if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2349 input.advance_to(&ahead);
2350 let ident = input.call(Ident::parse_any)?;
2351 let mut generics: Generics = input.parse()?;
2352 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2353 let ty: Type = input.parse()?;
2354 let default = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
2355 let expr: Expr = input.parse()?;
2356 Some((eq_token, expr))
2357 } else {
2358 None
2359 };
2360 generics.where_clause = input.parse()?;
2361 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2362 if generics.lt_token.is_none() && generics.where_clause.is_none() {
2363 Ok(TraitItem::Const(TraitItemConst {
2364 attrs: Vec::new(),
2365 const_token,
2366 ident,
2367 generics,
2368 colon_token,
2369 ty,
2370 default,
2371 semi_token,
2372 }))
2373 } else {
2374 return Ok(TraitItem::Verbatim(verbatim::between(&begin, input)));
2375 }
2376 } else if lookahead.peek(crate::token::AsyncToken![async])
2377 || lookahead.peek(crate::token::UnsafeToken![unsafe])
2378 || lookahead.peek(crate::token::ExternToken![extern])
2379 || lookahead.peek(crate::token::FnToken![fn])
2380 {
2381 input.parse().map(TraitItem::Fn)
2382 } else {
2383 Err(lookahead.error())
2384 }
2385 } else if lookahead.peek(crate::token::TypeToken![type]) {
2386 parse_trait_item_type(begin.fork(), input)
2387 } else if vis.is_inherited()
2388 && defaultness.is_none()
2389 && (lookahead.peek(Ident)
2390 || lookahead.peek(crate::token::SelfValueToken![self])
2391 || lookahead.peek(crate::token::SuperToken![super])
2392 || lookahead.peek(crate::token::CrateToken![crate])
2393 || lookahead.peek(crate::token::PathSepToken![::]))
2394 {
2395 input.parse().map(TraitItem::Macro)
2396 } else {
2397 Err(lookahead.error())
2398 }?;
2399
2400 match (vis, defaultness) {
2401 (Visibility::Inherited, None) => {}
2402 _ => return Ok(TraitItem::Verbatim(verbatim::between(&begin, input))),
2403 }
2404
2405 let item_attrs = match &mut item {
2406 TraitItem::Const(item) => &mut item.attrs,
2407 TraitItem::Fn(item) => &mut item.attrs,
2408 TraitItem::Type(item) => &mut item.attrs,
2409 TraitItem::Macro(item) => &mut item.attrs,
2410 TraitItem::Verbatim(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
2411 };
2412 attrs.append(item_attrs);
2413 *item_attrs = attrs;
2414 Ok(item)
2415 }
2416 }
2417
2418 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2419 impl Parse for TraitItemConst {
2420 fn parse(input: ParseStream) -> Result<Self> {
2421 let attrs = input.call(Attribute::parse_outer)?;
2422 let const_token: crate::token::ConstToken![const] = input.parse()?;
2423
2424 let lookahead = input.lookahead1();
2425 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2426 input.call(Ident::parse_any)?
2427 } else {
2428 return Err(lookahead.error());
2429 };
2430
2431 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2432 let ty: Type = input.parse()?;
2433 let default = if input.peek(crate::token::EqToken![=]) {
2434 let eq_token: crate::token::EqToken![=] = input.parse()?;
2435 let default: Expr = input.parse()?;
2436 Some((eq_token, default))
2437 } else {
2438 None
2439 };
2440 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2441
2442 Ok(TraitItemConst {
2443 attrs,
2444 const_token,
2445 ident,
2446 generics: Generics::default(),
2447 colon_token,
2448 ty,
2449 default,
2450 semi_token,
2451 })
2452 }
2453 }
2454
2455 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2456 impl Parse for TraitItemFn {
2457 fn parse(input: ParseStream) -> Result<Self> {
2458 let mut attrs = input.call(Attribute::parse_outer)?;
2459 let sig: Signature = input.parse()?;
2460
2461 let lookahead = input.lookahead1();
2462 let (brace_token, stmts, semi_token) = if lookahead.peek(token::Brace) {
2463 let content;
2464 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);
2465 attr::parsing::parse_inner(&content, &mut attrs)?;
2466 let stmts = content.call(Block::parse_within)?;
2467 (Some(brace_token), stmts, None)
2468 } else if lookahead.peek(crate::token::SemiToken![;]) {
2469 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2470 (None, Vec::new(), Some(semi_token))
2471 } else {
2472 return Err(lookahead.error());
2473 };
2474
2475 Ok(TraitItemFn {
2476 attrs,
2477 sig,
2478 default: brace_token.map(|brace_token| Block { brace_token, stmts }),
2479 semi_token,
2480 })
2481 }
2482 }
2483
2484 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2485 impl Parse for TraitItemType {
2486 fn parse(input: ParseStream) -> Result<Self> {
2487 let attrs = input.call(Attribute::parse_outer)?;
2488 let type_token: crate::token::TypeToken![type] = input.parse()?;
2489 let ident: Ident = input.parse()?;
2490 let mut generics: Generics = input.parse()?;
2491 let (colon_token, bounds) = FlexibleItemType::parse_optional_bounds(input)?;
2492 let default = FlexibleItemType::parse_optional_definition(input)?;
2493 generics.where_clause = input.parse()?;
2494 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2495 Ok(TraitItemType {
2496 attrs,
2497 type_token,
2498 ident,
2499 generics,
2500 colon_token,
2501 bounds,
2502 default,
2503 semi_token,
2504 })
2505 }
2506 }
2507
2508 fn parse_trait_item_type(begin: ParseBuffer, input: ParseStream) -> Result<TraitItem> {
2509 let FlexibleItemType {
2510 vis,
2511 defaultness: _,
2512 type_token,
2513 ident,
2514 generics,
2515 colon_token,
2516 bounds,
2517 ty,
2518 semi_token,
2519 } = FlexibleItemType::parse(
2520 input,
2521 TypeDefaultness::Disallowed,
2522 WhereClauseLocation::AfterEq,
2523 )?;
2524
2525 if vis.is_some() {
2526 Ok(TraitItem::Verbatim(verbatim::between(&begin, input)))
2527 } else {
2528 Ok(TraitItem::Type(TraitItemType {
2529 attrs: Vec::new(),
2530 type_token,
2531 ident,
2532 generics,
2533 colon_token,
2534 bounds,
2535 default: ty,
2536 semi_token,
2537 }))
2538 }
2539 }
2540
2541 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2542 impl Parse for TraitItemMacro {
2543 fn parse(input: ParseStream) -> Result<Self> {
2544 let attrs = input.call(Attribute::parse_outer)?;
2545 let mac: Macro = input.parse()?;
2546 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2547 None
2548 } else {
2549 Some(input.parse()?)
2550 };
2551 Ok(TraitItemMacro {
2552 attrs,
2553 mac,
2554 semi_token,
2555 })
2556 }
2557 }
2558
2559 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2560 impl Parse for ItemImpl {
2561 fn parse(input: ParseStream) -> Result<Self> {
2562 let allow_verbatim_impl = false;
2563 parse_impl(input, allow_verbatim_impl).map(Option::unwrap)
2564 }
2565 }
2566
2567 fn parse_impl(input: ParseStream, allow_verbatim_impl: bool) -> Result<Option<ItemImpl>> {
2568 let mut attrs = input.call(Attribute::parse_outer)?;
2569 let has_visibility = allow_verbatim_impl && input.parse::<Visibility>()?.is_some();
2570 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2571 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
2572 let impl_token: crate::token::ImplToken![impl] = input.parse()?;
2573
2574 let has_generics = generics::parsing::choose_generics_over_qpath(input);
2575 let mut generics: Generics = if has_generics {
2576 input.parse()?
2577 } else {
2578 Generics::default()
2579 };
2580
2581 let is_const_impl = allow_verbatim_impl
2582 && (input.peek(crate::token::ConstToken![const]) || input.peek(crate::token::QuestionToken![?]) && input.peek2(crate::token::ConstToken![const]));
2583 if is_const_impl {
2584 input.parse::<Option<crate::token::QuestionToken![?]>>()?;
2585 input.parse::<crate::token::ConstToken![const]>()?;
2586 }
2587
2588 let polarity = if input.peek(crate::token::NotToken![!]) && !input.peek2(token::Brace) {
2589 Some(input.parse::<crate::token::NotToken![!]>()?)
2590 } else {
2591 None
2592 };
2593
2594 #[cfg(not(feature = "printing"))]
2595 let first_ty_span = input.span();
2596 let mut first_ty: Type = input.parse()?;
2597 let self_ty: Type;
2598 let trait_;
2599
2600 let is_impl_for = input.peek(crate::token::ForToken![for]);
2601 if is_impl_for {
2602 let for_token: crate::token::ForToken![for] = input.parse()?;
2603 let mut first_ty_ref = &first_ty;
2604 while let Type::Group(ty) = first_ty_ref {
2605 first_ty_ref = &ty.elem;
2606 }
2607 if let Type::Path(TypePath { qself: None, .. }) = first_ty_ref {
2608 while let Type::Group(ty) = first_ty {
2609 first_ty = *ty.elem;
2610 }
2611 if let Type::Path(TypePath { qself: None, path }) = first_ty {
2612 trait_ = Some((polarity, path, for_token));
2613 } else {
2614 ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
2615 }
2616 } else if !allow_verbatim_impl {
2617 #[cfg(feature = "printing")]
2618 return Err(Error::new_spanned(first_ty_ref, "expected trait path"));
2619 #[cfg(not(feature = "printing"))]
2620 return Err(Error::new(first_ty_span, "expected trait path"));
2621 } else {
2622 trait_ = None;
2623 }
2624 self_ty = input.parse()?;
2625 } else if let Some(polarity) = polarity {
2626 return Err(Error::new(
2627 polarity.span,
2628 "inherent impls cannot be negative",
2629 ));
2630 } else {
2631 trait_ = None;
2632 self_ty = first_ty;
2633 }
2634
2635 generics.where_clause = input.parse()?;
2636
2637 let content;
2638 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);
2639 attr::parsing::parse_inner(&content, &mut attrs)?;
2640
2641 let mut items = Vec::new();
2642 while !content.is_empty() {
2643 items.push(content.parse()?);
2644 }
2645
2646 if has_visibility || is_const_impl || is_impl_for && trait_.is_none() {
2647 Ok(None)
2648 } else {
2649 Ok(Some(ItemImpl {
2650 attrs,
2651 defaultness,
2652 unsafety,
2653 impl_token,
2654 generics,
2655 trait_,
2656 self_ty: Box::new(self_ty),
2657 brace_token,
2658 items,
2659 }))
2660 }
2661 }
2662
2663 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2664 impl Parse for ImplItem {
2665 fn parse(input: ParseStream) -> Result<Self> {
2666 let begin = input.fork();
2667 let mut attrs = input.call(Attribute::parse_outer)?;
2668 let ahead = input.fork();
2669 let vis: Visibility = ahead.parse()?;
2670
2671 let mut lookahead = ahead.lookahead1();
2672 let defaultness = if lookahead.peek(crate::token::DefaultToken![default]) && !ahead.peek2(crate::token::NotToken![!]) {
2673 let defaultness: crate::token::DefaultToken![default] = ahead.parse()?;
2674 lookahead = ahead.lookahead1();
2675 Some(defaultness)
2676 } else {
2677 None
2678 };
2679
2680 let allow_safe = false;
2681 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
2682 let allow_omitted_body = true;
2683 if let Some(item) = parse_impl_item_fn(input, allow_omitted_body)? {
2684 Ok(ImplItem::Fn(item))
2685 } else {
2686 Ok(ImplItem::Verbatim(verbatim::between(&begin, input)))
2687 }
2688 } else if lookahead.peek(crate::token::ConstToken![const]) {
2689 input.advance_to(&ahead);
2690 let const_token: crate::token::ConstToken![const] = input.parse()?;
2691 let lookahead = input.lookahead1();
2692 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2693 input.call(Ident::parse_any)?
2694 } else {
2695 return Err(lookahead.error());
2696 };
2697 let mut generics: Generics = input.parse()?;
2698 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2699 let ty: Type = input.parse()?;
2700 let value = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
2701 let expr: Expr = input.parse()?;
2702 Some((eq_token, expr))
2703 } else {
2704 None
2705 };
2706 generics.where_clause = input.parse()?;
2707 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2708 return match value {
2709 Some((eq_token, expr))
2710 if generics.lt_token.is_none() && generics.where_clause.is_none() =>
2711 {
2712 Ok(ImplItem::Const(ImplItemConst {
2713 attrs,
2714 vis,
2715 defaultness,
2716 const_token,
2717 ident,
2718 generics,
2719 colon_token,
2720 ty,
2721 eq_token,
2722 expr,
2723 semi_token,
2724 }))
2725 }
2726 _ => Ok(ImplItem::Verbatim(verbatim::between(&begin, input))),
2727 };
2728 } else if lookahead.peek(crate::token::TypeToken![type]) {
2729 parse_impl_item_type(begin, input)
2730 } else if vis.is_inherited()
2731 && defaultness.is_none()
2732 && (lookahead.peek(Ident)
2733 || lookahead.peek(crate::token::SelfValueToken![self])
2734 || lookahead.peek(crate::token::SuperToken![super])
2735 || lookahead.peek(crate::token::CrateToken![crate])
2736 || lookahead.peek(crate::token::PathSepToken![::]))
2737 {
2738 input.parse().map(ImplItem::Macro)
2739 } else {
2740 Err(lookahead.error())
2741 }?;
2742
2743 {
2744 let item_attrs = match &mut item {
2745 ImplItem::Const(item) => &mut item.attrs,
2746 ImplItem::Fn(item) => &mut item.attrs,
2747 ImplItem::Type(item) => &mut item.attrs,
2748 ImplItem::Macro(item) => &mut item.attrs,
2749 ImplItem::Verbatim(_) => return Ok(item),
2750 };
2751 attrs.append(item_attrs);
2752 *item_attrs = attrs;
2753 }
2754
2755 Ok(item)
2756 }
2757 }
2758
2759 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2760 impl Parse for ImplItemConst {
2761 fn parse(input: ParseStream) -> Result<Self> {
2762 let attrs = input.call(Attribute::parse_outer)?;
2763 let vis: Visibility = input.parse()?;
2764 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2765 let const_token: crate::token::ConstToken![const] = input.parse()?;
2766
2767 let lookahead = input.lookahead1();
2768 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2769 input.call(Ident::parse_any)?
2770 } else {
2771 return Err(lookahead.error());
2772 };
2773
2774 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2775 let ty: Type = input.parse()?;
2776 let eq_token: crate::token::EqToken![=] = input.parse()?;
2777 let expr: Expr = input.parse()?;
2778 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2779
2780 Ok(ImplItemConst {
2781 attrs,
2782 vis,
2783 defaultness,
2784 const_token,
2785 ident,
2786 generics: Generics::default(),
2787 colon_token,
2788 ty,
2789 eq_token,
2790 expr,
2791 semi_token,
2792 })
2793 }
2794 }
2795
2796 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2797 impl Parse for ImplItemFn {
2798 fn parse(input: ParseStream) -> Result<Self> {
2799 let allow_omitted_body = false;
2800 parse_impl_item_fn(input, allow_omitted_body).map(Option::unwrap)
2801 }
2802 }
2803
2804 fn parse_impl_item_fn(
2805 input: ParseStream,
2806 allow_omitted_body: bool,
2807 ) -> Result<Option<ImplItemFn>> {
2808 let mut attrs = input.call(Attribute::parse_outer)?;
2809 let vis: Visibility = input.parse()?;
2810 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2811 let sig: Signature = input.parse()?;
2812
2813 if allow_omitted_body && input.parse::<Option<crate::token::SemiToken![;]>>()?.is_some() {
2817 return Ok(None);
2818 }
2819
2820 let content;
2821 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);
2822 attrs.extend(content.call(Attribute::parse_inner)?);
2823 let block = Block {
2824 brace_token,
2825 stmts: content.call(Block::parse_within)?,
2826 };
2827
2828 Ok(Some(ImplItemFn {
2829 attrs,
2830 vis,
2831 defaultness,
2832 sig,
2833 block,
2834 }))
2835 }
2836
2837 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2838 impl Parse for ImplItemType {
2839 fn parse(input: ParseStream) -> Result<Self> {
2840 let attrs = input.call(Attribute::parse_outer)?;
2841 let vis: Visibility = input.parse()?;
2842 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2843 let type_token: crate::token::TypeToken![type] = input.parse()?;
2844 let ident: Ident = input.parse()?;
2845 let mut generics: Generics = input.parse()?;
2846 let eq_token: crate::token::EqToken![=] = input.parse()?;
2847 let ty: Type = input.parse()?;
2848 generics.where_clause = input.parse()?;
2849 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2850 Ok(ImplItemType {
2851 attrs,
2852 vis,
2853 defaultness,
2854 type_token,
2855 ident,
2856 generics,
2857 eq_token,
2858 ty,
2859 semi_token,
2860 })
2861 }
2862 }
2863
2864 fn parse_impl_item_type(begin: ParseBuffer, input: ParseStream) -> Result<ImplItem> {
2865 let FlexibleItemType {
2866 vis,
2867 defaultness,
2868 type_token,
2869 ident,
2870 generics,
2871 colon_token,
2872 bounds: _,
2873 ty,
2874 semi_token,
2875 } = FlexibleItemType::parse(
2876 input,
2877 TypeDefaultness::Optional,
2878 WhereClauseLocation::AfterEq,
2879 )?;
2880
2881 let (eq_token, ty) = match ty {
2882 Some(ty) if colon_token.is_none() => ty,
2883 _ => return Ok(ImplItem::Verbatim(verbatim::between(&begin, input))),
2884 };
2885
2886 Ok(ImplItem::Type(ImplItemType {
2887 attrs: Vec::new(),
2888 vis,
2889 defaultness,
2890 type_token,
2891 ident,
2892 generics,
2893 eq_token,
2894 ty,
2895 semi_token,
2896 }))
2897 }
2898
2899 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2900 impl Parse for ImplItemMacro {
2901 fn parse(input: ParseStream) -> Result<Self> {
2902 let attrs = input.call(Attribute::parse_outer)?;
2903 let mac: Macro = input.parse()?;
2904 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2905 None
2906 } else {
2907 Some(input.parse()?)
2908 };
2909 Ok(ImplItemMacro {
2910 attrs,
2911 mac,
2912 semi_token,
2913 })
2914 }
2915 }
2916
2917 impl Visibility {
2918 fn is_inherited(&self) -> bool {
2919 match self {
2920 Visibility::Inherited => true,
2921 _ => false,
2922 }
2923 }
2924 }
2925
2926 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2927 impl Parse for StaticMutability {
2928 fn parse(input: ParseStream) -> Result<Self> {
2929 let mut_token: Option<crate::token::MutToken![mut]> = input.parse()?;
2930 Ok(mut_token.map_or(StaticMutability::None, StaticMutability::Mut))
2931 }
2932 }
2933}
2934
2935#[cfg(feature = "printing")]
2936mod printing {
2937 use crate::attr::FilterAttrs;
2938 use crate::data::Fields;
2939 use crate::item::{
2940 ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType, ImplItemConst,
2941 ImplItemFn, ImplItemMacro, ImplItemType, ItemConst, ItemEnum, ItemExternCrate, ItemFn,
2942 ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, ItemTrait,
2943 ItemTraitAlias, ItemType, ItemUnion, ItemUse, Receiver, Signature, StaticMutability,
2944 TraitItemConst, TraitItemFn, TraitItemMacro, TraitItemType, UseGlob, UseGroup, UseName,
2945 UsePath, UseRename, Variadic,
2946 };
2947 use crate::mac::MacroDelimiter;
2948 use crate::path;
2949 use crate::path::printing::PathStyle;
2950 use crate::print::TokensOrDefault;
2951 use crate::ty::Type;
2952 use proc_macro2::TokenStream;
2953 use quote::{ToTokens, TokenStreamExt as _};
2954
2955 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
2956 impl ToTokens for ItemExternCrate {
2957 fn to_tokens(&self, tokens: &mut TokenStream) {
2958 tokens.append_all(self.attrs.outer());
2959 self.vis.to_tokens(tokens);
2960 self.extern_token.to_tokens(tokens);
2961 self.crate_token.to_tokens(tokens);
2962 self.ident.to_tokens(tokens);
2963 if let Some((as_token, rename)) = &self.rename {
2964 as_token.to_tokens(tokens);
2965 rename.to_tokens(tokens);
2966 }
2967 self.semi_token.to_tokens(tokens);
2968 }
2969 }
2970
2971 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
2972 impl ToTokens for ItemUse {
2973 fn to_tokens(&self, tokens: &mut TokenStream) {
2974 tokens.append_all(self.attrs.outer());
2975 self.vis.to_tokens(tokens);
2976 self.use_token.to_tokens(tokens);
2977 self.leading_colon.to_tokens(tokens);
2978 self.tree.to_tokens(tokens);
2979 self.semi_token.to_tokens(tokens);
2980 }
2981 }
2982
2983 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
2984 impl ToTokens for ItemStatic {
2985 fn to_tokens(&self, tokens: &mut TokenStream) {
2986 tokens.append_all(self.attrs.outer());
2987 self.vis.to_tokens(tokens);
2988 self.static_token.to_tokens(tokens);
2989 self.mutability.to_tokens(tokens);
2990 self.ident.to_tokens(tokens);
2991 self.colon_token.to_tokens(tokens);
2992 self.ty.to_tokens(tokens);
2993 self.eq_token.to_tokens(tokens);
2994 self.expr.to_tokens(tokens);
2995 self.semi_token.to_tokens(tokens);
2996 }
2997 }
2998
2999 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3000 impl ToTokens for ItemConst {
3001 fn to_tokens(&self, tokens: &mut TokenStream) {
3002 tokens.append_all(self.attrs.outer());
3003 self.vis.to_tokens(tokens);
3004 self.const_token.to_tokens(tokens);
3005 self.ident.to_tokens(tokens);
3006 self.colon_token.to_tokens(tokens);
3007 self.ty.to_tokens(tokens);
3008 self.eq_token.to_tokens(tokens);
3009 self.expr.to_tokens(tokens);
3010 self.semi_token.to_tokens(tokens);
3011 }
3012 }
3013
3014 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3015 impl ToTokens for ItemFn {
3016 fn to_tokens(&self, tokens: &mut TokenStream) {
3017 tokens.append_all(self.attrs.outer());
3018 self.vis.to_tokens(tokens);
3019 self.sig.to_tokens(tokens);
3020 self.block.brace_token.surround(tokens, |tokens| {
3021 tokens.append_all(self.attrs.inner());
3022 tokens.append_all(&self.block.stmts);
3023 });
3024 }
3025 }
3026
3027 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3028 impl ToTokens for ItemMod {
3029 fn to_tokens(&self, tokens: &mut TokenStream) {
3030 tokens.append_all(self.attrs.outer());
3031 self.vis.to_tokens(tokens);
3032 self.unsafety.to_tokens(tokens);
3033 self.mod_token.to_tokens(tokens);
3034 self.ident.to_tokens(tokens);
3035 if let Some((brace, items)) = &self.content {
3036 brace.surround(tokens, |tokens| {
3037 tokens.append_all(self.attrs.inner());
3038 tokens.append_all(items);
3039 });
3040 } else {
3041 TokensOrDefault(&self.semi).to_tokens(tokens);
3042 }
3043 }
3044 }
3045
3046 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3047 impl ToTokens for ItemForeignMod {
3048 fn to_tokens(&self, tokens: &mut TokenStream) {
3049 tokens.append_all(self.attrs.outer());
3050 self.unsafety.to_tokens(tokens);
3051 self.abi.to_tokens(tokens);
3052 self.brace_token.surround(tokens, |tokens| {
3053 tokens.append_all(self.attrs.inner());
3054 tokens.append_all(&self.items);
3055 });
3056 }
3057 }
3058
3059 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3060 impl ToTokens for ItemType {
3061 fn to_tokens(&self, tokens: &mut TokenStream) {
3062 tokens.append_all(self.attrs.outer());
3063 self.vis.to_tokens(tokens);
3064 self.type_token.to_tokens(tokens);
3065 self.ident.to_tokens(tokens);
3066 self.generics.to_tokens(tokens);
3067 self.generics.where_clause.to_tokens(tokens);
3068 self.eq_token.to_tokens(tokens);
3069 self.ty.to_tokens(tokens);
3070 self.semi_token.to_tokens(tokens);
3071 }
3072 }
3073
3074 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3075 impl ToTokens for ItemEnum {
3076 fn to_tokens(&self, tokens: &mut TokenStream) {
3077 tokens.append_all(self.attrs.outer());
3078 self.vis.to_tokens(tokens);
3079 self.enum_token.to_tokens(tokens);
3080 self.ident.to_tokens(tokens);
3081 self.generics.to_tokens(tokens);
3082 self.generics.where_clause.to_tokens(tokens);
3083 self.brace_token.surround(tokens, |tokens| {
3084 self.variants.to_tokens(tokens);
3085 });
3086 }
3087 }
3088
3089 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3090 impl ToTokens for ItemStruct {
3091 fn to_tokens(&self, tokens: &mut TokenStream) {
3092 tokens.append_all(self.attrs.outer());
3093 self.vis.to_tokens(tokens);
3094 self.struct_token.to_tokens(tokens);
3095 self.ident.to_tokens(tokens);
3096 self.generics.to_tokens(tokens);
3097 match &self.fields {
3098 Fields::Named(fields) => {
3099 self.generics.where_clause.to_tokens(tokens);
3100 fields.to_tokens(tokens);
3101 }
3102 Fields::Unnamed(fields) => {
3103 fields.to_tokens(tokens);
3104 self.generics.where_clause.to_tokens(tokens);
3105 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3106 }
3107 Fields::Unit => {
3108 self.generics.where_clause.to_tokens(tokens);
3109 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3110 }
3111 }
3112 }
3113 }
3114
3115 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3116 impl ToTokens for ItemUnion {
3117 fn to_tokens(&self, tokens: &mut TokenStream) {
3118 tokens.append_all(self.attrs.outer());
3119 self.vis.to_tokens(tokens);
3120 self.union_token.to_tokens(tokens);
3121 self.ident.to_tokens(tokens);
3122 self.generics.to_tokens(tokens);
3123 self.generics.where_clause.to_tokens(tokens);
3124 self.fields.to_tokens(tokens);
3125 }
3126 }
3127
3128 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3129 impl ToTokens for ItemTrait {
3130 fn to_tokens(&self, tokens: &mut TokenStream) {
3131 tokens.append_all(self.attrs.outer());
3132 self.vis.to_tokens(tokens);
3133 self.unsafety.to_tokens(tokens);
3134 self.auto_token.to_tokens(tokens);
3135 self.trait_token.to_tokens(tokens);
3136 self.ident.to_tokens(tokens);
3137 self.generics.to_tokens(tokens);
3138 if !self.supertraits.is_empty() {
3139 TokensOrDefault(&self.colon_token).to_tokens(tokens);
3140 self.supertraits.to_tokens(tokens);
3141 }
3142 self.generics.where_clause.to_tokens(tokens);
3143 self.brace_token.surround(tokens, |tokens| {
3144 tokens.append_all(self.attrs.inner());
3145 tokens.append_all(&self.items);
3146 });
3147 }
3148 }
3149
3150 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3151 impl ToTokens for ItemTraitAlias {
3152 fn to_tokens(&self, tokens: &mut TokenStream) {
3153 tokens.append_all(self.attrs.outer());
3154 self.vis.to_tokens(tokens);
3155 self.trait_token.to_tokens(tokens);
3156 self.ident.to_tokens(tokens);
3157 self.generics.to_tokens(tokens);
3158 self.eq_token.to_tokens(tokens);
3159 self.bounds.to_tokens(tokens);
3160 self.generics.where_clause.to_tokens(tokens);
3161 self.semi_token.to_tokens(tokens);
3162 }
3163 }
3164
3165 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3166 impl ToTokens for ItemImpl {
3167 fn to_tokens(&self, tokens: &mut TokenStream) {
3168 tokens.append_all(self.attrs.outer());
3169 self.defaultness.to_tokens(tokens);
3170 self.unsafety.to_tokens(tokens);
3171 self.impl_token.to_tokens(tokens);
3172 self.generics.to_tokens(tokens);
3173 if let Some((polarity, path, for_token)) = &self.trait_ {
3174 polarity.to_tokens(tokens);
3175 path.to_tokens(tokens);
3176 for_token.to_tokens(tokens);
3177 }
3178 self.self_ty.to_tokens(tokens);
3179 self.generics.where_clause.to_tokens(tokens);
3180 self.brace_token.surround(tokens, |tokens| {
3181 tokens.append_all(self.attrs.inner());
3182 tokens.append_all(&self.items);
3183 });
3184 }
3185 }
3186
3187 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3188 impl ToTokens for ItemMacro {
3189 fn to_tokens(&self, tokens: &mut TokenStream) {
3190 tokens.append_all(self.attrs.outer());
3191 path::printing::print_path(tokens, &self.mac.path, PathStyle::Mod);
3192 self.mac.bang_token.to_tokens(tokens);
3193 self.ident.to_tokens(tokens);
3194 match &self.mac.delimiter {
3195 MacroDelimiter::Paren(paren) => {
3196 paren.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3197 }
3198 MacroDelimiter::Brace(brace) => {
3199 brace.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3200 }
3201 MacroDelimiter::Bracket(bracket) => {
3202 bracket.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3203 }
3204 }
3205 self.semi_token.to_tokens(tokens);
3206 }
3207 }
3208
3209 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3210 impl ToTokens for UsePath {
3211 fn to_tokens(&self, tokens: &mut TokenStream) {
3212 self.ident.to_tokens(tokens);
3213 self.colon2_token.to_tokens(tokens);
3214 self.tree.to_tokens(tokens);
3215 }
3216 }
3217
3218 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3219 impl ToTokens for UseName {
3220 fn to_tokens(&self, tokens: &mut TokenStream) {
3221 self.ident.to_tokens(tokens);
3222 }
3223 }
3224
3225 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3226 impl ToTokens for UseRename {
3227 fn to_tokens(&self, tokens: &mut TokenStream) {
3228 self.ident.to_tokens(tokens);
3229 self.as_token.to_tokens(tokens);
3230 self.rename.to_tokens(tokens);
3231 }
3232 }
3233
3234 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3235 impl ToTokens for UseGlob {
3236 fn to_tokens(&self, tokens: &mut TokenStream) {
3237 self.star_token.to_tokens(tokens);
3238 }
3239 }
3240
3241 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3242 impl ToTokens for UseGroup {
3243 fn to_tokens(&self, tokens: &mut TokenStream) {
3244 self.brace_token.surround(tokens, |tokens| {
3245 self.items.to_tokens(tokens);
3246 });
3247 }
3248 }
3249
3250 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3251 impl ToTokens for TraitItemConst {
3252 fn to_tokens(&self, tokens: &mut TokenStream) {
3253 tokens.append_all(self.attrs.outer());
3254 self.const_token.to_tokens(tokens);
3255 self.ident.to_tokens(tokens);
3256 self.colon_token.to_tokens(tokens);
3257 self.ty.to_tokens(tokens);
3258 if let Some((eq_token, default)) = &self.default {
3259 eq_token.to_tokens(tokens);
3260 default.to_tokens(tokens);
3261 }
3262 self.semi_token.to_tokens(tokens);
3263 }
3264 }
3265
3266 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3267 impl ToTokens for TraitItemFn {
3268 fn to_tokens(&self, tokens: &mut TokenStream) {
3269 tokens.append_all(self.attrs.outer());
3270 self.sig.to_tokens(tokens);
3271 match &self.default {
3272 Some(block) => {
3273 block.brace_token.surround(tokens, |tokens| {
3274 tokens.append_all(self.attrs.inner());
3275 tokens.append_all(&block.stmts);
3276 });
3277 }
3278 None => {
3279 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3280 }
3281 }
3282 }
3283 }
3284
3285 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3286 impl ToTokens for TraitItemType {
3287 fn to_tokens(&self, tokens: &mut TokenStream) {
3288 tokens.append_all(self.attrs.outer());
3289 self.type_token.to_tokens(tokens);
3290 self.ident.to_tokens(tokens);
3291 self.generics.to_tokens(tokens);
3292 if !self.bounds.is_empty() {
3293 TokensOrDefault(&self.colon_token).to_tokens(tokens);
3294 self.bounds.to_tokens(tokens);
3295 }
3296 if let Some((eq_token, default)) = &self.default {
3297 eq_token.to_tokens(tokens);
3298 default.to_tokens(tokens);
3299 }
3300 self.generics.where_clause.to_tokens(tokens);
3301 self.semi_token.to_tokens(tokens);
3302 }
3303 }
3304
3305 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3306 impl ToTokens for TraitItemMacro {
3307 fn to_tokens(&self, tokens: &mut TokenStream) {
3308 tokens.append_all(self.attrs.outer());
3309 self.mac.to_tokens(tokens);
3310 self.semi_token.to_tokens(tokens);
3311 }
3312 }
3313
3314 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3315 impl ToTokens for ImplItemConst {
3316 fn to_tokens(&self, tokens: &mut TokenStream) {
3317 tokens.append_all(self.attrs.outer());
3318 self.vis.to_tokens(tokens);
3319 self.defaultness.to_tokens(tokens);
3320 self.const_token.to_tokens(tokens);
3321 self.ident.to_tokens(tokens);
3322 self.colon_token.to_tokens(tokens);
3323 self.ty.to_tokens(tokens);
3324 self.eq_token.to_tokens(tokens);
3325 self.expr.to_tokens(tokens);
3326 self.semi_token.to_tokens(tokens);
3327 }
3328 }
3329
3330 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3331 impl ToTokens for ImplItemFn {
3332 fn to_tokens(&self, tokens: &mut TokenStream) {
3333 tokens.append_all(self.attrs.outer());
3334 self.vis.to_tokens(tokens);
3335 self.defaultness.to_tokens(tokens);
3336 self.sig.to_tokens(tokens);
3337 self.block.brace_token.surround(tokens, |tokens| {
3338 tokens.append_all(self.attrs.inner());
3339 tokens.append_all(&self.block.stmts);
3340 });
3341 }
3342 }
3343
3344 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3345 impl ToTokens for ImplItemType {
3346 fn to_tokens(&self, tokens: &mut TokenStream) {
3347 tokens.append_all(self.attrs.outer());
3348 self.vis.to_tokens(tokens);
3349 self.defaultness.to_tokens(tokens);
3350 self.type_token.to_tokens(tokens);
3351 self.ident.to_tokens(tokens);
3352 self.generics.to_tokens(tokens);
3353 self.eq_token.to_tokens(tokens);
3354 self.ty.to_tokens(tokens);
3355 self.generics.where_clause.to_tokens(tokens);
3356 self.semi_token.to_tokens(tokens);
3357 }
3358 }
3359
3360 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3361 impl ToTokens for ImplItemMacro {
3362 fn to_tokens(&self, tokens: &mut TokenStream) {
3363 tokens.append_all(self.attrs.outer());
3364 self.mac.to_tokens(tokens);
3365 self.semi_token.to_tokens(tokens);
3366 }
3367 }
3368
3369 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3370 impl ToTokens for ForeignItemFn {
3371 fn to_tokens(&self, tokens: &mut TokenStream) {
3372 tokens.append_all(self.attrs.outer());
3373 self.vis.to_tokens(tokens);
3374 self.sig.to_tokens(tokens);
3375 self.semi_token.to_tokens(tokens);
3376 }
3377 }
3378
3379 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3380 impl ToTokens for ForeignItemStatic {
3381 fn to_tokens(&self, tokens: &mut TokenStream) {
3382 tokens.append_all(self.attrs.outer());
3383 self.vis.to_tokens(tokens);
3384 self.static_token.to_tokens(tokens);
3385 self.mutability.to_tokens(tokens);
3386 self.ident.to_tokens(tokens);
3387 self.colon_token.to_tokens(tokens);
3388 self.ty.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 ForeignItemType {
3395 fn to_tokens(&self, tokens: &mut TokenStream) {
3396 tokens.append_all(self.attrs.outer());
3397 self.vis.to_tokens(tokens);
3398 self.type_token.to_tokens(tokens);
3399 self.ident.to_tokens(tokens);
3400 self.generics.to_tokens(tokens);
3401 self.generics.where_clause.to_tokens(tokens);
3402 self.semi_token.to_tokens(tokens);
3403 }
3404 }
3405
3406 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3407 impl ToTokens for ForeignItemMacro {
3408 fn to_tokens(&self, tokens: &mut TokenStream) {
3409 tokens.append_all(self.attrs.outer());
3410 self.mac.to_tokens(tokens);
3411 self.semi_token.to_tokens(tokens);
3412 }
3413 }
3414
3415 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3416 impl ToTokens for Signature {
3417 fn to_tokens(&self, tokens: &mut TokenStream) {
3418 self.constness.to_tokens(tokens);
3419 self.asyncness.to_tokens(tokens);
3420 self.unsafety.to_tokens(tokens);
3421 self.abi.to_tokens(tokens);
3422 self.fn_token.to_tokens(tokens);
3423 self.ident.to_tokens(tokens);
3424 self.generics.to_tokens(tokens);
3425 self.paren_token.surround(tokens, |tokens| {
3426 self.inputs.to_tokens(tokens);
3427 if let Some(variadic) = &self.variadic {
3428 if !self.inputs.empty_or_trailing() {
3429 <crate::token::CommaToken![,]>::default().to_tokens(tokens);
3430 }
3431 variadic.to_tokens(tokens);
3432 }
3433 });
3434 self.output.to_tokens(tokens);
3435 self.generics.where_clause.to_tokens(tokens);
3436 }
3437 }
3438
3439 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3440 impl ToTokens for Receiver {
3441 fn to_tokens(&self, tokens: &mut TokenStream) {
3442 tokens.append_all(self.attrs.outer());
3443 if let Some((ampersand, lifetime)) = &self.reference {
3444 ampersand.to_tokens(tokens);
3445 lifetime.to_tokens(tokens);
3446 }
3447 self.mutability.to_tokens(tokens);
3448 self.self_token.to_tokens(tokens);
3449 if let Some(colon_token) = &self.colon_token {
3450 colon_token.to_tokens(tokens);
3451 self.ty.to_tokens(tokens);
3452 } else {
3453 let consistent = match (&self.reference, &self.mutability, &*self.ty) {
3454 (Some(_), mutability, Type::Reference(ty)) => {
3455 mutability.is_some() == ty.mutability.is_some()
3456 && match &*ty.elem {
3457 Type::Path(ty) => ty.qself.is_none() && ty.path.is_ident("Self"),
3458 _ => false,
3459 }
3460 }
3461 (None, _, Type::Path(ty)) => ty.qself.is_none() && ty.path.is_ident("Self"),
3462 _ => false,
3463 };
3464 if !consistent {
3465 <crate::token::ColonToken![:]>::default().to_tokens(tokens);
3466 self.ty.to_tokens(tokens);
3467 }
3468 }
3469 }
3470 }
3471
3472 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3473 impl ToTokens for Variadic {
3474 fn to_tokens(&self, tokens: &mut TokenStream) {
3475 tokens.append_all(self.attrs.outer());
3476 if let Some((pat, colon)) = &self.pat {
3477 pat.to_tokens(tokens);
3478 colon.to_tokens(tokens);
3479 }
3480 self.dots.to_tokens(tokens);
3481 self.comma.to_tokens(tokens);
3482 }
3483 }
3484
3485 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3486 impl ToTokens for StaticMutability {
3487 fn to_tokens(&self, tokens: &mut TokenStream) {
3488 match self {
3489 StaticMutability::None => {}
3490 StaticMutability::Mut(mut_token) => mut_token.to_tokens(tokens),
3491 }
3492 }
3493 }
3494}