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"]
#[doc(cfg(feature = "full"))]
#[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) }
}
#[doc(cfg(feature = "printing"))]
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`."]
#[doc(cfg(feature = "full"))]
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) }`."]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
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<()> { ... }`."]
#[doc(cfg(feature = "full"))]
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" { ... }`."#]
#[doc(cfg(feature = "full"))]
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> { ... }`."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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 { ... }`."]
#[doc(cfg(feature = "full"))]
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)`."]
#[doc(cfg(feature = "full"))]
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 }`."]
#[doc(cfg(feature = "full"))]
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 { ... }`."]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
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>`."]
#[doc(cfg(feature = "full"))]
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 }`."]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
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"]
#[doc(cfg(feature = "full"))]
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) }
}
#[doc(cfg(feature = "printing"))]
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::...`."]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
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: `*`."]
#[doc(cfg(feature = "full"))]
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}`."]
#[doc(cfg(feature = "full"))]
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"]
#[doc(cfg(feature = "full"))]
#[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) }
}
#[doc(cfg(feature = "printing"))]
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."]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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"]
#[doc(cfg(feature = "full"))]
#[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) }
}
#[doc(cfg(feature = "printing"))]
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."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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"]
#[doc(cfg(feature = "full"))]
#[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) }
}
#[doc(cfg(feature = "printing"))]
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."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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."]
#[doc(cfg(feature = "full"))]
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)`."]
#[doc(cfg(feature = "full"))]
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)`."]
#[doc(cfg(feature = "full"))]
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) }
}
#[doc(cfg(feature = "printing"))]
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`."]
#[doc(cfg(feature = "full"))]
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" ```"]
#[doc(cfg(feature = "full"))]
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`."]
#[doc(cfg(feature = "full"))]
#[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."]
#[doc(cfg(feature = "full"))]
#[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::buffer::Cursor;
911 use crate::derive;
912 use crate::error::{Error, Result};
913 use crate::expr::Expr;
914 use crate::ext::IdentExt as _;
915 use crate::generics::{self, Generics, TypeParamBound};
916 use crate::ident::Ident;
917 use crate::item::{
918 FnArg, ForeignItem, ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType,
919 ImplItem, ImplItemConst, ImplItemFn, ImplItemMacro, ImplItemType, Item, ItemConst,
920 ItemEnum, ItemExternCrate, ItemFn, ItemForeignMod, ItemImpl, ItemMacro, ItemMod,
921 ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, ItemUnion, ItemUse, Receiver,
922 Signature, StaticMutability, TraitItem, TraitItemConst, TraitItemFn, TraitItemMacro,
923 TraitItemType, UseGlob, UseGroup, UseName, UsePath, UseRename, UseTree, Variadic,
924 };
925 use crate::lifetime::Lifetime;
926 use crate::lit::LitStr;
927 use crate::mac::{self, Macro};
928 use crate::parse::discouraged::Speculative as _;
929 use crate::parse::{Parse, ParseStream};
930 use crate::pat::{Pat, PatType, PatWild};
931 use crate::path::Path;
932 use crate::punctuated::Punctuated;
933 use crate::restriction::Visibility;
934 use crate::stmt::Block;
935 use crate::token;
936 use crate::ty::{Abi, ReturnType, Type, TypePath, TypeReference};
937 use crate::verbatim;
938 use alloc::boxed::Box;
939 use alloc::vec::Vec;
940 use proc_macro2::TokenStream;
941
942 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
943 impl Parse for Item {
944 fn parse(input: ParseStream) -> Result<Self> {
945 let begin = input.cursor();
946 let attrs = input.call(Attribute::parse_outer)?;
947 parse_rest_of_item(begin, attrs, input)
948 }
949 }
950
951 pub(crate) fn parse_rest_of_item(
952 begin: Cursor,
953 mut attrs: Vec<Attribute>,
954 input: ParseStream,
955 ) -> Result<Item> {
956 let ahead = input.fork();
957 let vis: Visibility = ahead.parse()?;
958
959 let lookahead = ahead.lookahead1();
960 let allow_safe = false;
961 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
962 let vis: Visibility = input.parse()?;
963 let sig: Signature = input.parse()?;
964 if input.peek(crate::token::SemiToken![;]) {
965 input.parse::<crate::token::SemiToken![;]>()?;
966 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
967 } else {
968 parse_rest_of_fn(input, Vec::new(), vis, sig).map(Item::Fn)
969 }
970 } else if lookahead.peek(crate::token::ExternToken![extern]) {
971 ahead.parse::<crate::token::ExternToken![extern]>()?;
972 let lookahead = ahead.lookahead1();
973 if lookahead.peek(crate::token::CrateToken![crate]) {
974 input.parse().map(Item::ExternCrate)
975 } else if lookahead.peek(token::Brace) {
976 input.parse().map(Item::ForeignMod)
977 } else if lookahead.peek(LitStr) {
978 ahead.parse::<LitStr>()?;
979 let lookahead = ahead.lookahead1();
980 if lookahead.peek(token::Brace) {
981 input.parse().map(Item::ForeignMod)
982 } else {
983 Err(lookahead.error())
984 }
985 } else {
986 Err(lookahead.error())
987 }
988 } else if lookahead.peek(crate::token::UseToken![use]) {
989 let allow_crate_root_in_path = true;
990 match parse_item_use(input, allow_crate_root_in_path)? {
991 Some(item_use) => Ok(Item::Use(item_use)),
992 None => Ok(Item::Verbatim(verbatim::between(begin, input.cursor()))),
993 }
994 } else if lookahead.peek(crate::token::StaticToken![static]) {
995 let vis = input.parse()?;
996 let static_token = input.parse()?;
997 let mutability = input.parse()?;
998 let ident = input.parse()?;
999 if input.peek(crate::token::EqToken![=]) {
1000 input.parse::<crate::token::EqToken![=]>()?;
1001 input.parse::<Expr>()?;
1002 input.parse::<crate::token::SemiToken![;]>()?;
1003 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
1004 } else {
1005 let colon_token = input.parse()?;
1006 let ty = input.parse()?;
1007 if input.peek(crate::token::SemiToken![;]) {
1008 input.parse::<crate::token::SemiToken![;]>()?;
1009 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
1010 } else {
1011 Ok(Item::Static(ItemStatic {
1012 attrs: Vec::new(),
1013 vis,
1014 static_token,
1015 mutability,
1016 ident,
1017 colon_token,
1018 ty,
1019 eq_token: input.parse()?,
1020 expr: input.parse()?,
1021 semi_token: input.parse()?,
1022 }))
1023 }
1024 }
1025 } else if lookahead.peek(crate::token::ConstToken![const]) {
1026 let vis = input.parse()?;
1027 let const_token: crate::token::ConstToken![const] = input.parse()?;
1028 let lookahead = input.lookahead1();
1029 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
1030 input.call(Ident::parse_any)?
1031 } else {
1032 return Err(lookahead.error());
1033 };
1034 let mut generics: Generics = input.parse()?;
1035 let colon_token = input.parse()?;
1036 let ty = input.parse()?;
1037 let value = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
1038 let expr: Expr = input.parse()?;
1039 Some((eq_token, expr))
1040 } else {
1041 None
1042 };
1043 generics.where_clause = input.parse()?;
1044 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1045 match value {
1046 Some((eq_token, expr))
1047 if generics.lt_token.is_none() && generics.where_clause.is_none() =>
1048 {
1049 Ok(Item::Const(ItemConst {
1050 attrs: Vec::new(),
1051 vis,
1052 const_token,
1053 ident,
1054 generics,
1055 colon_token,
1056 ty,
1057 eq_token,
1058 expr: Box::new(expr),
1059 semi_token,
1060 }))
1061 }
1062 _ => Ok(Item::Verbatim(verbatim::between(begin, input.cursor()))),
1063 }
1064 } else if lookahead.peek(crate::token::UnsafeToken![unsafe]) {
1065 ahead.parse::<crate::token::UnsafeToken![unsafe]>()?;
1066 let lookahead = ahead.lookahead1();
1067 if lookahead.peek(crate::token::TraitToken![trait])
1068 || lookahead.peek(crate::token::AutoToken![auto]) && ahead.peek2(crate::token::TraitToken![trait])
1069 {
1070 input.parse().map(Item::Trait)
1071 } else if lookahead.peek(crate::token::ImplToken![impl]) {
1072 let allow_verbatim_impl = true;
1073 if let Some(item) = parse_impl(input, allow_verbatim_impl)? {
1074 Ok(Item::Impl(item))
1075 } else {
1076 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
1077 }
1078 } else if lookahead.peek(crate::token::ExternToken![extern]) {
1079 input.parse().map(Item::ForeignMod)
1080 } else if lookahead.peek(crate::token::ModToken![mod]) {
1081 input.parse().map(Item::Mod)
1082 } else {
1083 Err(lookahead.error())
1084 }
1085 } else if lookahead.peek(crate::token::ModToken![mod]) {
1086 input.parse().map(Item::Mod)
1087 } else if lookahead.peek(crate::token::TypeToken![type]) {
1088 parse_item_type(begin, input)
1089 } else if lookahead.peek(crate::token::StructToken![struct]) {
1090 input.parse().map(Item::Struct)
1091 } else if lookahead.peek(crate::token::EnumToken![enum]) {
1092 input.parse().map(Item::Enum)
1093 } else if lookahead.peek(crate::token::UnionToken![union]) && ahead.peek2(Ident) {
1094 input.parse().map(Item::Union)
1095 } else if lookahead.peek(crate::token::TraitToken![trait]) {
1096 input.call(parse_trait_or_trait_alias)
1097 } else if lookahead.peek(crate::token::AutoToken![auto]) && ahead.peek2(crate::token::TraitToken![trait]) {
1098 input.parse().map(Item::Trait)
1099 } else if lookahead.peek(crate::token::ImplToken![impl])
1100 || lookahead.peek(crate::token::DefaultToken![default]) && !ahead.peek2(crate::token::NotToken![!])
1101 {
1102 let allow_verbatim_impl = true;
1103 if let Some(item) = parse_impl(input, allow_verbatim_impl)? {
1104 Ok(Item::Impl(item))
1105 } else {
1106 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
1107 }
1108 } else if lookahead.peek(crate::token::MacroToken![macro]) {
1109 input.advance_to(&ahead);
1110 parse_macro2(begin, vis, input)
1111 } else if vis.is_inherited()
1112 && (lookahead.peek(Ident)
1113 || lookahead.peek(crate::token::SelfValueToken![self])
1114 || lookahead.peek(crate::token::SuperToken![super])
1115 || lookahead.peek(crate::token::CrateToken![crate])
1116 || lookahead.peek(crate::token::PathSepToken![::]))
1117 {
1118 input.parse().map(Item::Macro)
1119 } else {
1120 Err(lookahead.error())
1121 }?;
1122
1123 attrs.extend(item.replace_attrs(Vec::new()));
1124 item.replace_attrs(attrs);
1125 Ok(item)
1126 }
1127
1128 struct FlexibleItemType {
1129 vis: Visibility,
1130 defaultness: Option<crate::token::DefaultToken![default]>,
1131 type_token: crate::token::TypeToken![type],
1132 ident: Ident,
1133 generics: Generics,
1134 colon_token: Option<crate::token::ColonToken![:]>,
1135 bounds: Punctuated<TypeParamBound, crate::token::PlusToken![+]>,
1136 ty: Option<(crate::token::EqToken![=], Type)>,
1137 semi_token: crate::token::SemiToken![;],
1138 }
1139
1140 enum TypeDefaultness {
1141 Optional,
1142 Disallowed,
1143 }
1144
1145 enum WhereClauseLocation {
1146 BeforeEq,
1148 AfterEq,
1150 Both,
1152 }
1153
1154 impl FlexibleItemType {
1155 fn parse(
1156 input: ParseStream,
1157 allow_defaultness: TypeDefaultness,
1158 where_clause_location: WhereClauseLocation,
1159 ) -> Result<Self> {
1160 let vis: Visibility = input.parse()?;
1161 let defaultness: Option<crate::token::DefaultToken![default]> = match allow_defaultness {
1162 TypeDefaultness::Optional => input.parse()?,
1163 TypeDefaultness::Disallowed => None,
1164 };
1165 let type_token: crate::token::TypeToken![type] = input.parse()?;
1166 let ident: Ident = input.parse()?;
1167 let mut generics: Generics = input.parse()?;
1168 let (colon_token, bounds) = Self::parse_optional_bounds(input)?;
1169
1170 match where_clause_location {
1171 WhereClauseLocation::BeforeEq | WhereClauseLocation::Both => {
1172 generics.where_clause = input.parse()?;
1173 }
1174 WhereClauseLocation::AfterEq => {}
1175 }
1176
1177 let ty = Self::parse_optional_definition(input)?;
1178
1179 match where_clause_location {
1180 WhereClauseLocation::AfterEq | WhereClauseLocation::Both
1181 if generics.where_clause.is_none() =>
1182 {
1183 generics.where_clause = input.parse()?;
1184 }
1185 _ => {}
1186 }
1187
1188 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1189
1190 Ok(FlexibleItemType {
1191 vis,
1192 defaultness,
1193 type_token,
1194 ident,
1195 generics,
1196 colon_token,
1197 bounds,
1198 ty,
1199 semi_token,
1200 })
1201 }
1202
1203 fn parse_optional_bounds(
1204 input: ParseStream,
1205 ) -> Result<(Option<crate::token::ColonToken![:]>, Punctuated<TypeParamBound, crate::token::PlusToken![+]>)> {
1206 let colon_token: Option<crate::token::ColonToken![:]> = input.parse()?;
1207
1208 let mut bounds = Punctuated::new();
1209 if colon_token.is_some() {
1210 loop {
1211 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::EqToken![=]) || input.peek(crate::token::SemiToken![;]) {
1212 break;
1213 }
1214 bounds.push_value({
1215 let allow_precise_capture = false;
1216 let allow_const = true;
1217 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
1218 });
1219 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::EqToken![=]) || input.peek(crate::token::SemiToken![;]) {
1220 break;
1221 }
1222 bounds.push_punct(input.parse::<crate::token::PlusToken![+]>()?);
1223 }
1224 }
1225
1226 Ok((colon_token, bounds))
1227 }
1228
1229 fn parse_optional_definition(input: ParseStream) -> Result<Option<(crate::token::EqToken![=], Type)>> {
1230 let eq_token: Option<crate::token::EqToken![=]> = input.parse()?;
1231 if let Some(eq_token) = eq_token {
1232 let definition: Type = input.parse()?;
1233 Ok(Some((eq_token, definition)))
1234 } else {
1235 Ok(None)
1236 }
1237 }
1238 }
1239
1240 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1241 impl Parse for ItemMacro {
1242 fn parse(input: ParseStream) -> Result<Self> {
1243 let attrs = input.call(Attribute::parse_outer)?;
1244 let path = input.call(Path::parse_mod_style)?;
1245 let bang_token: crate::token::NotToken![!] = input.parse()?;
1246 let ident: Option<Ident> = if input.peek(crate::token::TryToken![try]) {
1247 input.call(Ident::parse_any).map(Some)
1248 } else {
1249 input.parse()
1250 }?;
1251 let (delimiter, tokens) = input.call(mac::parse_delimiter)?;
1252 let semi_token: Option<crate::token::SemiToken![;]> = if !delimiter.is_brace() {
1253 Some(input.parse()?)
1254 } else {
1255 None
1256 };
1257 Ok(ItemMacro {
1258 attrs,
1259 ident,
1260 mac: Macro {
1261 path,
1262 bang_token,
1263 delimiter,
1264 tokens,
1265 },
1266 semi_token,
1267 })
1268 }
1269 }
1270
1271 fn parse_macro2(begin: Cursor, _vis: Visibility, input: ParseStream) -> Result<Item> {
1272 input.parse::<crate::token::MacroToken![macro]>()?;
1273 input.parse::<Ident>()?;
1274
1275 let mut lookahead = input.lookahead1();
1276 if lookahead.peek(token::Paren) {
1277 let paren_content;
1278 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);
1279 paren_content.parse::<TokenStream>()?;
1280 lookahead = input.lookahead1();
1281 }
1282
1283 if lookahead.peek(token::Brace) {
1284 let brace_content;
1285 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);
1286 brace_content.parse::<TokenStream>()?;
1287 } else {
1288 return Err(lookahead.error());
1289 }
1290
1291 Ok(Item::Verbatim(verbatim::between(begin, input.cursor())))
1292 }
1293
1294 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1295 impl Parse for ItemExternCrate {
1296 fn parse(input: ParseStream) -> Result<Self> {
1297 Ok(ItemExternCrate {
1298 attrs: input.call(Attribute::parse_outer)?,
1299 vis: input.parse()?,
1300 extern_token: input.parse()?,
1301 crate_token: input.parse()?,
1302 ident: {
1303 if input.peek(crate::token::SelfValueToken![self]) {
1304 input.call(Ident::parse_any)?
1305 } else {
1306 input.parse()?
1307 }
1308 },
1309 rename: {
1310 if input.peek(crate::token::AsToken![as]) {
1311 let as_token: crate::token::AsToken![as] = input.parse()?;
1312 let rename: Ident = if input.peek(crate::token::UnderscoreToken![_]) {
1313 Ident::from(input.parse::<crate::token::UnderscoreToken![_]>()?)
1314 } else {
1315 input.parse()?
1316 };
1317 Some((as_token, rename))
1318 } else {
1319 None
1320 }
1321 },
1322 semi_token: input.parse()?,
1323 })
1324 }
1325 }
1326
1327 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1328 impl Parse for ItemUse {
1329 fn parse(input: ParseStream) -> Result<Self> {
1330 let allow_crate_root_in_path = false;
1331 parse_item_use(input, allow_crate_root_in_path).map(Option::unwrap)
1332 }
1333 }
1334
1335 fn parse_item_use(
1336 input: ParseStream,
1337 allow_crate_root_in_path: bool,
1338 ) -> Result<Option<ItemUse>> {
1339 let attrs = input.call(Attribute::parse_outer)?;
1340 let vis: Visibility = input.parse()?;
1341 let use_token: crate::token::UseToken![use] = input.parse()?;
1342 let leading_colon: Option<crate::token::PathSepToken![::]> = input.parse()?;
1343 let tree = parse_use_tree(input, allow_crate_root_in_path && leading_colon.is_none())?;
1344 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1345
1346 let tree = match tree {
1347 Some(tree) => tree,
1348 None => return Ok(None),
1349 };
1350
1351 Ok(Some(ItemUse {
1352 attrs,
1353 vis,
1354 use_token,
1355 leading_colon,
1356 tree,
1357 semi_token,
1358 }))
1359 }
1360
1361 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1362 impl Parse for UseTree {
1363 fn parse(input: ParseStream) -> Result<UseTree> {
1364 let allow_crate_root_in_path = false;
1365 parse_use_tree(input, allow_crate_root_in_path).map(Option::unwrap)
1366 }
1367 }
1368
1369 fn parse_use_tree(
1370 input: ParseStream,
1371 allow_crate_root_in_path: bool,
1372 ) -> Result<Option<UseTree>> {
1373 let lookahead = input.lookahead1();
1374 if lookahead.peek(Ident)
1375 || lookahead.peek(crate::token::SelfValueToken![self])
1376 || lookahead.peek(crate::token::SuperToken![super])
1377 || lookahead.peek(crate::token::CrateToken![crate])
1378 || lookahead.peek(crate::token::TryToken![try])
1379 {
1380 let ident = input.call(Ident::parse_any)?;
1381 if input.peek(crate::token::PathSepToken![::]) {
1382 Ok(Some(UseTree::Path(UsePath {
1383 ident,
1384 colon2_token: input.parse()?,
1385 tree: Box::new(input.parse()?),
1386 })))
1387 } else if input.peek(crate::token::AsToken![as]) {
1388 Ok(Some(UseTree::Rename(UseRename {
1389 ident,
1390 as_token: input.parse()?,
1391 rename: {
1392 if input.peek(Ident) {
1393 input.parse()?
1394 } else if input.peek(crate::token::UnderscoreToken![_]) {
1395 Ident::from(input.parse::<crate::token::UnderscoreToken![_]>()?)
1396 } else {
1397 return Err(input.error("expected identifier or underscore"));
1398 }
1399 },
1400 })))
1401 } else {
1402 Ok(Some(UseTree::Name(UseName { ident })))
1403 }
1404 } else if lookahead.peek(crate::token::StarToken![*]) {
1405 Ok(Some(UseTree::Glob(UseGlob {
1406 star_token: input.parse()?,
1407 })))
1408 } else if lookahead.peek(token::Brace) {
1409 let content;
1410 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);
1411 let mut items = Punctuated::new();
1412 let mut has_any_crate_root_in_path = false;
1413 loop {
1414 if content.is_empty() {
1415 break;
1416 }
1417 let this_tree_starts_with_crate_root =
1418 allow_crate_root_in_path && content.parse::<Option<crate::token::PathSepToken![::]>>()?.is_some();
1419 has_any_crate_root_in_path |= this_tree_starts_with_crate_root;
1420 match parse_use_tree(
1421 &content,
1422 allow_crate_root_in_path && !this_tree_starts_with_crate_root,
1423 )? {
1424 Some(tree) if !has_any_crate_root_in_path => items.push_value(tree),
1425 _ => has_any_crate_root_in_path = true,
1426 }
1427 if content.is_empty() {
1428 break;
1429 }
1430 let comma: crate::token::CommaToken![,] = content.parse()?;
1431 if !has_any_crate_root_in_path {
1432 items.push_punct(comma);
1433 }
1434 }
1435 if has_any_crate_root_in_path {
1436 Ok(None)
1437 } else {
1438 Ok(Some(UseTree::Group(UseGroup { brace_token, items })))
1439 }
1440 } else {
1441 Err(lookahead.error())
1442 }
1443 }
1444
1445 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1446 impl Parse for ItemStatic {
1447 fn parse(input: ParseStream) -> Result<Self> {
1448 Ok(ItemStatic {
1449 attrs: input.call(Attribute::parse_outer)?,
1450 vis: input.parse()?,
1451 static_token: input.parse()?,
1452 mutability: input.parse()?,
1453 ident: input.parse()?,
1454 colon_token: input.parse()?,
1455 ty: input.parse()?,
1456 eq_token: input.parse()?,
1457 expr: input.parse()?,
1458 semi_token: input.parse()?,
1459 })
1460 }
1461 }
1462
1463 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1464 impl Parse for ItemConst {
1465 fn parse(input: ParseStream) -> Result<Self> {
1466 let attrs = input.call(Attribute::parse_outer)?;
1467 let vis: Visibility = input.parse()?;
1468 let const_token: crate::token::ConstToken![const] = input.parse()?;
1469
1470 let lookahead = input.lookahead1();
1471 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
1472 input.call(Ident::parse_any)?
1473 } else {
1474 return Err(lookahead.error());
1475 };
1476
1477 let colon_token: crate::token::ColonToken![:] = input.parse()?;
1478 let ty: Type = input.parse()?;
1479 let eq_token: crate::token::EqToken![=] = input.parse()?;
1480 let expr: Expr = input.parse()?;
1481 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1482
1483 Ok(ItemConst {
1484 attrs,
1485 vis,
1486 const_token,
1487 ident,
1488 generics: Generics::default(),
1489 colon_token,
1490 ty: Box::new(ty),
1491 eq_token,
1492 expr: Box::new(expr),
1493 semi_token,
1494 })
1495 }
1496 }
1497
1498 fn peek_signature(input: ParseStream, allow_safe: bool) -> bool {
1499 let fork = input.fork();
1500 fork.parse::<Option<crate::token::ConstToken![const]>>().is_ok()
1501 && fork.parse::<Option<crate::token::AsyncToken![async]>>().is_ok()
1502 && ((allow_safe
1503 && token::parsing::peek_keyword(fork.cursor(), "safe")
1504 && token::parsing::keyword(&fork, "safe").is_ok())
1505 || fork.parse::<Option<crate::token::UnsafeToken![unsafe]>>().is_ok())
1506 && fork.parse::<Option<Abi>>().is_ok()
1507 && fork.peek(crate::token::FnToken![fn])
1508 }
1509
1510 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1511 impl Parse for Signature {
1512 fn parse(input: ParseStream) -> Result<Self> {
1513 let allow_safe = false;
1514 parse_signature(input, allow_safe).map(Option::unwrap)
1515 }
1516 }
1517
1518 fn parse_signature(input: ParseStream, allow_safe: bool) -> Result<Option<Signature>> {
1519 let constness: Option<crate::token::ConstToken![const]> = input.parse()?;
1520 let asyncness: Option<crate::token::AsyncToken![async]> = input.parse()?;
1521 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1522 let safe = allow_safe
1523 && unsafety.is_none()
1524 && token::parsing::peek_keyword(input.cursor(), "safe");
1525 if safe {
1526 token::parsing::keyword(input, "safe")?;
1527 }
1528 let abi: Option<Abi> = input.parse()?;
1529 let fn_token: crate::token::FnToken![fn] = input.parse()?;
1530 let ident: Ident = input.parse()?;
1531 let mut generics: Generics = input.parse()?;
1532
1533 let content;
1534 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);
1535 let (inputs, variadic) = parse_fn_args(&content)?;
1536
1537 let output: ReturnType = input.parse()?;
1538 generics.where_clause = input.parse()?;
1539
1540 Ok(if safe {
1541 None
1542 } else {
1543 Some(Signature {
1544 constness,
1545 asyncness,
1546 unsafety,
1547 abi,
1548 fn_token,
1549 ident,
1550 generics,
1551 paren_token,
1552 inputs,
1553 variadic,
1554 output,
1555 })
1556 })
1557 }
1558
1559 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1560 impl Parse for ItemFn {
1561 fn parse(input: ParseStream) -> Result<Self> {
1562 let outer_attrs = input.call(Attribute::parse_outer)?;
1563 let vis: Visibility = input.parse()?;
1564 let sig: Signature = input.parse()?;
1565 parse_rest_of_fn(input, outer_attrs, vis, sig)
1566 }
1567 }
1568
1569 fn parse_rest_of_fn(
1570 input: ParseStream,
1571 mut attrs: Vec<Attribute>,
1572 vis: Visibility,
1573 sig: Signature,
1574 ) -> Result<ItemFn> {
1575 let content;
1576 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);
1577 attr::parsing::parse_inner(&content, &mut attrs)?;
1578 let stmts = content.call(Block::parse_within)?;
1579
1580 Ok(ItemFn {
1581 attrs,
1582 vis,
1583 sig,
1584 block: Box::new(Block { brace_token, stmts }),
1585 })
1586 }
1587
1588 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1589 impl Parse for FnArg {
1590 fn parse(input: ParseStream) -> Result<Self> {
1591 let allow_variadic = false;
1592 let attrs = input.call(Attribute::parse_outer)?;
1593 match parse_fn_arg_or_variadic(input, attrs, allow_variadic)? {
1594 FnArgOrVariadic::FnArg(arg) => Ok(arg),
1595 FnArgOrVariadic::Variadic(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1596 }
1597 }
1598 }
1599
1600 enum FnArgOrVariadic {
1601 FnArg(FnArg),
1602 Variadic(Variadic),
1603 }
1604
1605 fn parse_fn_arg_or_variadic(
1606 input: ParseStream,
1607 attrs: Vec<Attribute>,
1608 allow_variadic: bool,
1609 ) -> Result<FnArgOrVariadic> {
1610 let ahead = input.fork();
1611 if let Ok((reference, mutability, self_token)) = parse_receiver_begin(&ahead) {
1612 input.advance_to(&ahead);
1613 let mut receiver = parse_rest_of_receiver(reference, mutability, self_token, input)?;
1614 receiver.attrs = attrs;
1615 return Ok(FnArgOrVariadic::FnArg(FnArg::Receiver(receiver)));
1616 }
1617
1618 if input.peek(Ident) && input.peek2(crate::token::LtToken![<]) {
1622 let span = input.span();
1623 return Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType {
1624 attrs,
1625 pat: Box::new(Pat::Wild(PatWild {
1626 attrs: Vec::new(),
1627 underscore_token: crate::token::UnderscoreToken,
1628 })),
1629 colon_token: crate::token::ColonToken,
1630 ty: input.parse()?,
1631 })));
1632 }
1633
1634 let pat = Box::new(Pat::parse_single(input)?);
1635 let colon_token: crate::token::ColonToken![:] = input.parse()?;
1636
1637 if allow_variadic {
1638 if let Some(dots) = input.parse::<Option<crate::token::DotDotDotToken![...]>>()? {
1639 return Ok(FnArgOrVariadic::Variadic(Variadic {
1640 attrs,
1641 pat: Some((pat, colon_token)),
1642 dots,
1643 comma: None,
1644 }));
1645 }
1646 }
1647
1648 Ok(FnArgOrVariadic::FnArg(FnArg::Typed(PatType {
1649 attrs,
1650 pat,
1651 colon_token,
1652 ty: input.parse()?,
1653 })))
1654 }
1655
1656 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1657 impl Parse for Receiver {
1658 fn parse(input: ParseStream) -> Result<Self> {
1659 let (reference, mutability, self_token) = parse_receiver_begin(input)?;
1660 parse_rest_of_receiver(reference, mutability, self_token, input)
1661 }
1662 }
1663
1664 fn parse_receiver_begin(
1665 input: ParseStream,
1666 ) -> Result<(
1667 Option<(crate::token::AndToken![&], Option<Lifetime>)>,
1668 Option<crate::token::MutToken![mut]>,
1669 crate::token::SelfValueToken![self],
1670 )> {
1671 let reference = if input.peek(crate::token::AndToken![&]) {
1672 let ampersand: crate::token::AndToken![&] = input.parse()?;
1673 let lifetime: Option<Lifetime> = input.parse()?;
1674 Some((ampersand, lifetime))
1675 } else {
1676 None
1677 };
1678 let mutability: Option<crate::token::MutToken![mut]> = input.parse()?;
1679 let self_token: crate::token::SelfValueToken![self] = input.parse()?;
1680 if input.peek(crate::token::PathSepToken![::]) {
1681 return Err(input.error("expected `:`"));
1682 }
1683 Ok((reference, mutability, self_token))
1684 }
1685
1686 fn parse_rest_of_receiver(
1687 reference: Option<(crate::token::AndToken![&], Option<Lifetime>)>,
1688 mutability: Option<crate::token::MutToken![mut]>,
1689 self_token: crate::token::SelfValueToken![self],
1690 input: ParseStream,
1691 ) -> Result<Receiver> {
1692 let colon_token: Option<crate::token::ColonToken![:]> = if reference.is_some() {
1693 None
1694 } else {
1695 input.parse()?
1696 };
1697 let ty: Type = if colon_token.is_some() {
1698 input.parse()?
1699 } else {
1700 let mut ty = Type::Path(TypePath {
1701 qself: None,
1702 path: Path::from(Ident::new("Self", self_token.span)),
1703 });
1704 if let Some((ampersand, lifetime)) = reference.as_ref() {
1705 ty = Type::Reference(TypeReference {
1706 and_token: crate::token::AndToken,
1707 lifetime: lifetime.clone(),
1708 mutability: mutability.as_ref().map(|m| crate::token::MutToken),
1709 elem: Box::new(ty),
1710 });
1711 }
1712 ty
1713 };
1714 Ok(Receiver {
1715 attrs: Vec::new(),
1716 reference,
1717 mutability,
1718 self_token,
1719 colon_token,
1720 ty: Box::new(ty),
1721 })
1722 }
1723
1724 fn parse_fn_args(
1725 input: ParseStream,
1726 ) -> Result<(Punctuated<FnArg, crate::token::CommaToken![,]>, Option<Variadic>)> {
1727 let mut args = Punctuated::new();
1728 let mut variadic = None;
1729 let mut has_receiver = false;
1730
1731 while !input.is_empty() {
1732 let attrs = input.call(Attribute::parse_outer)?;
1733
1734 if let Some(dots) = input.parse::<Option<crate::token::DotDotDotToken![...]>>()? {
1735 variadic = Some(Variadic {
1736 attrs,
1737 pat: None,
1738 dots,
1739 comma: if input.is_empty() {
1740 None
1741 } else {
1742 Some(input.parse()?)
1743 },
1744 });
1745 break;
1746 }
1747
1748 let allow_variadic = true;
1749 let arg = match parse_fn_arg_or_variadic(input, attrs, allow_variadic)? {
1750 FnArgOrVariadic::FnArg(arg) => arg,
1751 FnArgOrVariadic::Variadic(arg) => {
1752 variadic = Some(Variadic {
1753 comma: if input.is_empty() {
1754 None
1755 } else {
1756 Some(input.parse()?)
1757 },
1758 ..arg
1759 });
1760 break;
1761 }
1762 };
1763
1764 match &arg {
1765 FnArg::Receiver(receiver) if has_receiver => {
1766 return Err(Error::new(
1767 receiver.self_token.span,
1768 "unexpected second method receiver",
1769 ));
1770 }
1771 FnArg::Receiver(receiver) if !args.is_empty() => {
1772 return Err(Error::new(
1773 receiver.self_token.span,
1774 "unexpected method receiver",
1775 ));
1776 }
1777 FnArg::Receiver(_) => has_receiver = true,
1778 FnArg::Typed(_) => {}
1779 }
1780 args.push_value(arg);
1781
1782 if input.is_empty() {
1783 break;
1784 }
1785
1786 let comma: crate::token::CommaToken![,] = input.parse()?;
1787 args.push_punct(comma);
1788 }
1789
1790 Ok((args, variadic))
1791 }
1792
1793 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1794 impl Parse for ItemMod {
1795 fn parse(input: ParseStream) -> Result<Self> {
1796 let mut attrs = input.call(Attribute::parse_outer)?;
1797 let vis: Visibility = input.parse()?;
1798 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1799 let mod_token: crate::token::ModToken![mod] = input.parse()?;
1800 let ident: Ident = if input.peek(crate::token::TryToken![try]) {
1801 input.call(Ident::parse_any)
1802 } else {
1803 input.parse()
1804 }?;
1805
1806 let lookahead = input.lookahead1();
1807 if lookahead.peek(crate::token::SemiToken![;]) {
1808 Ok(ItemMod {
1809 attrs,
1810 vis,
1811 unsafety,
1812 mod_token,
1813 ident,
1814 content: None,
1815 semi: Some(input.parse()?),
1816 })
1817 } else if lookahead.peek(token::Brace) {
1818 let content;
1819 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);
1820 attr::parsing::parse_inner(&content, &mut attrs)?;
1821
1822 let mut items = Vec::new();
1823 while !content.is_empty() {
1824 items.push(content.parse()?);
1825 }
1826
1827 Ok(ItemMod {
1828 attrs,
1829 vis,
1830 unsafety,
1831 mod_token,
1832 ident,
1833 content: Some((brace_token, items)),
1834 semi: None,
1835 })
1836 } else {
1837 Err(lookahead.error())
1838 }
1839 }
1840 }
1841
1842 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1843 impl Parse for ItemForeignMod {
1844 fn parse(input: ParseStream) -> Result<Self> {
1845 let mut attrs = input.call(Attribute::parse_outer)?;
1846 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1847 let abi: Abi = input.parse()?;
1848
1849 let content;
1850 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);
1851 attr::parsing::parse_inner(&content, &mut attrs)?;
1852 let mut items = Vec::new();
1853 while !content.is_empty() {
1854 items.push(content.parse()?);
1855 }
1856
1857 Ok(ItemForeignMod {
1858 attrs,
1859 unsafety,
1860 abi,
1861 brace_token,
1862 items,
1863 })
1864 }
1865 }
1866
1867 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1868 impl Parse for ForeignItem {
1869 fn parse(input: ParseStream) -> Result<Self> {
1870 let begin = input.cursor();
1871 let mut attrs = input.call(Attribute::parse_outer)?;
1872 let ahead = input.fork();
1873 let vis: Visibility = ahead.parse()?;
1874
1875 let lookahead = ahead.lookahead1();
1876 let allow_safe = true;
1877 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
1878 let vis: Visibility = input.parse()?;
1879 let sig = parse_signature(input, allow_safe)?;
1880 let has_safe = sig.is_none();
1881 let has_body = input.peek(token::Brace);
1882 let semi_token: Option<crate::token::SemiToken![;]> = if has_body {
1883 let content;
1884 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);
1885 content.call(Attribute::parse_inner)?;
1886 content.call(Block::parse_within)?;
1887 None
1888 } else {
1889 Some(input.parse()?)
1890 };
1891 if has_safe || has_body {
1892 Ok(ForeignItem::Verbatim(verbatim::between(
1893 begin,
1894 input.cursor(),
1895 )))
1896 } else {
1897 Ok(ForeignItem::Fn(ForeignItemFn {
1898 attrs: Vec::new(),
1899 vis,
1900 sig: sig.unwrap(),
1901 semi_token: semi_token.unwrap(),
1902 }))
1903 }
1904 } else if lookahead.peek(crate::token::StaticToken![static])
1905 || ((ahead.peek(crate::token::UnsafeToken![unsafe])
1906 || token::parsing::peek_keyword(ahead.cursor(), "safe"))
1907 && ahead.peek2(crate::token::StaticToken![static]))
1908 {
1909 let vis = input.parse()?;
1910 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
1911 let safe =
1912 unsafety.is_none() && token::parsing::peek_keyword(input.cursor(), "safe");
1913 if safe {
1914 token::parsing::keyword(input, "safe")?;
1915 }
1916 let static_token = input.parse()?;
1917 let mutability = input.parse()?;
1918 let ident = input.parse()?;
1919 let colon_token = input.parse()?;
1920 let ty = input.parse()?;
1921 let has_value = input.peek(crate::token::EqToken![=]);
1922 if has_value {
1923 input.parse::<crate::token::EqToken![=]>()?;
1924 input.parse::<Expr>()?;
1925 }
1926 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1927 if unsafety.is_some() || safe || has_value {
1928 Ok(ForeignItem::Verbatim(verbatim::between(
1929 begin,
1930 input.cursor(),
1931 )))
1932 } else {
1933 Ok(ForeignItem::Static(ForeignItemStatic {
1934 attrs: Vec::new(),
1935 vis,
1936 static_token,
1937 mutability,
1938 ident,
1939 colon_token,
1940 ty,
1941 semi_token,
1942 }))
1943 }
1944 } else if lookahead.peek(crate::token::TypeToken![type]) {
1945 parse_foreign_item_type(begin, input)
1946 } else if vis.is_inherited()
1947 && (lookahead.peek(Ident)
1948 || lookahead.peek(crate::token::SelfValueToken![self])
1949 || lookahead.peek(crate::token::SuperToken![super])
1950 || lookahead.peek(crate::token::CrateToken![crate])
1951 || lookahead.peek(crate::token::PathSepToken![::]))
1952 {
1953 input.parse().map(ForeignItem::Macro)
1954 } else {
1955 Err(lookahead.error())
1956 }?;
1957
1958 let item_attrs = match &mut item {
1959 ForeignItem::Fn(item) => &mut item.attrs,
1960 ForeignItem::Static(item) => &mut item.attrs,
1961 ForeignItem::Type(item) => &mut item.attrs,
1962 ForeignItem::Macro(item) => &mut item.attrs,
1963 ForeignItem::Verbatim(_) => return Ok(item),
1964 };
1965 attrs.append(item_attrs);
1966 *item_attrs = attrs;
1967
1968 Ok(item)
1969 }
1970 }
1971
1972 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1973 impl Parse for ForeignItemFn {
1974 fn parse(input: ParseStream) -> Result<Self> {
1975 let attrs = input.call(Attribute::parse_outer)?;
1976 let vis: Visibility = input.parse()?;
1977 let sig: Signature = input.parse()?;
1978 let semi_token: crate::token::SemiToken![;] = input.parse()?;
1979 Ok(ForeignItemFn {
1980 attrs,
1981 vis,
1982 sig,
1983 semi_token,
1984 })
1985 }
1986 }
1987
1988 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
1989 impl Parse for ForeignItemStatic {
1990 fn parse(input: ParseStream) -> Result<Self> {
1991 Ok(ForeignItemStatic {
1992 attrs: input.call(Attribute::parse_outer)?,
1993 vis: input.parse()?,
1994 static_token: input.parse()?,
1995 mutability: input.parse()?,
1996 ident: input.parse()?,
1997 colon_token: input.parse()?,
1998 ty: input.parse()?,
1999 semi_token: input.parse()?,
2000 })
2001 }
2002 }
2003
2004 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2005 impl Parse for ForeignItemType {
2006 fn parse(input: ParseStream) -> Result<Self> {
2007 Ok(ForeignItemType {
2008 attrs: input.call(Attribute::parse_outer)?,
2009 vis: input.parse()?,
2010 type_token: input.parse()?,
2011 ident: input.parse()?,
2012 generics: {
2013 let mut generics: Generics = input.parse()?;
2014 generics.where_clause = input.parse()?;
2015 generics
2016 },
2017 semi_token: input.parse()?,
2018 })
2019 }
2020 }
2021
2022 fn parse_foreign_item_type(begin: Cursor, input: ParseStream) -> Result<ForeignItem> {
2023 let FlexibleItemType {
2024 vis,
2025 defaultness: _,
2026 type_token,
2027 ident,
2028 generics,
2029 colon_token,
2030 bounds: _,
2031 ty,
2032 semi_token,
2033 } = FlexibleItemType::parse(
2034 input,
2035 TypeDefaultness::Disallowed,
2036 WhereClauseLocation::Both,
2037 )?;
2038
2039 if colon_token.is_some() || ty.is_some() {
2040 Ok(ForeignItem::Verbatim(verbatim::between(
2041 begin,
2042 input.cursor(),
2043 )))
2044 } else {
2045 Ok(ForeignItem::Type(ForeignItemType {
2046 attrs: Vec::new(),
2047 vis,
2048 type_token,
2049 ident,
2050 generics,
2051 semi_token,
2052 }))
2053 }
2054 }
2055
2056 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2057 impl Parse for ForeignItemMacro {
2058 fn parse(input: ParseStream) -> Result<Self> {
2059 let attrs = input.call(Attribute::parse_outer)?;
2060 let mac: Macro = input.parse()?;
2061 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2062 None
2063 } else {
2064 Some(input.parse()?)
2065 };
2066 Ok(ForeignItemMacro {
2067 attrs,
2068 mac,
2069 semi_token,
2070 })
2071 }
2072 }
2073
2074 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2075 impl Parse for ItemType {
2076 fn parse(input: ParseStream) -> Result<Self> {
2077 Ok(ItemType {
2078 attrs: input.call(Attribute::parse_outer)?,
2079 vis: input.parse()?,
2080 type_token: input.parse()?,
2081 ident: input.parse()?,
2082 generics: {
2083 let mut generics: Generics = input.parse()?;
2084 generics.where_clause = input.parse()?;
2085 generics
2086 },
2087 eq_token: input.parse()?,
2088 ty: input.parse()?,
2089 semi_token: input.parse()?,
2090 })
2091 }
2092 }
2093
2094 fn parse_item_type(begin: Cursor, input: ParseStream) -> Result<Item> {
2095 let FlexibleItemType {
2096 vis,
2097 defaultness: _,
2098 type_token,
2099 ident,
2100 generics,
2101 colon_token,
2102 bounds: _,
2103 ty,
2104 semi_token,
2105 } = FlexibleItemType::parse(
2106 input,
2107 TypeDefaultness::Disallowed,
2108 WhereClauseLocation::BeforeEq,
2109 )?;
2110
2111 let (eq_token, ty) = match ty {
2112 Some(ty) if colon_token.is_none() => ty,
2113 _ => return Ok(Item::Verbatim(verbatim::between(begin, input.cursor()))),
2114 };
2115
2116 Ok(Item::Type(ItemType {
2117 attrs: Vec::new(),
2118 vis,
2119 type_token,
2120 ident,
2121 generics,
2122 eq_token,
2123 ty: Box::new(ty),
2124 semi_token,
2125 }))
2126 }
2127
2128 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2129 impl Parse for ItemStruct {
2130 fn parse(input: ParseStream) -> Result<Self> {
2131 let attrs = input.call(Attribute::parse_outer)?;
2132 let vis = input.parse::<Visibility>()?;
2133 let struct_token = input.parse::<crate::token::StructToken![struct]>()?;
2134 let ident = input.parse::<Ident>()?;
2135 let generics = input.parse::<Generics>()?;
2136 let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
2137 Ok(ItemStruct {
2138 attrs,
2139 vis,
2140 struct_token,
2141 ident,
2142 generics: Generics {
2143 where_clause,
2144 ..generics
2145 },
2146 fields,
2147 semi_token,
2148 })
2149 }
2150 }
2151
2152 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2153 impl Parse for ItemEnum {
2154 fn parse(input: ParseStream) -> Result<Self> {
2155 let attrs = input.call(Attribute::parse_outer)?;
2156 let vis = input.parse::<Visibility>()?;
2157 let enum_token = input.parse::<crate::token::EnumToken![enum]>()?;
2158 let ident = input.parse::<Ident>()?;
2159 let generics = input.parse::<Generics>()?;
2160 let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
2161 Ok(ItemEnum {
2162 attrs,
2163 vis,
2164 enum_token,
2165 ident,
2166 generics: Generics {
2167 where_clause,
2168 ..generics
2169 },
2170 brace_token,
2171 variants,
2172 })
2173 }
2174 }
2175
2176 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2177 impl Parse for ItemUnion {
2178 fn parse(input: ParseStream) -> Result<Self> {
2179 let attrs = input.call(Attribute::parse_outer)?;
2180 let vis = input.parse::<Visibility>()?;
2181 let union_token = input.parse::<crate::token::UnionToken![union]>()?;
2182 let ident = input.parse::<Ident>()?;
2183 let generics = input.parse::<Generics>()?;
2184 let (where_clause, fields) = derive::parsing::data_union(input)?;
2185 Ok(ItemUnion {
2186 attrs,
2187 vis,
2188 union_token,
2189 ident,
2190 generics: Generics {
2191 where_clause,
2192 ..generics
2193 },
2194 fields,
2195 })
2196 }
2197 }
2198
2199 fn parse_trait_or_trait_alias(input: ParseStream) -> Result<Item> {
2200 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
2201 let lookahead = input.lookahead1();
2202 if lookahead.peek(token::Brace)
2203 || lookahead.peek(crate::token::ColonToken![:])
2204 || lookahead.peek(crate::token::WhereToken![where])
2205 {
2206 let unsafety = None;
2207 let auto_token = None;
2208 parse_rest_of_trait(
2209 input,
2210 attrs,
2211 vis,
2212 unsafety,
2213 auto_token,
2214 trait_token,
2215 ident,
2216 generics,
2217 )
2218 .map(Item::Trait)
2219 } else if lookahead.peek(crate::token::EqToken![=]) {
2220 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
2221 .map(Item::TraitAlias)
2222 } else {
2223 Err(lookahead.error())
2224 }
2225 }
2226
2227 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2228 impl Parse for ItemTrait {
2229 fn parse(input: ParseStream) -> Result<Self> {
2230 let outer_attrs = input.call(Attribute::parse_outer)?;
2231 let vis: Visibility = input.parse()?;
2232 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
2233 let auto_token: Option<crate::token::AutoToken![auto]> = input.parse()?;
2234 let trait_token: crate::token::TraitToken![trait] = input.parse()?;
2235 let ident: Ident = input.parse()?;
2236 let generics: Generics = input.parse()?;
2237 parse_rest_of_trait(
2238 input,
2239 outer_attrs,
2240 vis,
2241 unsafety,
2242 auto_token,
2243 trait_token,
2244 ident,
2245 generics,
2246 )
2247 }
2248 }
2249
2250 fn parse_rest_of_trait(
2251 input: ParseStream,
2252 mut attrs: Vec<Attribute>,
2253 vis: Visibility,
2254 unsafety: Option<crate::token::UnsafeToken![unsafe]>,
2255 auto_token: Option<crate::token::AutoToken![auto]>,
2256 trait_token: crate::token::TraitToken![trait],
2257 ident: Ident,
2258 mut generics: Generics,
2259 ) -> Result<ItemTrait> {
2260 let colon_token: Option<crate::token::ColonToken![:]> = input.parse()?;
2261
2262 let mut supertraits = Punctuated::new();
2263 if colon_token.is_some() {
2264 loop {
2265 if input.peek(crate::token::WhereToken![where]) || input.peek(token::Brace) {
2266 break;
2267 }
2268 supertraits.push_value({
2269 let allow_precise_capture = false;
2270 let allow_const = true;
2271 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
2272 });
2273 if input.peek(crate::token::WhereToken![where]) || input.peek(token::Brace) {
2274 break;
2275 }
2276 supertraits.push_punct(input.parse()?);
2277 }
2278 }
2279
2280 generics.where_clause = input.parse()?;
2281
2282 let content;
2283 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);
2284 attr::parsing::parse_inner(&content, &mut attrs)?;
2285 let mut items = Vec::new();
2286 while !content.is_empty() {
2287 items.push(content.parse()?);
2288 }
2289
2290 Ok(ItemTrait {
2291 attrs,
2292 vis,
2293 unsafety,
2294 auto_token,
2295 restriction: None,
2296 trait_token,
2297 ident,
2298 generics,
2299 colon_token,
2300 supertraits,
2301 brace_token,
2302 items,
2303 })
2304 }
2305
2306 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2307 impl Parse for ItemTraitAlias {
2308 fn parse(input: ParseStream) -> Result<Self> {
2309 let (attrs, vis, trait_token, ident, generics) = parse_start_of_trait_alias(input)?;
2310 parse_rest_of_trait_alias(input, attrs, vis, trait_token, ident, generics)
2311 }
2312 }
2313
2314 fn parse_start_of_trait_alias(
2315 input: ParseStream,
2316 ) -> Result<(Vec<Attribute>, Visibility, crate::token::TraitToken![trait], Ident, Generics)> {
2317 let attrs = input.call(Attribute::parse_outer)?;
2318 let vis: Visibility = input.parse()?;
2319 let trait_token: crate::token::TraitToken![trait] = input.parse()?;
2320 let ident: Ident = input.parse()?;
2321 let generics: Generics = input.parse()?;
2322 Ok((attrs, vis, trait_token, ident, generics))
2323 }
2324
2325 fn parse_rest_of_trait_alias(
2326 input: ParseStream,
2327 attrs: Vec<Attribute>,
2328 vis: Visibility,
2329 trait_token: crate::token::TraitToken![trait],
2330 ident: Ident,
2331 mut generics: Generics,
2332 ) -> Result<ItemTraitAlias> {
2333 let eq_token: crate::token::EqToken![=] = input.parse()?;
2334
2335 let mut bounds = Punctuated::new();
2336 loop {
2337 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::SemiToken![;]) {
2338 break;
2339 }
2340 bounds.push_value({
2341 let allow_precise_capture = false;
2342 let allow_const = false;
2343 TypeParamBound::parse_single(input, allow_precise_capture, allow_const)?
2344 });
2345 if input.peek(crate::token::WhereToken![where]) || input.peek(crate::token::SemiToken![;]) {
2346 break;
2347 }
2348 bounds.push_punct(input.parse()?);
2349 }
2350
2351 generics.where_clause = input.parse()?;
2352 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2353
2354 Ok(ItemTraitAlias {
2355 attrs,
2356 vis,
2357 trait_token,
2358 ident,
2359 generics,
2360 eq_token,
2361 bounds,
2362 semi_token,
2363 })
2364 }
2365
2366 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2367 impl Parse for TraitItem {
2368 fn parse(input: ParseStream) -> Result<Self> {
2369 let begin = input.cursor();
2370 let mut attrs = input.call(Attribute::parse_outer)?;
2371 let vis: Visibility = input.parse()?;
2372 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2373 let ahead = input.fork();
2374
2375 let lookahead = ahead.lookahead1();
2376 let allow_safe = false;
2377 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
2378 input.parse().map(TraitItem::Fn)
2379 } else if lookahead.peek(crate::token::ConstToken![const]) {
2380 let const_token: crate::token::ConstToken![const] = ahead.parse()?;
2381 let lookahead = ahead.lookahead1();
2382 if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2383 input.advance_to(&ahead);
2384 let ident = input.call(Ident::parse_any)?;
2385 let mut generics: Generics = input.parse()?;
2386 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2387 let ty: Type = input.parse()?;
2388 let default = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
2389 let expr: Expr = input.parse()?;
2390 Some((eq_token, expr))
2391 } else {
2392 None
2393 };
2394 generics.where_clause = input.parse()?;
2395 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2396 if generics.lt_token.is_none() && generics.where_clause.is_none() {
2397 Ok(TraitItem::Const(TraitItemConst {
2398 attrs: Vec::new(),
2399 const_token,
2400 ident,
2401 generics,
2402 colon_token,
2403 ty,
2404 default,
2405 semi_token,
2406 }))
2407 } else {
2408 return Ok(TraitItem::Verbatim(verbatim::between(
2409 begin,
2410 input.cursor(),
2411 )));
2412 }
2413 } else if lookahead.peek(crate::token::AsyncToken![async])
2414 || lookahead.peek(crate::token::UnsafeToken![unsafe])
2415 || lookahead.peek(crate::token::ExternToken![extern])
2416 || lookahead.peek(crate::token::FnToken![fn])
2417 {
2418 input.parse().map(TraitItem::Fn)
2419 } else {
2420 Err(lookahead.error())
2421 }
2422 } else if lookahead.peek(crate::token::TypeToken![type]) {
2423 parse_trait_item_type(begin, input)
2424 } else if vis.is_inherited()
2425 && defaultness.is_none()
2426 && (lookahead.peek(Ident)
2427 || lookahead.peek(crate::token::SelfValueToken![self])
2428 || lookahead.peek(crate::token::SuperToken![super])
2429 || lookahead.peek(crate::token::CrateToken![crate])
2430 || lookahead.peek(crate::token::PathSepToken![::]))
2431 {
2432 input.parse().map(TraitItem::Macro)
2433 } else {
2434 Err(lookahead.error())
2435 }?;
2436
2437 match (vis, defaultness) {
2438 (Visibility::Inherited, None) => {}
2439 _ => {
2440 return Ok(TraitItem::Verbatim(verbatim::between(
2441 begin,
2442 input.cursor(),
2443 )))
2444 }
2445 }
2446
2447 let item_attrs = match &mut item {
2448 TraitItem::Const(item) => &mut item.attrs,
2449 TraitItem::Fn(item) => &mut item.attrs,
2450 TraitItem::Type(item) => &mut item.attrs,
2451 TraitItem::Macro(item) => &mut item.attrs,
2452 TraitItem::Verbatim(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
2453 };
2454 attrs.append(item_attrs);
2455 *item_attrs = attrs;
2456 Ok(item)
2457 }
2458 }
2459
2460 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2461 impl Parse for TraitItemConst {
2462 fn parse(input: ParseStream) -> Result<Self> {
2463 let attrs = input.call(Attribute::parse_outer)?;
2464 let const_token: crate::token::ConstToken![const] = input.parse()?;
2465
2466 let lookahead = input.lookahead1();
2467 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2468 input.call(Ident::parse_any)?
2469 } else {
2470 return Err(lookahead.error());
2471 };
2472
2473 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2474 let ty: Type = input.parse()?;
2475 let default = if input.peek(crate::token::EqToken![=]) {
2476 let eq_token: crate::token::EqToken![=] = input.parse()?;
2477 let default: Expr = input.parse()?;
2478 Some((eq_token, default))
2479 } else {
2480 None
2481 };
2482 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2483
2484 Ok(TraitItemConst {
2485 attrs,
2486 const_token,
2487 ident,
2488 generics: Generics::default(),
2489 colon_token,
2490 ty,
2491 default,
2492 semi_token,
2493 })
2494 }
2495 }
2496
2497 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2498 impl Parse for TraitItemFn {
2499 fn parse(input: ParseStream) -> Result<Self> {
2500 let mut attrs = input.call(Attribute::parse_outer)?;
2501 let sig: Signature = input.parse()?;
2502
2503 let lookahead = input.lookahead1();
2504 let (brace_token, stmts, semi_token) = if lookahead.peek(token::Brace) {
2505 let content;
2506 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);
2507 attr::parsing::parse_inner(&content, &mut attrs)?;
2508 let stmts = content.call(Block::parse_within)?;
2509 (Some(brace_token), stmts, None)
2510 } else if lookahead.peek(crate::token::SemiToken![;]) {
2511 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2512 (None, Vec::new(), Some(semi_token))
2513 } else {
2514 return Err(lookahead.error());
2515 };
2516
2517 Ok(TraitItemFn {
2518 attrs,
2519 sig,
2520 default: brace_token.map(|brace_token| Block { brace_token, stmts }),
2521 semi_token,
2522 })
2523 }
2524 }
2525
2526 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2527 impl Parse for TraitItemType {
2528 fn parse(input: ParseStream) -> Result<Self> {
2529 let attrs = input.call(Attribute::parse_outer)?;
2530 let type_token: crate::token::TypeToken![type] = input.parse()?;
2531 let ident: Ident = input.parse()?;
2532 let mut generics: Generics = input.parse()?;
2533 let (colon_token, bounds) = FlexibleItemType::parse_optional_bounds(input)?;
2534 let default = FlexibleItemType::parse_optional_definition(input)?;
2535 generics.where_clause = input.parse()?;
2536 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2537 Ok(TraitItemType {
2538 attrs,
2539 type_token,
2540 ident,
2541 generics,
2542 colon_token,
2543 bounds,
2544 default,
2545 semi_token,
2546 })
2547 }
2548 }
2549
2550 fn parse_trait_item_type(begin: Cursor, input: ParseStream) -> Result<TraitItem> {
2551 let FlexibleItemType {
2552 vis,
2553 defaultness: _,
2554 type_token,
2555 ident,
2556 generics,
2557 colon_token,
2558 bounds,
2559 ty,
2560 semi_token,
2561 } = FlexibleItemType::parse(
2562 input,
2563 TypeDefaultness::Disallowed,
2564 WhereClauseLocation::AfterEq,
2565 )?;
2566
2567 if vis.is_some() {
2568 Ok(TraitItem::Verbatim(verbatim::between(
2569 begin,
2570 input.cursor(),
2571 )))
2572 } else {
2573 Ok(TraitItem::Type(TraitItemType {
2574 attrs: Vec::new(),
2575 type_token,
2576 ident,
2577 generics,
2578 colon_token,
2579 bounds,
2580 default: ty,
2581 semi_token,
2582 }))
2583 }
2584 }
2585
2586 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2587 impl Parse for TraitItemMacro {
2588 fn parse(input: ParseStream) -> Result<Self> {
2589 let attrs = input.call(Attribute::parse_outer)?;
2590 let mac: Macro = input.parse()?;
2591 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2592 None
2593 } else {
2594 Some(input.parse()?)
2595 };
2596 Ok(TraitItemMacro {
2597 attrs,
2598 mac,
2599 semi_token,
2600 })
2601 }
2602 }
2603
2604 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2605 impl Parse for ItemImpl {
2606 fn parse(input: ParseStream) -> Result<Self> {
2607 let allow_verbatim_impl = false;
2608 parse_impl(input, allow_verbatim_impl).map(Option::unwrap)
2609 }
2610 }
2611
2612 fn parse_impl(input: ParseStream, allow_verbatim_impl: bool) -> Result<Option<ItemImpl>> {
2613 let mut attrs = input.call(Attribute::parse_outer)?;
2614 let has_visibility = allow_verbatim_impl && input.parse::<Visibility>()?.is_some();
2615 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2616 let unsafety: Option<crate::token::UnsafeToken![unsafe]> = input.parse()?;
2617 let impl_token: crate::token::ImplToken![impl] = input.parse()?;
2618
2619 let has_generics = generics::parsing::choose_generics_over_qpath(input);
2620 let mut generics: Generics = if has_generics {
2621 input.parse()?
2622 } else {
2623 Generics::default()
2624 };
2625
2626 let is_const_impl = allow_verbatim_impl
2627 && (input.peek(crate::token::ConstToken![const]) || input.peek(crate::token::QuestionToken![?]) && input.peek2(crate::token::ConstToken![const]));
2628 if is_const_impl {
2629 input.parse::<Option<crate::token::QuestionToken![?]>>()?;
2630 input.parse::<crate::token::ConstToken![const]>()?;
2631 }
2632
2633 let polarity = if input.peek(crate::token::NotToken![!]) && !input.peek2(token::Brace) {
2634 Some(input.parse::<crate::token::NotToken![!]>()?)
2635 } else {
2636 None
2637 };
2638
2639 #[cfg(not(feature = "printing"))]
2640 let first_ty_span = input.span();
2641 let mut first_ty: Type = input.parse()?;
2642 let self_ty: Type;
2643 let trait_;
2644
2645 let is_impl_for = input.peek(crate::token::ForToken![for]);
2646 if is_impl_for {
2647 let for_token: crate::token::ForToken![for] = input.parse()?;
2648 let mut first_ty_ref = &first_ty;
2649 while let Type::Group(ty) = first_ty_ref {
2650 first_ty_ref = &ty.elem;
2651 }
2652 if let Type::Path(TypePath { qself: None, .. }) = first_ty_ref {
2653 while let Type::Group(ty) = first_ty {
2654 first_ty = *ty.elem;
2655 }
2656 if let Type::Path(TypePath { qself: None, path }) = first_ty {
2657 trait_ = Some((polarity, path, for_token));
2658 } else {
2659 ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
2660 }
2661 } else if !allow_verbatim_impl {
2662 #[cfg(feature = "printing")]
2663 return Err(Error::new_spanned(first_ty_ref, "expected trait path"));
2664 #[cfg(not(feature = "printing"))]
2665 return Err(Error::new(first_ty_span, "expected trait path"));
2666 } else {
2667 trait_ = None;
2668 }
2669 self_ty = input.parse()?;
2670 } else if let Some(polarity) = polarity {
2671 return Err(Error::new(
2672 polarity.span,
2673 "inherent impls cannot be negative",
2674 ));
2675 } else {
2676 trait_ = None;
2677 self_ty = first_ty;
2678 }
2679
2680 generics.where_clause = input.parse()?;
2681
2682 let content;
2683 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);
2684 attr::parsing::parse_inner(&content, &mut attrs)?;
2685
2686 let mut items = Vec::new();
2687 while !content.is_empty() {
2688 items.push(content.parse()?);
2689 }
2690
2691 if has_visibility || is_const_impl || is_impl_for && trait_.is_none() {
2692 Ok(None)
2693 } else {
2694 Ok(Some(ItemImpl {
2695 attrs,
2696 defaultness,
2697 unsafety,
2698 impl_token,
2699 generics,
2700 trait_,
2701 self_ty: Box::new(self_ty),
2702 brace_token,
2703 items,
2704 }))
2705 }
2706 }
2707
2708 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2709 impl Parse for ImplItem {
2710 fn parse(input: ParseStream) -> Result<Self> {
2711 let begin = input.cursor();
2712 let mut attrs = input.call(Attribute::parse_outer)?;
2713 let ahead = input.fork();
2714 let vis: Visibility = ahead.parse()?;
2715
2716 let mut lookahead = ahead.lookahead1();
2717 let defaultness = if lookahead.peek(crate::token::DefaultToken![default]) && !ahead.peek2(crate::token::NotToken![!]) {
2718 let defaultness: crate::token::DefaultToken![default] = ahead.parse()?;
2719 lookahead = ahead.lookahead1();
2720 Some(defaultness)
2721 } else {
2722 None
2723 };
2724
2725 let allow_safe = false;
2726 let mut item = if lookahead.peek(crate::token::FnToken![fn]) || peek_signature(&ahead, allow_safe) {
2727 let allow_omitted_body = true;
2728 if let Some(item) = parse_impl_item_fn(input, allow_omitted_body)? {
2729 Ok(ImplItem::Fn(item))
2730 } else {
2731 Ok(ImplItem::Verbatim(verbatim::between(begin, input.cursor())))
2732 }
2733 } else if lookahead.peek(crate::token::ConstToken![const]) {
2734 input.advance_to(&ahead);
2735 let const_token: crate::token::ConstToken![const] = input.parse()?;
2736 let lookahead = input.lookahead1();
2737 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2738 input.call(Ident::parse_any)?
2739 } else {
2740 return Err(lookahead.error());
2741 };
2742 let mut generics: Generics = input.parse()?;
2743 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2744 let ty: Type = input.parse()?;
2745 let value = if let Some(eq_token) = input.parse::<Option<crate::token::EqToken![=]>>()? {
2746 let expr: Expr = input.parse()?;
2747 Some((eq_token, expr))
2748 } else {
2749 None
2750 };
2751 generics.where_clause = input.parse()?;
2752 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2753 return match value {
2754 Some((eq_token, expr))
2755 if generics.lt_token.is_none() && generics.where_clause.is_none() =>
2756 {
2757 Ok(ImplItem::Const(ImplItemConst {
2758 attrs,
2759 vis,
2760 defaultness,
2761 const_token,
2762 ident,
2763 generics,
2764 colon_token,
2765 ty,
2766 eq_token,
2767 expr,
2768 semi_token,
2769 }))
2770 }
2771 _ => Ok(ImplItem::Verbatim(verbatim::between(begin, input.cursor()))),
2772 };
2773 } else if lookahead.peek(crate::token::TypeToken![type]) {
2774 parse_impl_item_type(begin, input)
2775 } else if vis.is_inherited()
2776 && defaultness.is_none()
2777 && (lookahead.peek(Ident)
2778 || lookahead.peek(crate::token::SelfValueToken![self])
2779 || lookahead.peek(crate::token::SuperToken![super])
2780 || lookahead.peek(crate::token::CrateToken![crate])
2781 || lookahead.peek(crate::token::PathSepToken![::]))
2782 {
2783 input.parse().map(ImplItem::Macro)
2784 } else {
2785 Err(lookahead.error())
2786 }?;
2787
2788 {
2789 let item_attrs = match &mut item {
2790 ImplItem::Const(item) => &mut item.attrs,
2791 ImplItem::Fn(item) => &mut item.attrs,
2792 ImplItem::Type(item) => &mut item.attrs,
2793 ImplItem::Macro(item) => &mut item.attrs,
2794 ImplItem::Verbatim(_) => return Ok(item),
2795 };
2796 attrs.append(item_attrs);
2797 *item_attrs = attrs;
2798 }
2799
2800 Ok(item)
2801 }
2802 }
2803
2804 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2805 impl Parse for ImplItemConst {
2806 fn parse(input: ParseStream) -> Result<Self> {
2807 let attrs = input.call(Attribute::parse_outer)?;
2808 let vis: Visibility = input.parse()?;
2809 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2810 let const_token: crate::token::ConstToken![const] = input.parse()?;
2811
2812 let lookahead = input.lookahead1();
2813 let ident = if lookahead.peek(Ident) || lookahead.peek(crate::token::UnderscoreToken![_]) {
2814 input.call(Ident::parse_any)?
2815 } else {
2816 return Err(lookahead.error());
2817 };
2818
2819 let colon_token: crate::token::ColonToken![:] = input.parse()?;
2820 let ty: Type = input.parse()?;
2821 let eq_token: crate::token::EqToken![=] = input.parse()?;
2822 let expr: Expr = input.parse()?;
2823 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2824
2825 Ok(ImplItemConst {
2826 attrs,
2827 vis,
2828 defaultness,
2829 const_token,
2830 ident,
2831 generics: Generics::default(),
2832 colon_token,
2833 ty,
2834 eq_token,
2835 expr,
2836 semi_token,
2837 })
2838 }
2839 }
2840
2841 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2842 impl Parse for ImplItemFn {
2843 fn parse(input: ParseStream) -> Result<Self> {
2844 let allow_omitted_body = false;
2845 parse_impl_item_fn(input, allow_omitted_body).map(Option::unwrap)
2846 }
2847 }
2848
2849 fn parse_impl_item_fn(
2850 input: ParseStream,
2851 allow_omitted_body: bool,
2852 ) -> Result<Option<ImplItemFn>> {
2853 let mut attrs = input.call(Attribute::parse_outer)?;
2854 let vis: Visibility = input.parse()?;
2855 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2856 let sig: Signature = input.parse()?;
2857
2858 if allow_omitted_body && input.parse::<Option<crate::token::SemiToken![;]>>()?.is_some() {
2862 return Ok(None);
2863 }
2864
2865 let content;
2866 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);
2867 attrs.extend(content.call(Attribute::parse_inner)?);
2868 let block = Block {
2869 brace_token,
2870 stmts: content.call(Block::parse_within)?,
2871 };
2872
2873 Ok(Some(ImplItemFn {
2874 attrs,
2875 vis,
2876 defaultness,
2877 sig,
2878 block,
2879 }))
2880 }
2881
2882 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2883 impl Parse for ImplItemType {
2884 fn parse(input: ParseStream) -> Result<Self> {
2885 let attrs = input.call(Attribute::parse_outer)?;
2886 let vis: Visibility = input.parse()?;
2887 let defaultness: Option<crate::token::DefaultToken![default]> = input.parse()?;
2888 let type_token: crate::token::TypeToken![type] = input.parse()?;
2889 let ident: Ident = input.parse()?;
2890 let mut generics: Generics = input.parse()?;
2891 let eq_token: crate::token::EqToken![=] = input.parse()?;
2892 let ty: Type = input.parse()?;
2893 generics.where_clause = input.parse()?;
2894 let semi_token: crate::token::SemiToken![;] = input.parse()?;
2895 Ok(ImplItemType {
2896 attrs,
2897 vis,
2898 defaultness,
2899 type_token,
2900 ident,
2901 generics,
2902 eq_token,
2903 ty,
2904 semi_token,
2905 })
2906 }
2907 }
2908
2909 fn parse_impl_item_type(begin: Cursor, input: ParseStream) -> Result<ImplItem> {
2910 let FlexibleItemType {
2911 vis,
2912 defaultness,
2913 type_token,
2914 ident,
2915 generics,
2916 colon_token,
2917 bounds: _,
2918 ty,
2919 semi_token,
2920 } = FlexibleItemType::parse(
2921 input,
2922 TypeDefaultness::Optional,
2923 WhereClauseLocation::AfterEq,
2924 )?;
2925
2926 let (eq_token, ty) = match ty {
2927 Some(ty) if colon_token.is_none() => ty,
2928 _ => return Ok(ImplItem::Verbatim(verbatim::between(begin, input.cursor()))),
2929 };
2930
2931 Ok(ImplItem::Type(ImplItemType {
2932 attrs: Vec::new(),
2933 vis,
2934 defaultness,
2935 type_token,
2936 ident,
2937 generics,
2938 eq_token,
2939 ty,
2940 semi_token,
2941 }))
2942 }
2943
2944 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2945 impl Parse for ImplItemMacro {
2946 fn parse(input: ParseStream) -> Result<Self> {
2947 let attrs = input.call(Attribute::parse_outer)?;
2948 let mac: Macro = input.parse()?;
2949 let semi_token: Option<crate::token::SemiToken![;]> = if mac.delimiter.is_brace() {
2950 None
2951 } else {
2952 Some(input.parse()?)
2953 };
2954 Ok(ImplItemMacro {
2955 attrs,
2956 mac,
2957 semi_token,
2958 })
2959 }
2960 }
2961
2962 impl Visibility {
2963 fn is_inherited(&self) -> bool {
2964 match self {
2965 Visibility::Inherited => true,
2966 _ => false,
2967 }
2968 }
2969 }
2970
2971 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
2972 impl Parse for StaticMutability {
2973 fn parse(input: ParseStream) -> Result<Self> {
2974 let mut_token: Option<crate::token::MutToken![mut]> = input.parse()?;
2975 Ok(mut_token.map_or(StaticMutability::None, StaticMutability::Mut))
2976 }
2977 }
2978}
2979
2980#[cfg(feature = "printing")]
2981mod printing {
2982 use crate::attr::FilterAttrs;
2983 use crate::data::Fields;
2984 use crate::item::{
2985 ForeignItemFn, ForeignItemMacro, ForeignItemStatic, ForeignItemType, ImplItemConst,
2986 ImplItemFn, ImplItemMacro, ImplItemType, ItemConst, ItemEnum, ItemExternCrate, ItemFn,
2987 ItemForeignMod, ItemImpl, ItemMacro, ItemMod, ItemStatic, ItemStruct, ItemTrait,
2988 ItemTraitAlias, ItemType, ItemUnion, ItemUse, Receiver, Signature, StaticMutability,
2989 TraitItemConst, TraitItemFn, TraitItemMacro, TraitItemType, UseGlob, UseGroup, UseName,
2990 UsePath, UseRename, Variadic,
2991 };
2992 use crate::mac::MacroDelimiter;
2993 use crate::path;
2994 use crate::path::printing::PathStyle;
2995 use crate::print::TokensOrDefault;
2996 use crate::ty::Type;
2997 use proc_macro2::TokenStream;
2998 use quote::{ToTokens, TokenStreamExt as _};
2999
3000 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3001 impl ToTokens for ItemExternCrate {
3002 fn to_tokens(&self, tokens: &mut TokenStream) {
3003 tokens.append_all(self.attrs.outer());
3004 self.vis.to_tokens(tokens);
3005 self.extern_token.to_tokens(tokens);
3006 self.crate_token.to_tokens(tokens);
3007 self.ident.to_tokens(tokens);
3008 if let Some((as_token, rename)) = &self.rename {
3009 as_token.to_tokens(tokens);
3010 rename.to_tokens(tokens);
3011 }
3012 self.semi_token.to_tokens(tokens);
3013 }
3014 }
3015
3016 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3017 impl ToTokens for ItemUse {
3018 fn to_tokens(&self, tokens: &mut TokenStream) {
3019 tokens.append_all(self.attrs.outer());
3020 self.vis.to_tokens(tokens);
3021 self.use_token.to_tokens(tokens);
3022 self.leading_colon.to_tokens(tokens);
3023 self.tree.to_tokens(tokens);
3024 self.semi_token.to_tokens(tokens);
3025 }
3026 }
3027
3028 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3029 impl ToTokens for ItemStatic {
3030 fn to_tokens(&self, tokens: &mut TokenStream) {
3031 tokens.append_all(self.attrs.outer());
3032 self.vis.to_tokens(tokens);
3033 self.static_token.to_tokens(tokens);
3034 self.mutability.to_tokens(tokens);
3035 self.ident.to_tokens(tokens);
3036 self.colon_token.to_tokens(tokens);
3037 self.ty.to_tokens(tokens);
3038 self.eq_token.to_tokens(tokens);
3039 self.expr.to_tokens(tokens);
3040 self.semi_token.to_tokens(tokens);
3041 }
3042 }
3043
3044 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3045 impl ToTokens for ItemConst {
3046 fn to_tokens(&self, tokens: &mut TokenStream) {
3047 tokens.append_all(self.attrs.outer());
3048 self.vis.to_tokens(tokens);
3049 self.const_token.to_tokens(tokens);
3050 self.ident.to_tokens(tokens);
3051 self.colon_token.to_tokens(tokens);
3052 self.ty.to_tokens(tokens);
3053 self.eq_token.to_tokens(tokens);
3054 self.expr.to_tokens(tokens);
3055 self.semi_token.to_tokens(tokens);
3056 }
3057 }
3058
3059 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3060 impl ToTokens for ItemFn {
3061 fn to_tokens(&self, tokens: &mut TokenStream) {
3062 tokens.append_all(self.attrs.outer());
3063 self.vis.to_tokens(tokens);
3064 self.sig.to_tokens(tokens);
3065 self.block.brace_token.surround(tokens, |tokens| {
3066 tokens.append_all(self.attrs.inner());
3067 tokens.append_all(&self.block.stmts);
3068 });
3069 }
3070 }
3071
3072 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3073 impl ToTokens for ItemMod {
3074 fn to_tokens(&self, tokens: &mut TokenStream) {
3075 tokens.append_all(self.attrs.outer());
3076 self.vis.to_tokens(tokens);
3077 self.unsafety.to_tokens(tokens);
3078 self.mod_token.to_tokens(tokens);
3079 self.ident.to_tokens(tokens);
3080 if let Some((brace, items)) = &self.content {
3081 brace.surround(tokens, |tokens| {
3082 tokens.append_all(self.attrs.inner());
3083 tokens.append_all(items);
3084 });
3085 } else {
3086 TokensOrDefault(&self.semi).to_tokens(tokens);
3087 }
3088 }
3089 }
3090
3091 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3092 impl ToTokens for ItemForeignMod {
3093 fn to_tokens(&self, tokens: &mut TokenStream) {
3094 tokens.append_all(self.attrs.outer());
3095 self.unsafety.to_tokens(tokens);
3096 self.abi.to_tokens(tokens);
3097 self.brace_token.surround(tokens, |tokens| {
3098 tokens.append_all(self.attrs.inner());
3099 tokens.append_all(&self.items);
3100 });
3101 }
3102 }
3103
3104 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3105 impl ToTokens for ItemType {
3106 fn to_tokens(&self, tokens: &mut TokenStream) {
3107 tokens.append_all(self.attrs.outer());
3108 self.vis.to_tokens(tokens);
3109 self.type_token.to_tokens(tokens);
3110 self.ident.to_tokens(tokens);
3111 self.generics.to_tokens(tokens);
3112 self.generics.where_clause.to_tokens(tokens);
3113 self.eq_token.to_tokens(tokens);
3114 self.ty.to_tokens(tokens);
3115 self.semi_token.to_tokens(tokens);
3116 }
3117 }
3118
3119 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3120 impl ToTokens for ItemEnum {
3121 fn to_tokens(&self, tokens: &mut TokenStream) {
3122 tokens.append_all(self.attrs.outer());
3123 self.vis.to_tokens(tokens);
3124 self.enum_token.to_tokens(tokens);
3125 self.ident.to_tokens(tokens);
3126 self.generics.to_tokens(tokens);
3127 self.generics.where_clause.to_tokens(tokens);
3128 self.brace_token.surround(tokens, |tokens| {
3129 self.variants.to_tokens(tokens);
3130 });
3131 }
3132 }
3133
3134 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3135 impl ToTokens for ItemStruct {
3136 fn to_tokens(&self, tokens: &mut TokenStream) {
3137 tokens.append_all(self.attrs.outer());
3138 self.vis.to_tokens(tokens);
3139 self.struct_token.to_tokens(tokens);
3140 self.ident.to_tokens(tokens);
3141 self.generics.to_tokens(tokens);
3142 match &self.fields {
3143 Fields::Named(fields) => {
3144 self.generics.where_clause.to_tokens(tokens);
3145 fields.to_tokens(tokens);
3146 }
3147 Fields::Unnamed(fields) => {
3148 fields.to_tokens(tokens);
3149 self.generics.where_clause.to_tokens(tokens);
3150 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3151 }
3152 Fields::Unit => {
3153 self.generics.where_clause.to_tokens(tokens);
3154 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3155 }
3156 }
3157 }
3158 }
3159
3160 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3161 impl ToTokens for ItemUnion {
3162 fn to_tokens(&self, tokens: &mut TokenStream) {
3163 tokens.append_all(self.attrs.outer());
3164 self.vis.to_tokens(tokens);
3165 self.union_token.to_tokens(tokens);
3166 self.ident.to_tokens(tokens);
3167 self.generics.to_tokens(tokens);
3168 self.generics.where_clause.to_tokens(tokens);
3169 self.fields.to_tokens(tokens);
3170 }
3171 }
3172
3173 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3174 impl ToTokens for ItemTrait {
3175 fn to_tokens(&self, tokens: &mut TokenStream) {
3176 tokens.append_all(self.attrs.outer());
3177 self.vis.to_tokens(tokens);
3178 self.unsafety.to_tokens(tokens);
3179 self.auto_token.to_tokens(tokens);
3180 self.trait_token.to_tokens(tokens);
3181 self.ident.to_tokens(tokens);
3182 self.generics.to_tokens(tokens);
3183 if !self.supertraits.is_empty() {
3184 TokensOrDefault(&self.colon_token).to_tokens(tokens);
3185 self.supertraits.to_tokens(tokens);
3186 }
3187 self.generics.where_clause.to_tokens(tokens);
3188 self.brace_token.surround(tokens, |tokens| {
3189 tokens.append_all(self.attrs.inner());
3190 tokens.append_all(&self.items);
3191 });
3192 }
3193 }
3194
3195 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3196 impl ToTokens for ItemTraitAlias {
3197 fn to_tokens(&self, tokens: &mut TokenStream) {
3198 tokens.append_all(self.attrs.outer());
3199 self.vis.to_tokens(tokens);
3200 self.trait_token.to_tokens(tokens);
3201 self.ident.to_tokens(tokens);
3202 self.generics.to_tokens(tokens);
3203 self.eq_token.to_tokens(tokens);
3204 self.bounds.to_tokens(tokens);
3205 self.generics.where_clause.to_tokens(tokens);
3206 self.semi_token.to_tokens(tokens);
3207 }
3208 }
3209
3210 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3211 impl ToTokens for ItemImpl {
3212 fn to_tokens(&self, tokens: &mut TokenStream) {
3213 tokens.append_all(self.attrs.outer());
3214 self.defaultness.to_tokens(tokens);
3215 self.unsafety.to_tokens(tokens);
3216 self.impl_token.to_tokens(tokens);
3217 self.generics.to_tokens(tokens);
3218 if let Some((polarity, path, for_token)) = &self.trait_ {
3219 polarity.to_tokens(tokens);
3220 path.to_tokens(tokens);
3221 for_token.to_tokens(tokens);
3222 }
3223 self.self_ty.to_tokens(tokens);
3224 self.generics.where_clause.to_tokens(tokens);
3225 self.brace_token.surround(tokens, |tokens| {
3226 tokens.append_all(self.attrs.inner());
3227 tokens.append_all(&self.items);
3228 });
3229 }
3230 }
3231
3232 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3233 impl ToTokens for ItemMacro {
3234 fn to_tokens(&self, tokens: &mut TokenStream) {
3235 tokens.append_all(self.attrs.outer());
3236 path::printing::print_path(tokens, &self.mac.path, PathStyle::Mod);
3237 self.mac.bang_token.to_tokens(tokens);
3238 self.ident.to_tokens(tokens);
3239 match &self.mac.delimiter {
3240 MacroDelimiter::Paren(paren) => {
3241 paren.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3242 }
3243 MacroDelimiter::Brace(brace) => {
3244 brace.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3245 }
3246 MacroDelimiter::Bracket(bracket) => {
3247 bracket.surround(tokens, |tokens| self.mac.tokens.to_tokens(tokens));
3248 }
3249 }
3250 self.semi_token.to_tokens(tokens);
3251 }
3252 }
3253
3254 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3255 impl ToTokens for UsePath {
3256 fn to_tokens(&self, tokens: &mut TokenStream) {
3257 self.ident.to_tokens(tokens);
3258 self.colon2_token.to_tokens(tokens);
3259 self.tree.to_tokens(tokens);
3260 }
3261 }
3262
3263 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3264 impl ToTokens for UseName {
3265 fn to_tokens(&self, tokens: &mut TokenStream) {
3266 self.ident.to_tokens(tokens);
3267 }
3268 }
3269
3270 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3271 impl ToTokens for UseRename {
3272 fn to_tokens(&self, tokens: &mut TokenStream) {
3273 self.ident.to_tokens(tokens);
3274 self.as_token.to_tokens(tokens);
3275 self.rename.to_tokens(tokens);
3276 }
3277 }
3278
3279 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3280 impl ToTokens for UseGlob {
3281 fn to_tokens(&self, tokens: &mut TokenStream) {
3282 self.star_token.to_tokens(tokens);
3283 }
3284 }
3285
3286 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3287 impl ToTokens for UseGroup {
3288 fn to_tokens(&self, tokens: &mut TokenStream) {
3289 self.brace_token.surround(tokens, |tokens| {
3290 self.items.to_tokens(tokens);
3291 });
3292 }
3293 }
3294
3295 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3296 impl ToTokens for TraitItemConst {
3297 fn to_tokens(&self, tokens: &mut TokenStream) {
3298 tokens.append_all(self.attrs.outer());
3299 self.const_token.to_tokens(tokens);
3300 self.ident.to_tokens(tokens);
3301 self.colon_token.to_tokens(tokens);
3302 self.ty.to_tokens(tokens);
3303 if let Some((eq_token, default)) = &self.default {
3304 eq_token.to_tokens(tokens);
3305 default.to_tokens(tokens);
3306 }
3307 self.semi_token.to_tokens(tokens);
3308 }
3309 }
3310
3311 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3312 impl ToTokens for TraitItemFn {
3313 fn to_tokens(&self, tokens: &mut TokenStream) {
3314 tokens.append_all(self.attrs.outer());
3315 self.sig.to_tokens(tokens);
3316 match &self.default {
3317 Some(block) => {
3318 block.brace_token.surround(tokens, |tokens| {
3319 tokens.append_all(self.attrs.inner());
3320 tokens.append_all(&block.stmts);
3321 });
3322 }
3323 None => {
3324 TokensOrDefault(&self.semi_token).to_tokens(tokens);
3325 }
3326 }
3327 }
3328 }
3329
3330 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3331 impl ToTokens for TraitItemType {
3332 fn to_tokens(&self, tokens: &mut TokenStream) {
3333 tokens.append_all(self.attrs.outer());
3334 self.type_token.to_tokens(tokens);
3335 self.ident.to_tokens(tokens);
3336 self.generics.to_tokens(tokens);
3337 if !self.bounds.is_empty() {
3338 TokensOrDefault(&self.colon_token).to_tokens(tokens);
3339 self.bounds.to_tokens(tokens);
3340 }
3341 if let Some((eq_token, default)) = &self.default {
3342 eq_token.to_tokens(tokens);
3343 default.to_tokens(tokens);
3344 }
3345 self.generics.where_clause.to_tokens(tokens);
3346 self.semi_token.to_tokens(tokens);
3347 }
3348 }
3349
3350 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3351 impl ToTokens for TraitItemMacro {
3352 fn to_tokens(&self, tokens: &mut TokenStream) {
3353 tokens.append_all(self.attrs.outer());
3354 self.mac.to_tokens(tokens);
3355 self.semi_token.to_tokens(tokens);
3356 }
3357 }
3358
3359 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3360 impl ToTokens for ImplItemConst {
3361 fn to_tokens(&self, tokens: &mut TokenStream) {
3362 tokens.append_all(self.attrs.outer());
3363 self.vis.to_tokens(tokens);
3364 self.defaultness.to_tokens(tokens);
3365 self.const_token.to_tokens(tokens);
3366 self.ident.to_tokens(tokens);
3367 self.colon_token.to_tokens(tokens);
3368 self.ty.to_tokens(tokens);
3369 self.eq_token.to_tokens(tokens);
3370 self.expr.to_tokens(tokens);
3371 self.semi_token.to_tokens(tokens);
3372 }
3373 }
3374
3375 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3376 impl ToTokens for ImplItemFn {
3377 fn to_tokens(&self, tokens: &mut TokenStream) {
3378 tokens.append_all(self.attrs.outer());
3379 self.vis.to_tokens(tokens);
3380 self.defaultness.to_tokens(tokens);
3381 self.sig.to_tokens(tokens);
3382 self.block.brace_token.surround(tokens, |tokens| {
3383 tokens.append_all(self.attrs.inner());
3384 tokens.append_all(&self.block.stmts);
3385 });
3386 }
3387 }
3388
3389 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3390 impl ToTokens for ImplItemType {
3391 fn to_tokens(&self, tokens: &mut TokenStream) {
3392 tokens.append_all(self.attrs.outer());
3393 self.vis.to_tokens(tokens);
3394 self.defaultness.to_tokens(tokens);
3395 self.type_token.to_tokens(tokens);
3396 self.ident.to_tokens(tokens);
3397 self.generics.to_tokens(tokens);
3398 self.eq_token.to_tokens(tokens);
3399 self.ty.to_tokens(tokens);
3400 self.generics.where_clause.to_tokens(tokens);
3401 self.semi_token.to_tokens(tokens);
3402 }
3403 }
3404
3405 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3406 impl ToTokens for ImplItemMacro {
3407 fn to_tokens(&self, tokens: &mut TokenStream) {
3408 tokens.append_all(self.attrs.outer());
3409 self.mac.to_tokens(tokens);
3410 self.semi_token.to_tokens(tokens);
3411 }
3412 }
3413
3414 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3415 impl ToTokens for ForeignItemFn {
3416 fn to_tokens(&self, tokens: &mut TokenStream) {
3417 tokens.append_all(self.attrs.outer());
3418 self.vis.to_tokens(tokens);
3419 self.sig.to_tokens(tokens);
3420 self.semi_token.to_tokens(tokens);
3421 }
3422 }
3423
3424 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3425 impl ToTokens for ForeignItemStatic {
3426 fn to_tokens(&self, tokens: &mut TokenStream) {
3427 tokens.append_all(self.attrs.outer());
3428 self.vis.to_tokens(tokens);
3429 self.static_token.to_tokens(tokens);
3430 self.mutability.to_tokens(tokens);
3431 self.ident.to_tokens(tokens);
3432 self.colon_token.to_tokens(tokens);
3433 self.ty.to_tokens(tokens);
3434 self.semi_token.to_tokens(tokens);
3435 }
3436 }
3437
3438 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3439 impl ToTokens for ForeignItemType {
3440 fn to_tokens(&self, tokens: &mut TokenStream) {
3441 tokens.append_all(self.attrs.outer());
3442 self.vis.to_tokens(tokens);
3443 self.type_token.to_tokens(tokens);
3444 self.ident.to_tokens(tokens);
3445 self.generics.to_tokens(tokens);
3446 self.generics.where_clause.to_tokens(tokens);
3447 self.semi_token.to_tokens(tokens);
3448 }
3449 }
3450
3451 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3452 impl ToTokens for ForeignItemMacro {
3453 fn to_tokens(&self, tokens: &mut TokenStream) {
3454 tokens.append_all(self.attrs.outer());
3455 self.mac.to_tokens(tokens);
3456 self.semi_token.to_tokens(tokens);
3457 }
3458 }
3459
3460 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3461 impl ToTokens for Signature {
3462 fn to_tokens(&self, tokens: &mut TokenStream) {
3463 self.constness.to_tokens(tokens);
3464 self.asyncness.to_tokens(tokens);
3465 self.unsafety.to_tokens(tokens);
3466 self.abi.to_tokens(tokens);
3467 self.fn_token.to_tokens(tokens);
3468 self.ident.to_tokens(tokens);
3469 self.generics.to_tokens(tokens);
3470 self.paren_token.surround(tokens, |tokens| {
3471 self.inputs.to_tokens(tokens);
3472 if let Some(variadic) = &self.variadic {
3473 if !self.inputs.empty_or_trailing() {
3474 <crate::token::CommaToken![,]>::default().to_tokens(tokens);
3475 }
3476 variadic.to_tokens(tokens);
3477 }
3478 });
3479 self.output.to_tokens(tokens);
3480 self.generics.where_clause.to_tokens(tokens);
3481 }
3482 }
3483
3484 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3485 impl ToTokens for Receiver {
3486 fn to_tokens(&self, tokens: &mut TokenStream) {
3487 tokens.append_all(self.attrs.outer());
3488 if let Some((ampersand, lifetime)) = &self.reference {
3489 ampersand.to_tokens(tokens);
3490 lifetime.to_tokens(tokens);
3491 }
3492 self.mutability.to_tokens(tokens);
3493 self.self_token.to_tokens(tokens);
3494 if let Some(colon_token) = &self.colon_token {
3495 colon_token.to_tokens(tokens);
3496 self.ty.to_tokens(tokens);
3497 } else {
3498 let consistent = match (&self.reference, &self.mutability, &*self.ty) {
3499 (Some(_), mutability, Type::Reference(ty)) => {
3500 mutability.is_some() == ty.mutability.is_some()
3501 && match &*ty.elem {
3502 Type::Path(ty) => ty.qself.is_none() && ty.path.is_ident("Self"),
3503 _ => false,
3504 }
3505 }
3506 (None, _, Type::Path(ty)) => ty.qself.is_none() && ty.path.is_ident("Self"),
3507 _ => false,
3508 };
3509 if !consistent {
3510 <crate::token::ColonToken![:]>::default().to_tokens(tokens);
3511 self.ty.to_tokens(tokens);
3512 }
3513 }
3514 }
3515 }
3516
3517 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3518 impl ToTokens for Variadic {
3519 fn to_tokens(&self, tokens: &mut TokenStream) {
3520 tokens.append_all(self.attrs.outer());
3521 if let Some((pat, colon)) = &self.pat {
3522 pat.to_tokens(tokens);
3523 colon.to_tokens(tokens);
3524 }
3525 self.dots.to_tokens(tokens);
3526 self.comma.to_tokens(tokens);
3527 }
3528 }
3529
3530 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
3531 impl ToTokens for StaticMutability {
3532 fn to_tokens(&self, tokens: &mut TokenStream) {
3533 match self {
3534 StaticMutability::None => {}
3535 StaticMutability::Mut(mut_token) => mut_token.to_tokens(tokens),
3536 }
3537 }
3538 }
3539}