]> git.proxmox.com Git - cargo.git/blob - vendor/syn/src/generics.rs
New upstream version 0.52.0
[cargo.git] / vendor / syn / src / generics.rs
1 use super::*;
2 use crate::punctuated::{Iter, IterMut, Punctuated};
3 #[cfg(all(feature = "printing", feature = "extra-traits"))]
4 use std::fmt::{self, Debug};
5 #[cfg(all(feature = "printing", feature = "extra-traits"))]
6 use std::hash::{Hash, Hasher};
7
8 ast_struct! {
9 /// Lifetimes and type parameters attached to a declaration of a function,
10 /// enum, trait, etc.
11 ///
12 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
13 /// feature.*
14 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
15 pub struct Generics {
16 pub lt_token: Option<Token![<]>,
17 pub params: Punctuated<GenericParam, Token![,]>,
18 pub gt_token: Option<Token![>]>,
19 pub where_clause: Option<WhereClause>,
20 }
21 }
22
23 ast_enum_of_structs! {
24 /// A generic type parameter, lifetime, or const generic: `T: Into<String>`,
25 /// `'a: 'b`, `const LEN: usize`.
26 ///
27 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
28 /// feature.*
29 ///
30 /// # Syntax tree enum
31 ///
32 /// This type is a [syntax tree enum].
33 ///
34 /// [syntax tree enum]: Expr#syntax-tree-enums
35 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
36 pub enum GenericParam {
37 /// A generic type parameter: `T: Into<String>`.
38 Type(TypeParam),
39
40 /// A lifetime definition: `'a: 'b + 'c + 'd`.
41 Lifetime(LifetimeDef),
42
43 /// A const generic parameter: `const LENGTH: usize`.
44 Const(ConstParam),
45 }
46 }
47
48 ast_struct! {
49 /// A generic type parameter: `T: Into<String>`.
50 ///
51 /// *This type is available only if Syn is built with the `"derive"` or
52 /// `"full"` feature.*
53 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
54 pub struct TypeParam {
55 pub attrs: Vec<Attribute>,
56 pub ident: Ident,
57 pub colon_token: Option<Token![:]>,
58 pub bounds: Punctuated<TypeParamBound, Token![+]>,
59 pub eq_token: Option<Token![=]>,
60 pub default: Option<Type>,
61 }
62 }
63
64 ast_struct! {
65 /// A lifetime definition: `'a: 'b + 'c + 'd`.
66 ///
67 /// *This type is available only if Syn is built with the `"derive"` or
68 /// `"full"` feature.*
69 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
70 pub struct LifetimeDef {
71 pub attrs: Vec<Attribute>,
72 pub lifetime: Lifetime,
73 pub colon_token: Option<Token![:]>,
74 pub bounds: Punctuated<Lifetime, Token![+]>,
75 }
76 }
77
78 ast_struct! {
79 /// A const generic parameter: `const LENGTH: usize`.
80 ///
81 /// *This type is available only if Syn is built with the `"derive"` or
82 /// `"full"` feature.*
83 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
84 pub struct ConstParam {
85 pub attrs: Vec<Attribute>,
86 pub const_token: Token![const],
87 pub ident: Ident,
88 pub colon_token: Token![:],
89 pub ty: Type,
90 pub eq_token: Option<Token![=]>,
91 pub default: Option<Expr>,
92 }
93 }
94
95 impl Default for Generics {
96 fn default() -> Self {
97 Generics {
98 lt_token: None,
99 params: Punctuated::new(),
100 gt_token: None,
101 where_clause: None,
102 }
103 }
104 }
105
106 impl Generics {
107 /// Returns an
108 /// <code
109 /// style="padding-right:0;">Iterator&lt;Item = &amp;</code><a
110 /// href="struct.TypeParam.html"><code
111 /// style="padding-left:0;padding-right:0;">TypeParam</code></a><code
112 /// style="padding-left:0;">&gt;</code>
113 /// over the type parameters in `self.params`.
114 pub fn type_params(&self) -> TypeParams {
115 TypeParams(self.params.iter())
116 }
117
118 /// Returns an
119 /// <code
120 /// style="padding-right:0;">Iterator&lt;Item = &amp;mut </code><a
121 /// href="struct.TypeParam.html"><code
122 /// style="padding-left:0;padding-right:0;">TypeParam</code></a><code
123 /// style="padding-left:0;">&gt;</code>
124 /// over the type parameters in `self.params`.
125 pub fn type_params_mut(&mut self) -> TypeParamsMut {
126 TypeParamsMut(self.params.iter_mut())
127 }
128
129 /// Returns an
130 /// <code
131 /// style="padding-right:0;">Iterator&lt;Item = &amp;</code><a
132 /// href="struct.LifetimeDef.html"><code
133 /// style="padding-left:0;padding-right:0;">LifetimeDef</code></a><code
134 /// style="padding-left:0;">&gt;</code>
135 /// over the lifetime parameters in `self.params`.
136 pub fn lifetimes(&self) -> Lifetimes {
137 Lifetimes(self.params.iter())
138 }
139
140 /// Returns an
141 /// <code
142 /// style="padding-right:0;">Iterator&lt;Item = &amp;mut </code><a
143 /// href="struct.LifetimeDef.html"><code
144 /// style="padding-left:0;padding-right:0;">LifetimeDef</code></a><code
145 /// style="padding-left:0;">&gt;</code>
146 /// over the lifetime parameters in `self.params`.
147 pub fn lifetimes_mut(&mut self) -> LifetimesMut {
148 LifetimesMut(self.params.iter_mut())
149 }
150
151 /// Returns an
152 /// <code
153 /// style="padding-right:0;">Iterator&lt;Item = &amp;</code><a
154 /// href="struct.ConstParam.html"><code
155 /// style="padding-left:0;padding-right:0;">ConstParam</code></a><code
156 /// style="padding-left:0;">&gt;</code>
157 /// over the constant parameters in `self.params`.
158 pub fn const_params(&self) -> ConstParams {
159 ConstParams(self.params.iter())
160 }
161
162 /// Returns an
163 /// <code
164 /// style="padding-right:0;">Iterator&lt;Item = &amp;mut </code><a
165 /// href="struct.ConstParam.html"><code
166 /// style="padding-left:0;padding-right:0;">ConstParam</code></a><code
167 /// style="padding-left:0;">&gt;</code>
168 /// over the constant parameters in `self.params`.
169 pub fn const_params_mut(&mut self) -> ConstParamsMut {
170 ConstParamsMut(self.params.iter_mut())
171 }
172
173 /// Initializes an empty `where`-clause if there is not one present already.
174 pub fn make_where_clause(&mut self) -> &mut WhereClause {
175 self.where_clause.get_or_insert_with(|| WhereClause {
176 where_token: <Token![where]>::default(),
177 predicates: Punctuated::new(),
178 })
179 }
180 }
181
182 pub struct TypeParams<'a>(Iter<'a, GenericParam>);
183
184 impl<'a> Iterator for TypeParams<'a> {
185 type Item = &'a TypeParam;
186
187 fn next(&mut self) -> Option<Self::Item> {
188 let next = match self.0.next() {
189 Some(item) => item,
190 None => return None,
191 };
192 if let GenericParam::Type(type_param) = next {
193 Some(type_param)
194 } else {
195 self.next()
196 }
197 }
198 }
199
200 pub struct TypeParamsMut<'a>(IterMut<'a, GenericParam>);
201
202 impl<'a> Iterator for TypeParamsMut<'a> {
203 type Item = &'a mut TypeParam;
204
205 fn next(&mut self) -> Option<Self::Item> {
206 let next = match self.0.next() {
207 Some(item) => item,
208 None => return None,
209 };
210 if let GenericParam::Type(type_param) = next {
211 Some(type_param)
212 } else {
213 self.next()
214 }
215 }
216 }
217
218 pub struct Lifetimes<'a>(Iter<'a, GenericParam>);
219
220 impl<'a> Iterator for Lifetimes<'a> {
221 type Item = &'a LifetimeDef;
222
223 fn next(&mut self) -> Option<Self::Item> {
224 let next = match self.0.next() {
225 Some(item) => item,
226 None => return None,
227 };
228 if let GenericParam::Lifetime(lifetime) = next {
229 Some(lifetime)
230 } else {
231 self.next()
232 }
233 }
234 }
235
236 pub struct LifetimesMut<'a>(IterMut<'a, GenericParam>);
237
238 impl<'a> Iterator for LifetimesMut<'a> {
239 type Item = &'a mut LifetimeDef;
240
241 fn next(&mut self) -> Option<Self::Item> {
242 let next = match self.0.next() {
243 Some(item) => item,
244 None => return None,
245 };
246 if let GenericParam::Lifetime(lifetime) = next {
247 Some(lifetime)
248 } else {
249 self.next()
250 }
251 }
252 }
253
254 pub struct ConstParams<'a>(Iter<'a, GenericParam>);
255
256 impl<'a> Iterator for ConstParams<'a> {
257 type Item = &'a ConstParam;
258
259 fn next(&mut self) -> Option<Self::Item> {
260 let next = match self.0.next() {
261 Some(item) => item,
262 None => return None,
263 };
264 if let GenericParam::Const(const_param) = next {
265 Some(const_param)
266 } else {
267 self.next()
268 }
269 }
270 }
271
272 pub struct ConstParamsMut<'a>(IterMut<'a, GenericParam>);
273
274 impl<'a> Iterator for ConstParamsMut<'a> {
275 type Item = &'a mut ConstParam;
276
277 fn next(&mut self) -> Option<Self::Item> {
278 let next = match self.0.next() {
279 Some(item) => item,
280 None => return None,
281 };
282 if let GenericParam::Const(const_param) = next {
283 Some(const_param)
284 } else {
285 self.next()
286 }
287 }
288 }
289
290 /// Returned by `Generics::split_for_impl`.
291 ///
292 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
293 /// feature and the `"printing"` feature.*
294 #[cfg(feature = "printing")]
295 #[cfg_attr(
296 doc_cfg,
297 doc(cfg(all(any(feature = "full", feature = "derive"), feature = "printing")))
298 )]
299 pub struct ImplGenerics<'a>(&'a Generics);
300
301 /// Returned by `Generics::split_for_impl`.
302 ///
303 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
304 /// feature and the `"printing"` feature.*
305 #[cfg(feature = "printing")]
306 #[cfg_attr(
307 doc_cfg,
308 doc(cfg(all(any(feature = "full", feature = "derive"), feature = "printing")))
309 )]
310 pub struct TypeGenerics<'a>(&'a Generics);
311
312 /// Returned by `TypeGenerics::as_turbofish`.
313 ///
314 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
315 /// feature and the `"printing"` feature.*
316 #[cfg(feature = "printing")]
317 #[cfg_attr(
318 doc_cfg,
319 doc(cfg(all(any(feature = "full", feature = "derive"), feature = "printing")))
320 )]
321 pub struct Turbofish<'a>(&'a Generics);
322
323 #[cfg(feature = "printing")]
324 impl Generics {
325 /// Split a type's generics into the pieces required for impl'ing a trait
326 /// for that type.
327 ///
328 /// ```
329 /// # use proc_macro2::{Span, Ident};
330 /// # use quote::quote;
331 /// #
332 /// # let generics: syn::Generics = Default::default();
333 /// # let name = Ident::new("MyType", Span::call_site());
334 /// #
335 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
336 /// quote! {
337 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
338 /// // ...
339 /// }
340 /// }
341 /// # ;
342 /// ```
343 ///
344 /// *This method is available only if Syn is built with the `"derive"` or
345 /// `"full"` feature and the `"printing"` feature.*
346 #[cfg_attr(
347 doc_cfg,
348 doc(cfg(all(any(feature = "full", feature = "derive"), feature = "printing")))
349 )]
350 pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) {
351 (
352 ImplGenerics(self),
353 TypeGenerics(self),
354 self.where_clause.as_ref(),
355 )
356 }
357 }
358
359 #[cfg(feature = "printing")]
360 macro_rules! generics_wrapper_impls {
361 ($ty:ident) => {
362 #[cfg(feature = "clone-impls")]
363 #[cfg_attr(doc_cfg, doc(cfg(feature = "clone-impls")))]
364 impl<'a> Clone for $ty<'a> {
365 fn clone(&self) -> Self {
366 $ty(self.0)
367 }
368 }
369
370 #[cfg(feature = "extra-traits")]
371 #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
372 impl<'a> Debug for $ty<'a> {
373 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
374 formatter
375 .debug_tuple(stringify!($ty))
376 .field(self.0)
377 .finish()
378 }
379 }
380
381 #[cfg(feature = "extra-traits")]
382 #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
383 impl<'a> Eq for $ty<'a> {}
384
385 #[cfg(feature = "extra-traits")]
386 #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
387 impl<'a> PartialEq for $ty<'a> {
388 fn eq(&self, other: &Self) -> bool {
389 self.0 == other.0
390 }
391 }
392
393 #[cfg(feature = "extra-traits")]
394 #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
395 impl<'a> Hash for $ty<'a> {
396 fn hash<H: Hasher>(&self, state: &mut H) {
397 self.0.hash(state);
398 }
399 }
400 };
401 }
402
403 #[cfg(feature = "printing")]
404 generics_wrapper_impls!(ImplGenerics);
405 #[cfg(feature = "printing")]
406 generics_wrapper_impls!(TypeGenerics);
407 #[cfg(feature = "printing")]
408 generics_wrapper_impls!(Turbofish);
409
410 #[cfg(feature = "printing")]
411 impl<'a> TypeGenerics<'a> {
412 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
413 ///
414 /// *This method is available only if Syn is built with the `"derive"` or
415 /// `"full"` feature and the `"printing"` feature.*
416 pub fn as_turbofish(&self) -> Turbofish {
417 Turbofish(self.0)
418 }
419 }
420
421 ast_struct! {
422 /// A set of bound lifetimes: `for<'a, 'b, 'c>`.
423 ///
424 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
425 /// feature.*
426 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
427 pub struct BoundLifetimes {
428 pub for_token: Token![for],
429 pub lt_token: Token![<],
430 pub lifetimes: Punctuated<LifetimeDef, Token![,]>,
431 pub gt_token: Token![>],
432 }
433 }
434
435 impl Default for BoundLifetimes {
436 fn default() -> Self {
437 BoundLifetimes {
438 for_token: Default::default(),
439 lt_token: Default::default(),
440 lifetimes: Punctuated::new(),
441 gt_token: Default::default(),
442 }
443 }
444 }
445
446 impl LifetimeDef {
447 pub fn new(lifetime: Lifetime) -> Self {
448 LifetimeDef {
449 attrs: Vec::new(),
450 lifetime,
451 colon_token: None,
452 bounds: Punctuated::new(),
453 }
454 }
455 }
456
457 impl From<Ident> for TypeParam {
458 fn from(ident: Ident) -> Self {
459 TypeParam {
460 attrs: vec![],
461 ident,
462 colon_token: None,
463 bounds: Punctuated::new(),
464 eq_token: None,
465 default: None,
466 }
467 }
468 }
469
470 ast_enum_of_structs! {
471 /// A trait or lifetime used as a bound on a type parameter.
472 ///
473 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
474 /// feature.*
475 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
476 pub enum TypeParamBound {
477 Trait(TraitBound),
478 Lifetime(Lifetime),
479 }
480 }
481
482 ast_struct! {
483 /// A trait used as a bound on a type parameter.
484 ///
485 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
486 /// feature.*
487 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
488 pub struct TraitBound {
489 pub paren_token: Option<token::Paren>,
490 pub modifier: TraitBoundModifier,
491 /// The `for<'a>` in `for<'a> Foo<&'a T>`
492 pub lifetimes: Option<BoundLifetimes>,
493 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
494 pub path: Path,
495 }
496 }
497
498 ast_enum! {
499 /// A modifier on a trait bound, currently only used for the `?` in
500 /// `?Sized`.
501 ///
502 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
503 /// feature.*
504 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
505 pub enum TraitBoundModifier {
506 None,
507 Maybe(Token![?]),
508 }
509 }
510
511 ast_struct! {
512 /// A `where` clause in a definition: `where T: Deserialize<'de>, D:
513 /// 'static`.
514 ///
515 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
516 /// feature.*
517 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
518 pub struct WhereClause {
519 pub where_token: Token![where],
520 pub predicates: Punctuated<WherePredicate, Token![,]>,
521 }
522 }
523
524 ast_enum_of_structs! {
525 /// A single predicate in a `where` clause: `T: Deserialize<'de>`.
526 ///
527 /// *This type is available only if Syn is built with the `"derive"` or `"full"`
528 /// feature.*
529 ///
530 /// # Syntax tree enum
531 ///
532 /// This type is a [syntax tree enum].
533 ///
534 /// [syntax tree enum]: Expr#syntax-tree-enums
535 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
536 pub enum WherePredicate {
537 /// A type predicate in a `where` clause: `for<'c> Foo<'c>: Trait<'c>`.
538 Type(PredicateType),
539
540 /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`.
541 Lifetime(PredicateLifetime),
542
543 /// An equality predicate in a `where` clause (unsupported).
544 Eq(PredicateEq),
545 }
546 }
547
548 ast_struct! {
549 /// A type predicate in a `where` clause: `for<'c> Foo<'c>: Trait<'c>`.
550 ///
551 /// *This type is available only if Syn is built with the `"derive"` or
552 /// `"full"` feature.*
553 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
554 pub struct PredicateType {
555 /// Any lifetimes from a `for` binding
556 pub lifetimes: Option<BoundLifetimes>,
557 /// The type being bounded
558 pub bounded_ty: Type,
559 pub colon_token: Token![:],
560 /// Trait and lifetime bounds (`Clone+Send+'static`)
561 pub bounds: Punctuated<TypeParamBound, Token![+]>,
562 }
563 }
564
565 ast_struct! {
566 /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`.
567 ///
568 /// *This type is available only if Syn is built with the `"derive"` or
569 /// `"full"` feature.*
570 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
571 pub struct PredicateLifetime {
572 pub lifetime: Lifetime,
573 pub colon_token: Token![:],
574 pub bounds: Punctuated<Lifetime, Token![+]>,
575 }
576 }
577
578 ast_struct! {
579 /// An equality predicate in a `where` clause (unsupported).
580 ///
581 /// *This type is available only if Syn is built with the `"derive"` or
582 /// `"full"` feature.*
583 #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))]
584 pub struct PredicateEq {
585 pub lhs_ty: Type,
586 pub eq_token: Token![=],
587 pub rhs_ty: Type,
588 }
589 }
590
591 #[cfg(feature = "parsing")]
592 pub mod parsing {
593 use super::*;
594 use crate::ext::IdentExt;
595 use crate::parse::{Parse, ParseStream, Result};
596
597 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
598 impl Parse for Generics {
599 fn parse(input: ParseStream) -> Result<Self> {
600 if !input.peek(Token![<]) {
601 return Ok(Generics::default());
602 }
603
604 let lt_token: Token![<] = input.parse()?;
605
606 let mut params = Punctuated::new();
607 loop {
608 if input.peek(Token![>]) {
609 break;
610 }
611
612 let attrs = input.call(Attribute::parse_outer)?;
613 let lookahead = input.lookahead1();
614 if lookahead.peek(Lifetime) {
615 params.push_value(GenericParam::Lifetime(LifetimeDef {
616 attrs,
617 ..input.parse()?
618 }));
619 } else if lookahead.peek(Ident) {
620 params.push_value(GenericParam::Type(TypeParam {
621 attrs,
622 ..input.parse()?
623 }));
624 } else if lookahead.peek(Token![const]) {
625 params.push_value(GenericParam::Const(ConstParam {
626 attrs,
627 ..input.parse()?
628 }));
629 } else if input.peek(Token![_]) {
630 params.push_value(GenericParam::Type(TypeParam {
631 attrs,
632 ident: input.call(Ident::parse_any)?,
633 colon_token: None,
634 bounds: Punctuated::new(),
635 eq_token: None,
636 default: None,
637 }));
638 } else {
639 return Err(lookahead.error());
640 }
641
642 if input.peek(Token![>]) {
643 break;
644 }
645 let punct = input.parse()?;
646 params.push_punct(punct);
647 }
648
649 let gt_token: Token![>] = input.parse()?;
650
651 Ok(Generics {
652 lt_token: Some(lt_token),
653 params,
654 gt_token: Some(gt_token),
655 where_clause: None,
656 })
657 }
658 }
659
660 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
661 impl Parse for GenericParam {
662 fn parse(input: ParseStream) -> Result<Self> {
663 let attrs = input.call(Attribute::parse_outer)?;
664
665 let lookahead = input.lookahead1();
666 if lookahead.peek(Ident) {
667 Ok(GenericParam::Type(TypeParam {
668 attrs,
669 ..input.parse()?
670 }))
671 } else if lookahead.peek(Lifetime) {
672 Ok(GenericParam::Lifetime(LifetimeDef {
673 attrs,
674 ..input.parse()?
675 }))
676 } else if lookahead.peek(Token![const]) {
677 Ok(GenericParam::Const(ConstParam {
678 attrs,
679 ..input.parse()?
680 }))
681 } else {
682 Err(lookahead.error())
683 }
684 }
685 }
686
687 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
688 impl Parse for LifetimeDef {
689 fn parse(input: ParseStream) -> Result<Self> {
690 let has_colon;
691 Ok(LifetimeDef {
692 attrs: input.call(Attribute::parse_outer)?,
693 lifetime: input.parse()?,
694 colon_token: {
695 if input.peek(Token![:]) {
696 has_colon = true;
697 Some(input.parse()?)
698 } else {
699 has_colon = false;
700 None
701 }
702 },
703 bounds: {
704 let mut bounds = Punctuated::new();
705 if has_colon {
706 loop {
707 if input.peek(Token![,]) || input.peek(Token![>]) {
708 break;
709 }
710 let value = input.parse()?;
711 bounds.push_value(value);
712 if !input.peek(Token![+]) {
713 break;
714 }
715 let punct = input.parse()?;
716 bounds.push_punct(punct);
717 }
718 }
719 bounds
720 },
721 })
722 }
723 }
724
725 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
726 impl Parse for BoundLifetimes {
727 fn parse(input: ParseStream) -> Result<Self> {
728 Ok(BoundLifetimes {
729 for_token: input.parse()?,
730 lt_token: input.parse()?,
731 lifetimes: {
732 let mut lifetimes = Punctuated::new();
733 while !input.peek(Token![>]) {
734 lifetimes.push_value(input.parse()?);
735 if input.peek(Token![>]) {
736 break;
737 }
738 lifetimes.push_punct(input.parse()?);
739 }
740 lifetimes
741 },
742 gt_token: input.parse()?,
743 })
744 }
745 }
746
747 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
748 impl Parse for Option<BoundLifetimes> {
749 fn parse(input: ParseStream) -> Result<Self> {
750 if input.peek(Token![for]) {
751 input.parse().map(Some)
752 } else {
753 Ok(None)
754 }
755 }
756 }
757
758 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
759 impl Parse for TypeParam {
760 fn parse(input: ParseStream) -> Result<Self> {
761 let attrs = input.call(Attribute::parse_outer)?;
762 let ident: Ident = input.parse()?;
763 let colon_token: Option<Token![:]> = input.parse()?;
764
765 let begin_bound = input.fork();
766 let mut is_maybe_const = false;
767 let mut bounds = Punctuated::new();
768 if colon_token.is_some() {
769 loop {
770 if input.peek(Token![,]) || input.peek(Token![>]) || input.peek(Token![=]) {
771 break;
772 }
773 if input.peek(Token![?]) && input.peek2(Token![const]) {
774 input.parse::<Token![?]>()?;
775 input.parse::<Token![const]>()?;
776 is_maybe_const = true;
777 }
778 let value: TypeParamBound = input.parse()?;
779 bounds.push_value(value);
780 if !input.peek(Token![+]) {
781 break;
782 }
783 let punct: Token![+] = input.parse()?;
784 bounds.push_punct(punct);
785 }
786 }
787
788 let mut eq_token: Option<Token![=]> = input.parse()?;
789 let mut default = if eq_token.is_some() {
790 Some(input.parse::<Type>()?)
791 } else {
792 None
793 };
794
795 if is_maybe_const {
796 bounds.clear();
797 eq_token = None;
798 default = Some(Type::Verbatim(verbatim::between(begin_bound, input)));
799 }
800
801 Ok(TypeParam {
802 attrs,
803 ident,
804 colon_token,
805 bounds,
806 eq_token,
807 default,
808 })
809 }
810 }
811
812 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
813 impl Parse for TypeParamBound {
814 fn parse(input: ParseStream) -> Result<Self> {
815 if input.peek(Lifetime) {
816 return input.parse().map(TypeParamBound::Lifetime);
817 }
818
819 if input.peek(token::Paren) {
820 let content;
821 let paren_token = parenthesized!(content in input);
822 let mut bound: TraitBound = content.parse()?;
823 bound.paren_token = Some(paren_token);
824 return Ok(TypeParamBound::Trait(bound));
825 }
826
827 input.parse().map(TypeParamBound::Trait)
828 }
829 }
830
831 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
832 impl Parse for TraitBound {
833 fn parse(input: ParseStream) -> Result<Self> {
834 let modifier: TraitBoundModifier = input.parse()?;
835 let lifetimes: Option<BoundLifetimes> = input.parse()?;
836
837 let mut path: Path = input.parse()?;
838 if path.segments.last().unwrap().arguments.is_empty() && input.peek(token::Paren) {
839 let parenthesized = PathArguments::Parenthesized(input.parse()?);
840 path.segments.last_mut().unwrap().arguments = parenthesized;
841 }
842
843 Ok(TraitBound {
844 paren_token: None,
845 modifier,
846 lifetimes,
847 path,
848 })
849 }
850 }
851
852 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
853 impl Parse for TraitBoundModifier {
854 fn parse(input: ParseStream) -> Result<Self> {
855 if input.peek(Token![?]) {
856 input.parse().map(TraitBoundModifier::Maybe)
857 } else {
858 Ok(TraitBoundModifier::None)
859 }
860 }
861 }
862
863 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
864 impl Parse for ConstParam {
865 fn parse(input: ParseStream) -> Result<Self> {
866 let mut default = None;
867 Ok(ConstParam {
868 attrs: input.call(Attribute::parse_outer)?,
869 const_token: input.parse()?,
870 ident: input.parse()?,
871 colon_token: input.parse()?,
872 ty: input.parse()?,
873 eq_token: {
874 if input.peek(Token![=]) {
875 let eq_token = input.parse()?;
876 default = Some(path::parsing::const_argument(input)?);
877 Some(eq_token)
878 } else {
879 None
880 }
881 },
882 default,
883 })
884 }
885 }
886
887 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
888 impl Parse for WhereClause {
889 fn parse(input: ParseStream) -> Result<Self> {
890 Ok(WhereClause {
891 where_token: input.parse()?,
892 predicates: {
893 let mut predicates = Punctuated::new();
894 loop {
895 if input.is_empty()
896 || input.peek(token::Brace)
897 || input.peek(Token![,])
898 || input.peek(Token![;])
899 || input.peek(Token![:]) && !input.peek(Token![::])
900 || input.peek(Token![=])
901 {
902 break;
903 }
904 let value = input.parse()?;
905 predicates.push_value(value);
906 if !input.peek(Token![,]) {
907 break;
908 }
909 let punct = input.parse()?;
910 predicates.push_punct(punct);
911 }
912 predicates
913 },
914 })
915 }
916 }
917
918 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
919 impl Parse for Option<WhereClause> {
920 fn parse(input: ParseStream) -> Result<Self> {
921 if input.peek(Token![where]) {
922 input.parse().map(Some)
923 } else {
924 Ok(None)
925 }
926 }
927 }
928
929 #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
930 impl Parse for WherePredicate {
931 fn parse(input: ParseStream) -> Result<Self> {
932 if input.peek(Lifetime) && input.peek2(Token![:]) {
933 Ok(WherePredicate::Lifetime(PredicateLifetime {
934 lifetime: input.parse()?,
935 colon_token: input.parse()?,
936 bounds: {
937 let mut bounds = Punctuated::new();
938 loop {
939 if input.is_empty()
940 || input.peek(token::Brace)
941 || input.peek(Token![,])
942 || input.peek(Token![;])
943 || input.peek(Token![:])
944 || input.peek(Token![=])
945 {
946 break;
947 }
948 let value = input.parse()?;
949 bounds.push_value(value);
950 if !input.peek(Token![+]) {
951 break;
952 }
953 let punct = input.parse()?;
954 bounds.push_punct(punct);
955 }
956 bounds
957 },
958 }))
959 } else {
960 Ok(WherePredicate::Type(PredicateType {
961 lifetimes: input.parse()?,
962 bounded_ty: input.parse()?,
963 colon_token: input.parse()?,
964 bounds: {
965 let mut bounds = Punctuated::new();
966 loop {
967 if input.is_empty()
968 || input.peek(token::Brace)
969 || input.peek(Token![,])
970 || input.peek(Token![;])
971 || input.peek(Token![:]) && !input.peek(Token![::])
972 || input.peek(Token![=])
973 {
974 break;
975 }
976 let value = input.parse()?;
977 bounds.push_value(value);
978 if !input.peek(Token![+]) {
979 break;
980 }
981 let punct = input.parse()?;
982 bounds.push_punct(punct);
983 }
984 bounds
985 },
986 }))
987 }
988 }
989 }
990 }
991
992 #[cfg(feature = "printing")]
993 mod printing {
994 use super::*;
995 use crate::attr::FilterAttrs;
996 use crate::print::TokensOrDefault;
997 use proc_macro2::TokenStream;
998 #[cfg(feature = "full")]
999 use proc_macro2::TokenTree;
1000 use quote::{ToTokens, TokenStreamExt};
1001
1002 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1003 impl ToTokens for Generics {
1004 fn to_tokens(&self, tokens: &mut TokenStream) {
1005 if self.params.is_empty() {
1006 return;
1007 }
1008
1009 TokensOrDefault(&self.lt_token).to_tokens(tokens);
1010
1011 // Print lifetimes before types and consts, regardless of their
1012 // order in self.params.
1013 //
1014 // TODO: ordering rules for const parameters vs type parameters have
1015 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
1016 let mut trailing_or_empty = true;
1017 for param in self.params.pairs() {
1018 if let GenericParam::Lifetime(_) = **param.value() {
1019 param.to_tokens(tokens);
1020 trailing_or_empty = param.punct().is_some();
1021 }
1022 }
1023 for param in self.params.pairs() {
1024 match **param.value() {
1025 GenericParam::Type(_) | GenericParam::Const(_) => {
1026 if !trailing_or_empty {
1027 <Token![,]>::default().to_tokens(tokens);
1028 trailing_or_empty = true;
1029 }
1030 param.to_tokens(tokens);
1031 }
1032 GenericParam::Lifetime(_) => {}
1033 }
1034 }
1035
1036 TokensOrDefault(&self.gt_token).to_tokens(tokens);
1037 }
1038 }
1039
1040 impl<'a> ToTokens for ImplGenerics<'a> {
1041 fn to_tokens(&self, tokens: &mut TokenStream) {
1042 if self.0.params.is_empty() {
1043 return;
1044 }
1045
1046 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
1047
1048 // Print lifetimes before types and consts, regardless of their
1049 // order in self.params.
1050 //
1051 // TODO: ordering rules for const parameters vs type parameters have
1052 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
1053 let mut trailing_or_empty = true;
1054 for param in self.0.params.pairs() {
1055 if let GenericParam::Lifetime(_) = **param.value() {
1056 param.to_tokens(tokens);
1057 trailing_or_empty = param.punct().is_some();
1058 }
1059 }
1060 for param in self.0.params.pairs() {
1061 if let GenericParam::Lifetime(_) = **param.value() {
1062 continue;
1063 }
1064 if !trailing_or_empty {
1065 <Token![,]>::default().to_tokens(tokens);
1066 trailing_or_empty = true;
1067 }
1068 match *param.value() {
1069 GenericParam::Lifetime(_) => unreachable!(),
1070 GenericParam::Type(param) => {
1071 // Leave off the type parameter defaults
1072 tokens.append_all(param.attrs.outer());
1073 param.ident.to_tokens(tokens);
1074 if !param.bounds.is_empty() {
1075 TokensOrDefault(&param.colon_token).to_tokens(tokens);
1076 param.bounds.to_tokens(tokens);
1077 }
1078 }
1079 GenericParam::Const(param) => {
1080 // Leave off the const parameter defaults
1081 tokens.append_all(param.attrs.outer());
1082 param.const_token.to_tokens(tokens);
1083 param.ident.to_tokens(tokens);
1084 param.colon_token.to_tokens(tokens);
1085 param.ty.to_tokens(tokens);
1086 }
1087 }
1088 param.punct().to_tokens(tokens);
1089 }
1090
1091 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
1092 }
1093 }
1094
1095 impl<'a> ToTokens for TypeGenerics<'a> {
1096 fn to_tokens(&self, tokens: &mut TokenStream) {
1097 if self.0.params.is_empty() {
1098 return;
1099 }
1100
1101 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
1102
1103 // Print lifetimes before types and consts, regardless of their
1104 // order in self.params.
1105 //
1106 // TODO: ordering rules for const parameters vs type parameters have
1107 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
1108 let mut trailing_or_empty = true;
1109 for param in self.0.params.pairs() {
1110 if let GenericParam::Lifetime(def) = *param.value() {
1111 // Leave off the lifetime bounds and attributes
1112 def.lifetime.to_tokens(tokens);
1113 param.punct().to_tokens(tokens);
1114 trailing_or_empty = param.punct().is_some();
1115 }
1116 }
1117 for param in self.0.params.pairs() {
1118 if let GenericParam::Lifetime(_) = **param.value() {
1119 continue;
1120 }
1121 if !trailing_or_empty {
1122 <Token![,]>::default().to_tokens(tokens);
1123 trailing_or_empty = true;
1124 }
1125 match *param.value() {
1126 GenericParam::Lifetime(_) => unreachable!(),
1127 GenericParam::Type(param) => {
1128 // Leave off the type parameter defaults
1129 param.ident.to_tokens(tokens);
1130 }
1131 GenericParam::Const(param) => {
1132 // Leave off the const parameter defaults
1133 param.ident.to_tokens(tokens);
1134 }
1135 }
1136 param.punct().to_tokens(tokens);
1137 }
1138
1139 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
1140 }
1141 }
1142
1143 impl<'a> ToTokens for Turbofish<'a> {
1144 fn to_tokens(&self, tokens: &mut TokenStream) {
1145 if !self.0.params.is_empty() {
1146 <Token![::]>::default().to_tokens(tokens);
1147 TypeGenerics(self.0).to_tokens(tokens);
1148 }
1149 }
1150 }
1151
1152 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1153 impl ToTokens for BoundLifetimes {
1154 fn to_tokens(&self, tokens: &mut TokenStream) {
1155 self.for_token.to_tokens(tokens);
1156 self.lt_token.to_tokens(tokens);
1157 self.lifetimes.to_tokens(tokens);
1158 self.gt_token.to_tokens(tokens);
1159 }
1160 }
1161
1162 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1163 impl ToTokens for LifetimeDef {
1164 fn to_tokens(&self, tokens: &mut TokenStream) {
1165 tokens.append_all(self.attrs.outer());
1166 self.lifetime.to_tokens(tokens);
1167 if !self.bounds.is_empty() {
1168 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1169 self.bounds.to_tokens(tokens);
1170 }
1171 }
1172 }
1173
1174 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1175 impl ToTokens for TypeParam {
1176 fn to_tokens(&self, tokens: &mut TokenStream) {
1177 tokens.append_all(self.attrs.outer());
1178 self.ident.to_tokens(tokens);
1179 if !self.bounds.is_empty() {
1180 TokensOrDefault(&self.colon_token).to_tokens(tokens);
1181 self.bounds.to_tokens(tokens);
1182 }
1183 if let Some(default) = &self.default {
1184 #[cfg(feature = "full")]
1185 {
1186 if self.eq_token.is_none() {
1187 if let Type::Verbatim(default) = default {
1188 let mut iter = default.clone().into_iter().peekable();
1189 while let Some(token) = iter.next() {
1190 if let TokenTree::Punct(q) = token {
1191 if q.as_char() == '?' {
1192 if let Some(TokenTree::Ident(c)) = iter.peek() {
1193 if c == "const" {
1194 if self.bounds.is_empty() {
1195 TokensOrDefault(&self.colon_token)
1196 .to_tokens(tokens);
1197 }
1198 return default.to_tokens(tokens);
1199 }
1200 }
1201 }
1202 }
1203 }
1204 }
1205 }
1206 }
1207 TokensOrDefault(&self.eq_token).to_tokens(tokens);
1208 default.to_tokens(tokens);
1209 }
1210 }
1211 }
1212
1213 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1214 impl ToTokens for TraitBound {
1215 fn to_tokens(&self, tokens: &mut TokenStream) {
1216 let to_tokens = |tokens: &mut TokenStream| {
1217 self.modifier.to_tokens(tokens);
1218 self.lifetimes.to_tokens(tokens);
1219 self.path.to_tokens(tokens);
1220 };
1221 match &self.paren_token {
1222 Some(paren) => paren.surround(tokens, to_tokens),
1223 None => to_tokens(tokens),
1224 }
1225 }
1226 }
1227
1228 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1229 impl ToTokens for TraitBoundModifier {
1230 fn to_tokens(&self, tokens: &mut TokenStream) {
1231 match self {
1232 TraitBoundModifier::None => {}
1233 TraitBoundModifier::Maybe(t) => t.to_tokens(tokens),
1234 }
1235 }
1236 }
1237
1238 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1239 impl ToTokens for ConstParam {
1240 fn to_tokens(&self, tokens: &mut TokenStream) {
1241 tokens.append_all(self.attrs.outer());
1242 self.const_token.to_tokens(tokens);
1243 self.ident.to_tokens(tokens);
1244 self.colon_token.to_tokens(tokens);
1245 self.ty.to_tokens(tokens);
1246 if let Some(default) = &self.default {
1247 TokensOrDefault(&self.eq_token).to_tokens(tokens);
1248 default.to_tokens(tokens);
1249 }
1250 }
1251 }
1252
1253 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1254 impl ToTokens for WhereClause {
1255 fn to_tokens(&self, tokens: &mut TokenStream) {
1256 if !self.predicates.is_empty() {
1257 self.where_token.to_tokens(tokens);
1258 self.predicates.to_tokens(tokens);
1259 }
1260 }
1261 }
1262
1263 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1264 impl ToTokens for PredicateType {
1265 fn to_tokens(&self, tokens: &mut TokenStream) {
1266 self.lifetimes.to_tokens(tokens);
1267 self.bounded_ty.to_tokens(tokens);
1268 self.colon_token.to_tokens(tokens);
1269 self.bounds.to_tokens(tokens);
1270 }
1271 }
1272
1273 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1274 impl ToTokens for PredicateLifetime {
1275 fn to_tokens(&self, tokens: &mut TokenStream) {
1276 self.lifetime.to_tokens(tokens);
1277 self.colon_token.to_tokens(tokens);
1278 self.bounds.to_tokens(tokens);
1279 }
1280 }
1281
1282 #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
1283 impl ToTokens for PredicateEq {
1284 fn to_tokens(&self, tokens: &mut TokenStream) {
1285 self.lhs_ty.to_tokens(tokens);
1286 self.eq_token.to_tokens(tokens);
1287 self.rhs_ty.to_tokens(tokens);
1288 }
1289 }
1290 }