blob: 0361befaa93a55d7dcffaa10780af9cc245d3380 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
Alex Crichtonccbb45d2017-05-23 10:58:24 -07002use delimited::Delimited;
David Tolnayb79ee962016-09-04 09:39:20 -07003
Alex Crichton62a0a592017-05-22 13:58:53 -07004ast_struct! {
5 /// Represents lifetimes and type parameters attached to a declaration
6 /// of a function, enum, trait, etc.
7 #[derive(Default)]
8 pub struct Generics {
David Tolnayf8db7ba2017-11-11 22:52:16 -08009 pub lt_token: Option<Token![<]>,
David Tolnayc2f1aba2017-11-12 20:29:22 -080010 pub params: Delimited<GenericParam, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080011 pub gt_token: Option<Token![>]>,
Alex Crichton62a0a592017-05-22 13:58:53 -070012 pub where_clause: WhereClause,
13 }
David Tolnayb79ee962016-09-04 09:39:20 -070014}
15
David Tolnayc2f1aba2017-11-12 20:29:22 -080016ast_enum_of_structs! {
17 pub enum GenericParam {
18 /// A lifetime definition, e.g. `'a: 'b+'c+'d`
19 pub Lifetime(LifetimeDef {
20 pub attrs: Vec<Attribute>,
21 pub lifetime: Lifetime,
22 pub colon_token: Option<Token![:]>,
23 pub bounds: Delimited<Lifetime, Token![+]>,
24 }),
25 /// A generic type parameter, e.g. `T: Into<String>`.
26 pub Type(TypeParam {
27 pub attrs: Vec<Attribute>,
28 pub ident: Ident,
29 pub colon_token: Option<Token![:]>,
30 pub bounds: Delimited<TypeParamBound, Token![+]>,
31 pub eq_token: Option<Token![=]>,
32 pub default: Option<Type>,
33 }),
Nika Layzellf1fdc0b2017-12-04 19:58:32 -050034 /// A generic const parameter, e.g. `const LENGTH: usize`.
35 pub Const(ConstParam {
36 pub attrs: Vec<Attribute>,
37 pub const_token: Token![const],
38 pub ident: Ident,
39 pub colon_token: Token![:],
40 pub ty: Type,
41 pub eq_token: Option<Token![=]>,
42 pub default: Option<Expr>,
43 }),
David Tolnayc2f1aba2017-11-12 20:29:22 -080044 }
45}
46
David Tolnaye7678922016-10-13 20:44:03 -070047#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040048#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
49#[cfg_attr(feature = "clone-impls", derive(Clone))]
50/// Returned by `Generics::split_for_impl`.
51pub struct ImplGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070052
53#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040054#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
55#[cfg_attr(feature = "clone-impls", derive(Clone))]
56/// Returned by `Generics::split_for_impl`.
David Tolnayfd6bf5c2017-11-12 09:41:14 -080057pub struct TypeGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -070058
David Tolnayc879a502017-01-25 15:51:32 -080059#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -040060#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
61#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayfd6bf5c2017-11-12 09:41:14 -080062/// Returned by `TypeGenerics::as_turbofish`.
Nika Layzell6b38b132017-10-24 23:09:39 -040063pub struct Turbofish<'a>(&'a Generics);
David Tolnayc879a502017-01-25 15:51:32 -080064
David Tolnaye95cc9f2017-01-25 15:57:09 -080065#[cfg(feature = "printing")]
David Tolnayb153dbc2016-10-04 23:39:10 -070066impl Generics {
67 /// Split a type's generics into the pieces required for impl'ing a trait
68 /// for that type.
69 ///
70 /// ```
71 /// # extern crate syn;
72 /// # #[macro_use]
73 /// # extern crate quote;
74 /// # fn main() {
75 /// # let generics: syn::Generics = Default::default();
Alex Crichtonccbb45d2017-05-23 10:58:24 -070076 /// # let name = syn::Ident::from("MyType");
David Tolnayb153dbc2016-10-04 23:39:10 -070077 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
78 /// quote! {
79 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
80 /// // ...
81 /// }
82 /// }
83 /// # ;
84 /// # }
85 /// ```
David Tolnayfd6bf5c2017-11-12 09:41:14 -080086 pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, &WhereClause) {
87 (ImplGenerics(self), TypeGenerics(self), &self.where_clause)
David Tolnayb153dbc2016-10-04 23:39:10 -070088 }
89}
90
David Tolnaye95cc9f2017-01-25 15:57:09 -080091#[cfg(feature = "printing")]
David Tolnayfd6bf5c2017-11-12 09:41:14 -080092impl<'a> TypeGenerics<'a> {
David Tolnayc879a502017-01-25 15:51:32 -080093 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
94 pub fn as_turbofish(&self) -> Turbofish {
95 Turbofish(self.0)
96 }
97}
98
Alex Crichton62a0a592017-05-22 13:58:53 -070099ast_struct! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700100 /// A set of bound lifetimes, e.g. `for<'a, 'b, 'c>`
101 #[derive(Default)]
102 pub struct BoundLifetimes {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800103 pub for_token: Token![for],
104 pub lt_token: Token![<],
105 pub lifetimes: Delimited<LifetimeDef, Token![,]>,
106 pub gt_token: Token![>],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700107 }
108}
109
David Tolnayf9505b52016-10-02 09:18:52 -0700110impl LifetimeDef {
David Tolnay63e3dee2017-06-03 20:13:17 -0700111 pub fn new(lifetime: Lifetime) -> Self {
David Tolnayf9505b52016-10-02 09:18:52 -0700112 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -0700113 attrs: Vec::new(),
David Tolnay63e3dee2017-06-03 20:13:17 -0700114 lifetime: lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700115 colon_token: None,
116 bounds: Delimited::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700117 }
118 }
119}
120
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800121impl From<Ident> for TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700122 fn from(ident: Ident) -> Self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800123 TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700124 attrs: vec![],
125 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700126 colon_token: None,
127 bounds: Delimited::new(),
128 eq_token: None,
Ted Driggs0547d0d2017-04-20 10:00:12 -0700129 default: None,
130 }
131 }
132}
133
Alex Crichton62a0a592017-05-22 13:58:53 -0700134ast_enum! {
135 /// The AST represents all type param bounds as types.
136 /// `typeck::collect::compute_bounds` matches these against
137 /// the "special" built-in traits (see `middle::lang_items`) and
138 /// detects Copy, Send and Sync.
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800139 pub enum TypeParamBound {
Alex Crichton62a0a592017-05-22 13:58:53 -0700140 Trait(PolyTraitRef, TraitBoundModifier),
141 Region(Lifetime),
142 }
David Tolnay55337722016-09-11 12:58:56 -0700143}
144
Alex Crichton62a0a592017-05-22 13:58:53 -0700145ast_enum! {
146 /// A modifier on a bound, currently this is only used for `?Sized`, where the
147 /// modifier is `Maybe`. Negative bounds should also be handled here.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700148 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700149 pub enum TraitBoundModifier {
150 None,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800151 Maybe(Token![?]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700152 }
David Tolnay55337722016-09-11 12:58:56 -0700153}
154
Alex Crichton62a0a592017-05-22 13:58:53 -0700155ast_struct! {
156 /// A `where` clause in a definition
157 #[derive(Default)]
158 pub struct WhereClause {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800159 pub where_token: Option<Token![where]>,
160 pub predicates: Delimited<WherePredicate, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700161 }
David Tolnayb79ee962016-09-04 09:39:20 -0700162}
163
David Tolnayb153dbc2016-10-04 23:39:10 -0700164impl WhereClause {
165 pub fn none() -> Self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700166 WhereClause::default()
David Tolnayb153dbc2016-10-04 23:39:10 -0700167 }
168}
169
Alex Crichton62a0a592017-05-22 13:58:53 -0700170ast_enum_of_structs! {
171 /// A single predicate in a `where` clause
172 pub enum WherePredicate {
173 /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c`
174 pub BoundPredicate(WhereBoundPredicate {
175 /// Any lifetimes from a `for` binding
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700176 pub bound_lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700177 /// The type being bounded
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800178 pub bounded_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800179 pub colon_token: Token![:],
Alex Crichton62a0a592017-05-22 13:58:53 -0700180 /// Trait and lifetime bounds (`Clone+Send+'static`)
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800181 pub bounds: Delimited<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700182 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700183
Alex Crichton62a0a592017-05-22 13:58:53 -0700184 /// A lifetime predicate, e.g. `'a: 'b+'c`
185 pub RegionPredicate(WhereRegionPredicate {
186 pub lifetime: Lifetime,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800187 pub colon_token: Option<Token![:]>,
188 pub bounds: Delimited<Lifetime, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700189 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700190
Alex Crichton62a0a592017-05-22 13:58:53 -0700191 /// An equality predicate (unsupported)
192 pub EqPredicate(WhereEqPredicate {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800193 pub lhs_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800194 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800195 pub rhs_ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700196 }),
197 }
David Tolnayf8e08832017-01-23 00:04:32 -0800198}
199
David Tolnay86eca752016-09-04 11:26:41 -0700200#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700201pub mod parsing {
202 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700203
David Tolnay63e3dee2017-06-03 20:13:17 -0700204 use synom::Synom;
David Tolnayc2f1aba2017-11-12 20:29:22 -0800205 use synom::delimited::Element;
Alex Crichton954046c2017-05-30 21:49:42 -0700206
207 impl Synom for Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400208 named!(parse -> Self, map!(
209 alt!(
210 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800211 lt: punct!(<) >>
David Tolnayc2f1aba2017-11-12 20:29:22 -0800212 lifetimes: call!(Delimited::<LifetimeDef, Token![,]>::parse_terminated) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400213 ty_params: cond!(
David Tolnayc2f1aba2017-11-12 20:29:22 -0800214 lifetimes.empty_or_trailing(),
215 call!(Delimited::<TypeParam, Token![,]>::parse_terminated)
Michael Layzell92639a52017-06-01 00:07:44 -0400216 ) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800217 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400218 (lifetimes, ty_params, Some(lt), Some(gt))
219 )
220 |
221 epsilon!() => { |_| (Delimited::new(), None, None, None) }
222 ),
David Tolnaybc7d7d92017-06-03 20:54:05 -0700223 |(lifetimes, ty_params, lt, gt)| Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400224 lt_token: lt,
David Tolnayc2f1aba2017-11-12 20:29:22 -0800225 params: lifetimes.into_iter()
226 .map(Element::into_tuple)
227 .map(|(lifetime, comma)| (GenericParam::Lifetime(lifetime), comma))
228 .chain(ty_params.unwrap_or_default()
229 .into_iter()
230 .map(Element::into_tuple)
231 .map(|(ty_param, comma)| (GenericParam::Type(ty_param), comma)))
232 .collect::<Vec<_>>()
233 .into(),
234 gt_token: gt,
235 where_clause: WhereClause::default(),
Michael Layzell416724e2017-05-24 21:12:34 -0400236 }
Michael Layzell92639a52017-06-01 00:07:44 -0400237 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700238 }
239
Alex Crichton954046c2017-05-30 21:49:42 -0700240 impl Synom for LifetimeDef {
Michael Layzell92639a52017-06-01 00:07:44 -0400241 named!(parse -> Self, do_parse!(
242 attrs: many0!(call!(Attribute::parse_outer)) >>
243 life: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800244 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400245 bounds: cond!(
246 colon.is_some(),
247 call!(Delimited::parse_separated_nonempty)
248 ) >>
249 (LifetimeDef {
250 attrs: attrs,
251 lifetime: life,
252 bounds: bounds.unwrap_or_default(),
David Tolnayf8db7ba2017-11-11 22:52:16 -0800253 colon_token: colon.map(|_| <Token![:]>::default()),
Michael Layzell92639a52017-06-01 00:07:44 -0400254 })
255 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700256 }
257
258 impl Synom for BoundLifetimes {
Michael Layzell92639a52017-06-01 00:07:44 -0400259 named!(parse -> Self, do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800260 for_: keyword!(for) >>
261 lt: punct!(<) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400262 lifetimes: call!(Delimited::parse_terminated) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800263 gt: punct!(>) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400264 (BoundLifetimes {
265 for_token: for_,
266 lt_token: lt,
267 gt_token: gt,
268 lifetimes: lifetimes,
269 })
270 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700271 }
David Tolnay55337722016-09-11 12:58:56 -0700272
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800273 impl Synom for TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400274 named!(parse -> Self, do_parse!(
275 attrs: many0!(call!(Attribute::parse_outer)) >>
276 id: syn!(Ident) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800277 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400278 bounds: cond!(
279 colon.is_some(),
280 call!(Delimited::parse_separated_nonempty)
281 ) >>
282 default: option!(do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800283 eq: punct!(=) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800284 ty: syn!(Type) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400285 (eq, ty)
286 )) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800287 (TypeParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400288 attrs: attrs,
289 ident: id,
290 bounds: bounds.unwrap_or_default(),
291 colon_token: colon,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800292 eq_token: default.as_ref().map(|d| Token![=]((d.0).0)),
Michael Layzell92639a52017-06-01 00:07:44 -0400293 default: default.map(|d| d.1),
294 })
295 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700296 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700297
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800298 impl Synom for TypeParamBound {
Michael Layzell92639a52017-06-01 00:07:44 -0400299 named!(parse -> Self, alt!(
300 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800301 question: punct!(?) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400302 poly: syn!(PolyTraitRef) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800303 (TypeParamBound::Trait(poly, TraitBoundModifier::Maybe(question)))
Michael Layzell92639a52017-06-01 00:07:44 -0400304 )
305 |
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800306 syn!(Lifetime) => { TypeParamBound::Region }
Michael Layzell92639a52017-06-01 00:07:44 -0400307 |
308 syn!(PolyTraitRef) => {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800309 |poly| TypeParamBound::Trait(poly, TraitBoundModifier::None)
Alex Crichton954046c2017-05-30 21:49:42 -0700310 }
David Tolnayd9faf442017-12-25 23:36:05 -0500311 |
312 parens!(syn!(PolyTraitRef)) => {
313 |poly| TypeParamBound::Trait(poly.0, TraitBoundModifier::None)
314 }
Michael Layzell92639a52017-06-01 00:07:44 -0400315 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700316
317 fn description() -> Option<&'static str> {
Nika Layzellc691cb42017-12-04 13:44:38 -0500318 Some("type parameter bound")
Alex Crichton954046c2017-05-30 21:49:42 -0700319 }
320 }
321
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500322 impl Synom for ConstParam {
323 named!(parse -> Self, do_parse!(
324 attrs: many0!(call!(Attribute::parse_outer)) >>
325 const_: keyword!(const) >>
326 ident: syn!(Ident) >>
327 colon: punct!(:) >>
328 ty: syn!(Type) >>
329 eq_def: option!(tuple!(punct!(=), syn!(Expr))) >>
David Tolnay78ee5202017-12-04 22:17:54 -0800330 ({
331 let (eq_token, default) = match eq_def {
332 Some((eq_token, default)) => (Some(eq_token), Some(default)),
333 None => (None, None),
334 };
335 ConstParam {
336 attrs: attrs,
337 const_token: const_,
338 ident: ident,
339 colon_token: colon,
340 ty: ty,
341 eq_token: eq_token,
342 default: default,
343 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500344 })
345 ));
346 }
347
Alex Crichton954046c2017-05-30 21:49:42 -0700348 impl Synom for WhereClause {
Michael Layzell92639a52017-06-01 00:07:44 -0400349 named!(parse -> Self, alt!(
350 do_parse!(
David Tolnayf8db7ba2017-11-11 22:52:16 -0800351 where_: keyword!(where) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400352 predicates: call!(Delimited::parse_terminated) >>
353 (WhereClause {
354 predicates: predicates,
355 where_token: Some(where_),
356 })
357 )
358 |
359 epsilon!() => { |_| WhereClause::default() }
360 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700361
362 fn description() -> Option<&'static str> {
363 Some("where clause")
364 }
365 }
366
367 impl Synom for WherePredicate {
Michael Layzell92639a52017-06-01 00:07:44 -0400368 named!(parse -> Self, alt!(
369 do_parse!(
370 ident: syn!(Lifetime) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800371 colon: option!(punct!(:)) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400372 bounds: cond!(
373 colon.is_some(),
374 call!(Delimited::parse_separated)
375 ) >>
376 (WherePredicate::RegionPredicate(WhereRegionPredicate {
377 lifetime: ident,
378 bounds: bounds.unwrap_or_default(),
379 colon_token: colon,
380 }))
381 )
382 |
383 do_parse!(
384 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800385 bounded_ty: syn!(Type) >>
David Tolnayf8db7ba2017-11-11 22:52:16 -0800386 colon: punct!(:) >>
Michael Layzell92639a52017-06-01 00:07:44 -0400387 bounds: call!(Delimited::parse_separated_nonempty) >>
388 (WherePredicate::BoundPredicate(WhereBoundPredicate {
389 bound_lifetimes: bound_lifetimes,
390 bounded_ty: bounded_ty,
391 bounds: bounds,
392 colon_token: colon,
393 }))
394 )
395 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700396 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700397}
David Tolnay87d0b442016-09-04 11:52:12 -0700398
399#[cfg(feature = "printing")]
400mod printing {
401 use super::*;
David Tolnaye7678922016-10-13 20:44:03 -0700402 use attr::FilterAttrs;
David Tolnay87d0b442016-09-04 11:52:12 -0700403 use quote::{Tokens, ToTokens};
404
Michael Layzell3936ceb2017-07-08 00:28:36 -0400405 /// Returns true if the generics object has no lifetimes or ty_params.
406 fn empty_normal_generics(generics: &Generics) -> bool {
David Tolnayc2f1aba2017-11-12 20:29:22 -0800407 generics.params.is_empty()
Michael Layzell3936ceb2017-07-08 00:28:36 -0400408 }
409
David Tolnay8ef93042016-09-04 14:08:40 -0700410 impl ToTokens for Generics {
411 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400412 if empty_normal_generics(self) {
413 return;
414 }
415
Alex Crichton259ee532017-07-14 06:51:02 -0700416 TokensOrDefault(&self.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800417 self.params.to_tokens(tokens);
Alex Crichton259ee532017-07-14 06:51:02 -0700418 TokensOrDefault(&self.gt_token).to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700419 }
420 }
421
David Tolnaye7678922016-10-13 20:44:03 -0700422 impl<'a> ToTokens for ImplGenerics<'a> {
423 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400424 if empty_normal_generics(&self.0) {
425 return;
426 }
427
Alex Crichton259ee532017-07-14 06:51:02 -0700428 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800429 for param in self.0.params.iter() {
430 match **param.item() {
431 GenericParam::Lifetime(ref param) => {
432 param.to_tokens(tokens);
433 }
434 GenericParam::Type(ref param) => {
435 // Leave off the type parameter defaults
436 tokens.append_all(param.attrs.outer());
437 param.ident.to_tokens(tokens);
438 if !param.bounds.is_empty() {
439 TokensOrDefault(&param.colon_token).to_tokens(tokens);
440 param.bounds.to_tokens(tokens);
441 }
442 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500443 GenericParam::Const(ref param) => {
444 // Leave off the const parameter defaults
445 tokens.append_all(param.attrs.outer());
446 param.const_token.to_tokens(tokens);
447 param.ident.to_tokens(tokens);
448 param.colon_token.to_tokens(tokens);
449 param.ty.to_tokens(tokens);
450 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400451 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700452 param.delimiter().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700453 }
Alex Crichton259ee532017-07-14 06:51:02 -0700454 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700455 }
456 }
457
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800458 impl<'a> ToTokens for TypeGenerics<'a> {
David Tolnaye7678922016-10-13 20:44:03 -0700459 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400460 if empty_normal_generics(&self.0) {
461 return;
462 }
463
Alex Crichton259ee532017-07-14 06:51:02 -0700464 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnayc2f1aba2017-11-12 20:29:22 -0800465 for param in self.0.params.iter() {
466 match **param.item() {
467 GenericParam::Lifetime(ref param) => {
468 // Leave off the lifetime bounds and attributes
469 param.lifetime.to_tokens(tokens);
470 }
471 GenericParam::Type(ref param) => {
472 // Leave off the type parameter defaults
473 param.ident.to_tokens(tokens);
474 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500475 GenericParam::Const(ref param) => {
476 // Leave off the const parameter defaults
477 param.ident.to_tokens(tokens);
478 }
David Tolnayc2f1aba2017-11-12 20:29:22 -0800479 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700480 param.delimiter().to_tokens(tokens);
481 }
Alex Crichton259ee532017-07-14 06:51:02 -0700482 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700483 }
484 }
485
David Tolnayc879a502017-01-25 15:51:32 -0800486 impl<'a> ToTokens for Turbofish<'a> {
487 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400488 if !empty_normal_generics(&self.0) {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800489 <Token![::]>::default().to_tokens(tokens);
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800490 TypeGenerics(self.0).to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -0800491 }
492 }
493 }
494
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700495 impl ToTokens for BoundLifetimes {
496 fn to_tokens(&self, tokens: &mut Tokens) {
497 self.for_token.to_tokens(tokens);
498 self.lt_token.to_tokens(tokens);
499 self.lifetimes.to_tokens(tokens);
500 self.gt_token.to_tokens(tokens);
501 }
502 }
503
David Tolnay87d0b442016-09-04 11:52:12 -0700504 impl ToTokens for LifetimeDef {
505 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700506 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -0700507 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400508 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700509 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400510 self.bounds.to_tokens(tokens);
511 }
David Tolnay87d0b442016-09-04 11:52:12 -0700512 }
513 }
514
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800515 impl ToTokens for TypeParam {
David Tolnay8ef93042016-09-04 14:08:40 -0700516 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700517 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -0700518 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400519 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700520 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400521 self.bounds.to_tokens(tokens);
522 }
523 if self.default.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -0700524 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400525 self.default.to_tokens(tokens);
526 }
David Tolnay8ef93042016-09-04 14:08:40 -0700527 }
528 }
529
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800530 impl ToTokens for TypeParamBound {
David Tolnay87d0b442016-09-04 11:52:12 -0700531 fn to_tokens(&self, tokens: &mut Tokens) {
532 match *self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800533 TypeParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens),
534 TypeParamBound::Trait(ref trait_ref, ref modifier) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700535 modifier.to_tokens(tokens);
David Tolnay55337722016-09-11 12:58:56 -0700536 trait_ref.to_tokens(tokens);
537 }
538 }
539 }
540 }
541
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700542 impl ToTokens for TraitBoundModifier {
543 fn to_tokens(&self, tokens: &mut Tokens) {
544 match *self {
545 TraitBoundModifier::None => {}
546 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
547 }
548 }
549 }
550
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500551 impl ToTokens for ConstParam {
552 fn to_tokens(&self, tokens: &mut Tokens) {
553 tokens.append_all(self.attrs.outer());
554 self.const_token.to_tokens(tokens);
555 self.ident.to_tokens(tokens);
556 self.colon_token.to_tokens(tokens);
557 self.ty.to_tokens(tokens);
558 if self.default.is_some() {
David Tolnay78ee5202017-12-04 22:17:54 -0800559 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500560 self.default.to_tokens(tokens);
561 }
562 }
563 }
564
David Tolnay55337722016-09-11 12:58:56 -0700565 impl ToTokens for WhereClause {
566 fn to_tokens(&self, tokens: &mut Tokens) {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400567 if !self.predicates.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700568 TokensOrDefault(&self.where_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400569 self.predicates.to_tokens(tokens);
570 }
David Tolnay87d0b442016-09-04 11:52:12 -0700571 }
572 }
David Tolnay8ef93042016-09-04 14:08:40 -0700573
David Tolnay8ef93042016-09-04 14:08:40 -0700574 impl ToTokens for WhereBoundPredicate {
575 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700576 self.bound_lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700577 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700578 self.colon_token.to_tokens(tokens);
579 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700580 }
581 }
582
583 impl ToTokens for WhereRegionPredicate {
584 fn to_tokens(&self, tokens: &mut Tokens) {
585 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400586 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -0700587 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -0400588 self.bounds.to_tokens(tokens);
589 }
David Tolnay8ef93042016-09-04 14:08:40 -0700590 }
591 }
David Tolnayf8e08832017-01-23 00:04:32 -0800592
593 impl ToTokens for WhereEqPredicate {
594 fn to_tokens(&self, tokens: &mut Tokens) {
595 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700596 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -0800597 self.rhs_ty.to_tokens(tokens);
598 }
599 }
David Tolnay87d0b442016-09-04 11:52:12 -0700600}