blob: afcf9a4345e814ca8b44affac750e0d94ae1631d [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// Copyright 2018 Syn Developers
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
David Tolnayb79ee962016-09-04 09:39:20 -07009use super::*;
Sean Griffin8fcca802018-01-15 15:45:41 -070010use punctuated::{Iter, IterMut, Punctuated};
David Tolnayb79ee962016-09-04 09:39:20 -070011
Alex Crichton62a0a592017-05-22 13:58:53 -070012ast_struct! {
David Tolnayed906d12018-01-07 01:20:29 -080013 /// Lifetimes and type parameters attached to a declaration of a function,
14 /// enum, trait, etc.
David Tolnay461d98e2018-01-07 11:07:19 -080015 ///
16 /// *This type is available if Syn is built with the `"derive"` or `"full"`
17 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -070018 #[derive(Default)]
19 pub struct Generics {
David Tolnayf8db7ba2017-11-11 22:52:16 -080020 pub lt_token: Option<Token![<]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050021 pub params: Punctuated<GenericParam, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -080022 pub gt_token: Option<Token![>]>,
David Tolnayac997dd2017-12-27 23:18:22 -050023 pub where_clause: Option<WhereClause>,
Alex Crichton62a0a592017-05-22 13:58:53 -070024 }
David Tolnayb79ee962016-09-04 09:39:20 -070025}
26
David Tolnayc2f1aba2017-11-12 20:29:22 -080027ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -080028 /// A generic type parameter, lifetime, or const generic: `T: Into<String>`,
29 /// `'a: 'b`, `const LEN: usize`.
David Tolnay614a0142018-01-07 10:25:43 -080030 ///
David Tolnay461d98e2018-01-07 11:07:19 -080031 /// *This type is available if Syn is built with the `"derive"` or `"full"`
32 /// feature.*
33 ///
David Tolnay614a0142018-01-07 10:25:43 -080034 /// # Syntax tree enum
35 ///
36 /// This type is a [syntax tree enum].
37 ///
38 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
David Tolnayc2f1aba2017-11-12 20:29:22 -080039 pub enum GenericParam {
David Tolnayed906d12018-01-07 01:20:29 -080040 /// A generic type parameter: `T: Into<String>`.
David Tolnay461d98e2018-01-07 11:07:19 -080041 ///
42 /// *This type is available if Syn is built with the `"derive"` or
43 /// `"full"` feature.*
David Tolnayc2f1aba2017-11-12 20:29:22 -080044 pub Type(TypeParam {
45 pub attrs: Vec<Attribute>,
46 pub ident: Ident,
47 pub colon_token: Option<Token![:]>,
David Tolnayf2cfd722017-12-31 18:02:51 -050048 pub bounds: Punctuated<TypeParamBound, Token![+]>,
David Tolnayc2f1aba2017-11-12 20:29:22 -080049 pub eq_token: Option<Token![=]>,
50 pub default: Option<Type>,
51 }),
David Tolnayed906d12018-01-07 01:20:29 -080052
53 /// A lifetime definition: `'a: 'b + 'c + 'd`.
David Tolnay461d98e2018-01-07 11:07:19 -080054 ///
55 /// *This type is available if Syn is built with the `"derive"` or
56 /// `"full"` feature.*
David Tolnay517f3692018-01-01 20:17:23 -080057 pub Lifetime(LifetimeDef {
58 pub attrs: Vec<Attribute>,
59 pub lifetime: Lifetime,
60 pub colon_token: Option<Token![:]>,
61 pub bounds: Punctuated<Lifetime, Token![+]>,
62 }),
David Tolnayed906d12018-01-07 01:20:29 -080063
64 /// A const generic parameter: `const LENGTH: usize`.
David Tolnay461d98e2018-01-07 11:07:19 -080065 ///
66 /// *This type is available if Syn is built with the `"derive"` or
67 /// `"full"` feature.*
Nika Layzellf1fdc0b2017-12-04 19:58:32 -050068 pub Const(ConstParam {
69 pub attrs: Vec<Attribute>,
70 pub const_token: Token![const],
71 pub ident: Ident,
72 pub colon_token: Token![:],
73 pub ty: Type,
74 pub eq_token: Option<Token![=]>,
75 pub default: Option<Expr>,
76 }),
David Tolnayc2f1aba2017-11-12 20:29:22 -080077 }
78}
79
Sean Griffin8fcca802018-01-15 15:45:41 -070080impl Generics {
David Tolnayef5199b2018-01-17 10:56:03 -080081 /// Returns an
82 /// <code
83 /// style="padding-right:0;">Iterator&lt;Item = &amp;</code><a
84 /// href="struct.TypeParam.html"><code
85 /// style="padding-left:0;padding-right:0;">TypeParam</code></a><code
86 /// style="padding-left:0;">&gt;</code>
87 /// over the type parameters in `self.params`.
Sean Griffin8fcca802018-01-15 15:45:41 -070088 pub fn type_params(&self) -> TypeParams {
89 TypeParams(self.params.iter())
90 }
91
David Tolnayef5199b2018-01-17 10:56:03 -080092 /// Returns an
93 /// <code
94 /// style="padding-right:0;">Iterator&lt;Item = &amp;mut </code><a
95 /// href="struct.TypeParam.html"><code
96 /// style="padding-left:0;padding-right:0;">TypeParam</code></a><code
97 /// style="padding-left:0;">&gt;</code>
98 /// over the type parameters in `self.params`.
Sean Griffin8fcca802018-01-15 15:45:41 -070099 pub fn type_params_mut(&mut self) -> TypeParamsMut {
100 TypeParamsMut(self.params.iter_mut())
101 }
102
David Tolnayef5199b2018-01-17 10:56:03 -0800103 /// Returns an
104 /// <code
105 /// style="padding-right:0;">Iterator&lt;Item = &amp;</code><a
106 /// href="struct.LifetimeDef.html"><code
107 /// style="padding-left:0;padding-right:0;">LifetimeDef</code></a><code
108 /// style="padding-left:0;">&gt;</code>
109 /// over the lifetime parameters in `self.params`.
Sean Griffin8fcca802018-01-15 15:45:41 -0700110 pub fn lifetimes(&self) -> Lifetimes {
111 Lifetimes(self.params.iter())
112 }
113
David Tolnayef5199b2018-01-17 10:56:03 -0800114 /// Returns an
115 /// <code
116 /// style="padding-right:0;">Iterator&lt;Item = &amp;mut </code><a
117 /// href="struct.LifetimeDef.html"><code
118 /// style="padding-left:0;padding-right:0;">LifetimeDef</code></a><code
119 /// style="padding-left:0;">&gt;</code>
120 /// over the lifetime parameters in `self.params`.
Sean Griffin8fcca802018-01-15 15:45:41 -0700121 pub fn lifetimes_mut(&mut self) -> LifetimesMut {
122 LifetimesMut(self.params.iter_mut())
123 }
124
David Tolnayef5199b2018-01-17 10:56:03 -0800125 /// Returns an
126 /// <code
127 /// style="padding-right:0;">Iterator&lt;Item = &amp;</code><a
128 /// href="struct.ConstParam.html"><code
129 /// style="padding-left:0;padding-right:0;">ConstParam</code></a><code
130 /// style="padding-left:0;">&gt;</code>
131 /// over the constant parameters in `self.params`.
Sean Griffin8fcca802018-01-15 15:45:41 -0700132 pub fn const_params(&self) -> ConstParams {
133 ConstParams(self.params.iter())
134 }
135
David Tolnayef5199b2018-01-17 10:56:03 -0800136 /// Returns an
137 /// <code
138 /// style="padding-right:0;">Iterator&lt;Item = &amp;mut </code><a
139 /// href="struct.ConstParam.html"><code
140 /// style="padding-left:0;padding-right:0;">ConstParam</code></a><code
141 /// style="padding-left:0;">&gt;</code>
142 /// over the constant parameters in `self.params`.
Sean Griffin8fcca802018-01-15 15:45:41 -0700143 pub fn const_params_mut(&mut self) -> ConstParamsMut {
144 ConstParamsMut(self.params.iter_mut())
145 }
David Tolnay7f9954d2018-04-01 16:52:15 +0200146
147 /// Initializes an empty `where`-clause if there is not one present already.
148 pub fn make_where_clause(&mut self) -> &mut WhereClause {
149 // This is Option::get_or_insert_with in Rust 1.20.
150 if self.where_clause.is_none() {
151 self.where_clause = Some(WhereClause {
David Tolnayd228b332018-06-27 23:56:05 -0700152 where_token: <Token![where]>::default(),
David Tolnay7f9954d2018-04-01 16:52:15 +0200153 predicates: Punctuated::new(),
154 });
155 }
156 match self.where_clause {
157 Some(ref mut where_clause) => where_clause,
158 None => unreachable!(),
159 }
160 }
Sean Griffin8fcca802018-01-15 15:45:41 -0700161}
162
David Tolnay8095c302018-03-31 19:34:17 +0200163pub struct TypeParams<'a>(Iter<'a, GenericParam>);
Sean Griffin8fcca802018-01-15 15:45:41 -0700164
165impl<'a> Iterator for TypeParams<'a> {
166 type Item = &'a TypeParam;
167
168 fn next(&mut self) -> Option<Self::Item> {
Sean Griffin8fcca802018-01-15 15:45:41 -0700169 let next = match self.0.next() {
170 Some(item) => item,
171 None => return None,
172 };
173 if let GenericParam::Type(ref type_param) = *next {
174 Some(type_param)
175 } else {
176 self.next()
177 }
178 }
179}
180
David Tolnay8095c302018-03-31 19:34:17 +0200181pub struct TypeParamsMut<'a>(IterMut<'a, GenericParam>);
Sean Griffin8fcca802018-01-15 15:45:41 -0700182
183impl<'a> Iterator for TypeParamsMut<'a> {
184 type Item = &'a mut TypeParam;
185
186 fn next(&mut self) -> Option<Self::Item> {
Sean Griffin8fcca802018-01-15 15:45:41 -0700187 let next = match self.0.next() {
188 Some(item) => item,
189 None => return None,
190 };
191 if let GenericParam::Type(ref mut type_param) = *next {
192 Some(type_param)
193 } else {
194 self.next()
195 }
196 }
197}
198
David Tolnay8095c302018-03-31 19:34:17 +0200199pub struct Lifetimes<'a>(Iter<'a, GenericParam>);
Sean Griffin8fcca802018-01-15 15:45:41 -0700200
201impl<'a> Iterator for Lifetimes<'a> {
202 type Item = &'a LifetimeDef;
203
204 fn next(&mut self) -> Option<Self::Item> {
Sean Griffin8fcca802018-01-15 15:45:41 -0700205 let next = match self.0.next() {
206 Some(item) => item,
207 None => return None,
208 };
209 if let GenericParam::Lifetime(ref lifetime) = *next {
210 Some(lifetime)
211 } else {
212 self.next()
213 }
214 }
215}
216
David Tolnay8095c302018-03-31 19:34:17 +0200217pub struct LifetimesMut<'a>(IterMut<'a, GenericParam>);
Sean Griffin8fcca802018-01-15 15:45:41 -0700218
219impl<'a> Iterator for LifetimesMut<'a> {
220 type Item = &'a mut LifetimeDef;
221
222 fn next(&mut self) -> Option<Self::Item> {
Sean Griffin8fcca802018-01-15 15:45:41 -0700223 let next = match self.0.next() {
224 Some(item) => item,
225 None => return None,
226 };
227 if let GenericParam::Lifetime(ref mut lifetime) = *next {
228 Some(lifetime)
229 } else {
230 self.next()
231 }
232 }
233}
234
David Tolnay8095c302018-03-31 19:34:17 +0200235pub struct ConstParams<'a>(Iter<'a, GenericParam>);
Sean Griffin8fcca802018-01-15 15:45:41 -0700236
237impl<'a> Iterator for ConstParams<'a> {
238 type Item = &'a ConstParam;
239
240 fn next(&mut self) -> Option<Self::Item> {
Sean Griffin8fcca802018-01-15 15:45:41 -0700241 let next = match self.0.next() {
242 Some(item) => item,
243 None => return None,
244 };
245 if let GenericParam::Const(ref const_param) = *next {
246 Some(const_param)
247 } else {
248 self.next()
249 }
250 }
251}
252
David Tolnay8095c302018-03-31 19:34:17 +0200253pub struct ConstParamsMut<'a>(IterMut<'a, GenericParam>);
Sean Griffin8fcca802018-01-15 15:45:41 -0700254
255impl<'a> Iterator for ConstParamsMut<'a> {
256 type Item = &'a mut ConstParam;
257
258 fn next(&mut self) -> Option<Self::Item> {
Sean Griffin8fcca802018-01-15 15:45:41 -0700259 let next = match self.0.next() {
260 Some(item) => item,
261 None => return None,
262 };
263 if let GenericParam::Const(ref mut const_param) = *next {
264 Some(const_param)
265 } else {
266 self.next()
267 }
268 }
269}
270
David Tolnay461d98e2018-01-07 11:07:19 -0800271/// Returned by `Generics::split_for_impl`.
272///
273/// *This type is available if Syn is built with the `"derive"` or `"full"`
274/// feature and the `"printing"` feature.*
David Tolnaye7678922016-10-13 20:44:03 -0700275#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -0400276#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
277#[cfg_attr(feature = "clone-impls", derive(Clone))]
Nika Layzell6b38b132017-10-24 23:09:39 -0400278pub struct ImplGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -0700279
David Tolnay461d98e2018-01-07 11:07:19 -0800280/// Returned by `Generics::split_for_impl`.
281///
282/// *This type is available if Syn is built with the `"derive"` or `"full"`
283/// feature and the `"printing"` feature.*
David Tolnaye7678922016-10-13 20:44:03 -0700284#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -0400285#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
286#[cfg_attr(feature = "clone-impls", derive(Clone))]
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800287pub struct TypeGenerics<'a>(&'a Generics);
David Tolnaye7678922016-10-13 20:44:03 -0700288
David Tolnay461d98e2018-01-07 11:07:19 -0800289/// Returned by `TypeGenerics::as_turbofish`.
290///
291/// *This type is available if Syn is built with the `"derive"` or `"full"`
292/// feature and the `"printing"` feature.*
David Tolnayc879a502017-01-25 15:51:32 -0800293#[cfg(feature = "printing")]
Nika Layzell6b38b132017-10-24 23:09:39 -0400294#[cfg_attr(feature = "extra-traits", derive(Debug, Eq, PartialEq, Hash))]
295#[cfg_attr(feature = "clone-impls", derive(Clone))]
Nika Layzell6b38b132017-10-24 23:09:39 -0400296pub struct Turbofish<'a>(&'a Generics);
David Tolnayc879a502017-01-25 15:51:32 -0800297
David Tolnaye95cc9f2017-01-25 15:57:09 -0800298#[cfg(feature = "printing")]
David Tolnayb153dbc2016-10-04 23:39:10 -0700299impl Generics {
300 /// Split a type's generics into the pieces required for impl'ing a trait
301 /// for that type.
302 ///
303 /// ```
David Tolnaya1c98072018-09-06 08:58:10 -0700304 /// # #[macro_use]
David Tolnayb153dbc2016-10-04 23:39:10 -0700305 /// # extern crate quote;
David Tolnay9b00f652018-09-01 10:31:02 -0700306 /// #
David Tolnaya1c98072018-09-06 08:58:10 -0700307 /// # extern crate proc_macro2;
308 /// # extern crate syn;
309 /// #
Alex Crichtonf0fea9a2018-05-17 11:19:34 -0700310 /// # use proc_macro2::{Span, Ident};
David Tolnay9b00f652018-09-01 10:31:02 -0700311 /// #
David Tolnayb153dbc2016-10-04 23:39:10 -0700312 /// # fn main() {
David Tolnay9b00f652018-09-01 10:31:02 -0700313 /// # let generics: syn::Generics = Default::default();
314 /// # let name = Ident::new("MyType", Span::call_site());
315 /// #
David Tolnayb153dbc2016-10-04 23:39:10 -0700316 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
317 /// quote! {
318 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
319 /// // ...
320 /// }
321 /// }
David Tolnay9b00f652018-09-01 10:31:02 -0700322 /// # ;
David Tolnayb153dbc2016-10-04 23:39:10 -0700323 /// # }
324 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800325 ///
326 /// *This method is available if Syn is built with the `"derive"` or
327 /// `"full"` feature and the `"printing"` feature.*
David Tolnayac997dd2017-12-27 23:18:22 -0500328 pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) {
David Tolnay61037c62018-01-05 16:21:03 -0800329 (
330 ImplGenerics(self),
331 TypeGenerics(self),
332 self.where_clause.as_ref(),
333 )
David Tolnayb153dbc2016-10-04 23:39:10 -0700334 }
335}
336
David Tolnaye95cc9f2017-01-25 15:57:09 -0800337#[cfg(feature = "printing")]
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800338impl<'a> TypeGenerics<'a> {
David Tolnayc879a502017-01-25 15:51:32 -0800339 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800340 ///
341 /// *This method is available if Syn is built with the `"derive"` or
342 /// `"full"` feature and the `"printing"` feature.*
David Tolnayc879a502017-01-25 15:51:32 -0800343 pub fn as_turbofish(&self) -> Turbofish {
344 Turbofish(self.0)
345 }
346}
347
Alex Crichton62a0a592017-05-22 13:58:53 -0700348ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800349 /// A set of bound lifetimes: `for<'a, 'b, 'c>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800350 ///
351 /// *This type is available if Syn is built with the `"derive"` or `"full"`
352 /// feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700353 #[derive(Default)]
354 pub struct BoundLifetimes {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800355 pub for_token: Token![for],
356 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500357 pub lifetimes: Punctuated<LifetimeDef, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800358 pub gt_token: Token![>],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700359 }
360}
361
David Tolnayf9505b52016-10-02 09:18:52 -0700362impl LifetimeDef {
David Tolnay63e3dee2017-06-03 20:13:17 -0700363 pub fn new(lifetime: Lifetime) -> Self {
David Tolnayf9505b52016-10-02 09:18:52 -0700364 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -0700365 attrs: Vec::new(),
David Tolnay63e3dee2017-06-03 20:13:17 -0700366 lifetime: lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700367 colon_token: None,
David Tolnayf2cfd722017-12-31 18:02:51 -0500368 bounds: Punctuated::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700369 }
370 }
371}
372
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800373impl From<Ident> for TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700374 fn from(ident: Ident) -> Self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800375 TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700376 attrs: vec![],
377 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700378 colon_token: None,
David Tolnayf2cfd722017-12-31 18:02:51 -0500379 bounds: Punctuated::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700380 eq_token: None,
Ted Driggs0547d0d2017-04-20 10:00:12 -0700381 default: None,
382 }
383 }
384}
385
David Tolnay40fb8ce2018-01-02 10:53:46 -0800386ast_enum_of_structs! {
David Tolnayed906d12018-01-07 01:20:29 -0800387 /// A trait or lifetime used as a bound on a type parameter.
David Tolnay461d98e2018-01-07 11:07:19 -0800388 ///
389 /// *This type is available if Syn is built with the `"derive"` or `"full"`
390 /// feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800391 pub enum TypeParamBound {
David Tolnay40fb8ce2018-01-02 10:53:46 -0800392 pub Trait(TraitBound),
393 pub Lifetime(Lifetime),
394 }
395}
396
397ast_struct! {
David Tolnayed906d12018-01-07 01:20:29 -0800398 /// A trait used as a bound on a type parameter.
David Tolnay461d98e2018-01-07 11:07:19 -0800399 ///
400 /// *This type is available if Syn is built with the `"derive"` or `"full"`
401 /// feature.*
David Tolnay40fb8ce2018-01-02 10:53:46 -0800402 pub struct TraitBound {
David Tolnayc1f5d5d2018-03-31 22:17:56 +0200403 pub paren_token: Option<token::Paren>,
David Tolnay40fb8ce2018-01-02 10:53:46 -0800404 pub modifier: TraitBoundModifier,
405 /// The `for<'a>` in `for<'a> Foo<&'a T>`
406 pub lifetimes: Option<BoundLifetimes>,
407 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
408 pub path: Path,
Alex Crichton62a0a592017-05-22 13:58:53 -0700409 }
David Tolnay55337722016-09-11 12:58:56 -0700410}
411
Alex Crichton62a0a592017-05-22 13:58:53 -0700412ast_enum! {
David Tolnayed906d12018-01-07 01:20:29 -0800413 /// A modifier on a trait bound, currently only used for the `?` in
414 /// `?Sized`.
David Tolnay461d98e2018-01-07 11:07:19 -0800415 ///
416 /// *This type is available if Syn is built with the `"derive"` or `"full"`
417 /// feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700418 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700419 pub enum TraitBoundModifier {
420 None,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800421 Maybe(Token![?]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700422 }
David Tolnay55337722016-09-11 12:58:56 -0700423}
424
Alex Crichton62a0a592017-05-22 13:58:53 -0700425ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800426 /// A `where` clause in a definition: `where T: Deserialize<'de>, D:
427 /// 'static`.
David Tolnay461d98e2018-01-07 11:07:19 -0800428 ///
429 /// *This type is available if Syn is built with the `"derive"` or `"full"`
430 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700431 pub struct WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500432 pub where_token: Token![where],
David Tolnayf2cfd722017-12-31 18:02:51 -0500433 pub predicates: Punctuated<WherePredicate, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700434 }
David Tolnayb79ee962016-09-04 09:39:20 -0700435}
436
Alex Crichton62a0a592017-05-22 13:58:53 -0700437ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800438 /// A single predicate in a `where` clause: `T: Deserialize<'de>`.
David Tolnay614a0142018-01-07 10:25:43 -0800439 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800440 /// *This type is available if Syn is built with the `"derive"` or `"full"`
441 /// feature.*
442 ///
David Tolnay614a0142018-01-07 10:25:43 -0800443 /// # Syntax tree enum
444 ///
445 /// This type is a [syntax tree enum].
446 ///
447 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700448 pub enum WherePredicate {
David Tolnayed906d12018-01-07 01:20:29 -0800449 /// A type predicate in a `where` clause: `for<'c> Foo<'c>: Trait<'c>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800450 ///
451 /// *This type is available if Syn is built with the `"derive"` or
452 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800453 pub Type(PredicateType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700454 /// Any lifetimes from a `for` binding
David Tolnay40fb8ce2018-01-02 10:53:46 -0800455 pub lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700456 /// The type being bounded
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800457 pub bounded_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800458 pub colon_token: Token![:],
Alex Crichton62a0a592017-05-22 13:58:53 -0700459 /// Trait and lifetime bounds (`Clone+Send+'static`)
David Tolnayf2cfd722017-12-31 18:02:51 -0500460 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700461 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700462
David Tolnayed906d12018-01-07 01:20:29 -0800463 /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`.
David Tolnay461d98e2018-01-07 11:07:19 -0800464 ///
465 /// *This type is available if Syn is built with the `"derive"` or
466 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800467 pub Lifetime(PredicateLifetime {
Alex Crichton62a0a592017-05-22 13:58:53 -0700468 pub lifetime: Lifetime,
David Tolnay1b8e2852018-08-26 08:25:18 -0400469 pub colon_token: Token![:],
David Tolnayf2cfd722017-12-31 18:02:51 -0500470 pub bounds: Punctuated<Lifetime, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700471 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700472
David Tolnayed906d12018-01-07 01:20:29 -0800473 /// An equality predicate in a `where` clause (unsupported).
David Tolnay461d98e2018-01-07 11:07:19 -0800474 ///
475 /// *This type is available if Syn is built with the `"derive"` or
476 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800477 pub Eq(PredicateEq {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800478 pub lhs_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800479 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800480 pub rhs_ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700481 }),
482 }
David Tolnayf8e08832017-01-23 00:04:32 -0800483}
484
David Tolnay86eca752016-09-04 11:26:41 -0700485#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700486pub mod parsing {
487 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700488
David Tolnay1b8e2852018-08-26 08:25:18 -0400489 use parse::{Parse, ParseStream, Result};
Alex Crichton954046c2017-05-30 21:49:42 -0700490
David Tolnay1b8e2852018-08-26 08:25:18 -0400491 impl Parse for Generics {
492 fn parse(input: ParseStream) -> Result<Self> {
493 let mut params = Punctuated::new();
494
495 if !input.peek(Token![<]) {
496 return Ok(Generics {
497 lt_token: None,
498 params: params,
499 gt_token: None,
500 where_clause: None,
501 });
Michael Layzell416724e2017-05-24 21:12:34 -0400502 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800503
David Tolnay1b8e2852018-08-26 08:25:18 -0400504 let lt_token: Token![<] = input.parse()?;
Alex Crichton954046c2017-05-30 21:49:42 -0700505
David Tolnay1b8e2852018-08-26 08:25:18 -0400506 let mut has_type_param = false;
507 loop {
508 if input.peek(Token![>]) {
509 break;
David Tolnay40fb8ce2018-01-02 10:53:46 -0800510 }
David Tolnay1b8e2852018-08-26 08:25:18 -0400511
512 let attrs = input.call(Attribute::parse_outer)?;
513 let lookahead = input.lookahead1();
514 if !has_type_param && lookahead.peek(Lifetime) {
515 params.push_value(GenericParam::Lifetime(LifetimeDef {
516 attrs: attrs,
517 ..input.parse()?
518 }));
519 } else if lookahead.peek(Ident) {
520 has_type_param = true;
521 params.push_value(GenericParam::Type(TypeParam {
522 attrs: attrs,
523 ..input.parse()?
524 }));
525 } else {
526 return Err(lookahead.error());
David Tolnay40fb8ce2018-01-02 10:53:46 -0800527 }
David Tolnay1b8e2852018-08-26 08:25:18 -0400528
529 if input.peek(Token![>]) {
530 break;
531 }
532 let punct = input.parse()?;
533 params.push_punct(punct);
534 }
535
536 let gt_token: Token![>] = input.parse()?;
537
538 Ok(Generics {
539 lt_token: Some(lt_token),
540 params: params,
541 gt_token: Some(gt_token),
542 where_clause: None,
David Tolnay40fb8ce2018-01-02 10:53:46 -0800543 })
David Tolnay40fb8ce2018-01-02 10:53:46 -0800544 }
545 }
546
David Tolnay1b8e2852018-08-26 08:25:18 -0400547 impl Parse for GenericParam {
548 fn parse(input: ParseStream) -> Result<Self> {
549 let attrs = input.call(Attribute::parse_outer)?;
David Tolnay40fb8ce2018-01-02 10:53:46 -0800550
David Tolnay1b8e2852018-08-26 08:25:18 -0400551 let lookahead = input.lookahead1();
552 if lookahead.peek(Ident) {
553 Ok(GenericParam::Type(TypeParam {
David Tolnay78ee5202017-12-04 22:17:54 -0800554 attrs: attrs,
David Tolnay1b8e2852018-08-26 08:25:18 -0400555 ..input.parse()?
556 }))
557 } else if lookahead.peek(Lifetime) {
558 Ok(GenericParam::Lifetime(LifetimeDef {
559 attrs: attrs,
560 ..input.parse()?
561 }))
562 } else if lookahead.peek(Token![const]) {
563 Ok(GenericParam::Const(ConstParam {
564 attrs: attrs,
565 ..input.parse()?
566 }))
567 } else {
568 Err(lookahead.error())
569 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800570 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500571 }
572
David Tolnay1b8e2852018-08-26 08:25:18 -0400573 impl Parse for LifetimeDef {
574 fn parse(input: ParseStream) -> Result<Self> {
575 let has_colon;
576 Ok(LifetimeDef {
577 attrs: input.call(Attribute::parse_outer)?,
578 lifetime: input.parse()?,
579 colon_token: {
580 if input.peek(Token![:]) {
581 has_colon = true;
582 Some(input.parse()?)
583 } else {
584 has_colon = false;
585 None
586 }
587 },
588 bounds: {
589 let mut bounds = Punctuated::new();
590 if has_colon {
591 loop {
592 if input.peek(Token![,]) || input.peek(Token![>]) {
593 break;
594 }
595 let value = input.parse()?;
596 bounds.push_value(value);
597 if !input.peek(Token![+]) {
598 break;
599 }
600 let punct = input.parse()?;
601 bounds.push_punct(punct);
602 }
603 }
604 bounds
605 },
David Tolnayac997dd2017-12-27 23:18:22 -0500606 })
Alex Crichton954046c2017-05-30 21:49:42 -0700607 }
608 }
609
David Tolnay1b8e2852018-08-26 08:25:18 -0400610 impl Parse for BoundLifetimes {
611 fn parse(input: ParseStream) -> Result<Self> {
612 Ok(BoundLifetimes {
613 for_token: input.parse()?,
614 lt_token: input.parse()?,
David Tolnayf5ebc192018-08-30 18:23:46 -0700615 lifetimes: {
616 let mut lifetimes = Punctuated::new();
617 while !input.peek(Token![>]) {
618 lifetimes.push_value(input.parse()?);
619 if input.peek(Token![>]) {
620 break;
621 }
622 lifetimes.push_punct(input.parse()?);
623 }
624 lifetimes
625 },
David Tolnay1b8e2852018-08-26 08:25:18 -0400626 gt_token: input.parse()?,
627 })
628 }
629 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800630
David Tolnay1b8e2852018-08-26 08:25:18 -0400631 impl Parse for Option<BoundLifetimes> {
632 fn parse(input: ParseStream) -> Result<Self> {
633 if input.peek(Token![for]) {
634 input.parse().map(Some)
635 } else {
636 Ok(None)
637 }
638 }
639 }
640
641 impl Parse for TypeParam {
642 fn parse(input: ParseStream) -> Result<Self> {
643 let has_colon;
644 let has_default;
645 Ok(TypeParam {
646 attrs: input.call(Attribute::parse_outer)?,
647 ident: input.parse()?,
648 colon_token: {
649 if input.peek(Token![:]) {
650 has_colon = true;
651 Some(input.parse()?)
652 } else {
653 has_colon = false;
654 None
655 }
656 },
657 bounds: {
658 let mut bounds = Punctuated::new();
659 if has_colon {
660 loop {
David Tolnaycb445c12018-10-27 22:36:16 -0700661 if input.peek(Token![,]) || input.peek(Token![>]) || input.peek(Token![=]) {
David Tolnay1b8e2852018-08-26 08:25:18 -0400662 break;
663 }
664 let value = input.parse()?;
665 bounds.push_value(value);
666 if !input.peek(Token![+]) {
667 break;
668 }
669 let punct = input.parse()?;
670 bounds.push_punct(punct);
671 }
672 }
673 bounds
674 },
675 eq_token: {
676 if input.peek(Token![=]) {
677 has_default = true;
678 Some(input.parse()?)
679 } else {
680 has_default = false;
681 None
682 }
683 },
684 default: {
685 if has_default {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400686 Some(input.parse()?)
David Tolnay1b8e2852018-08-26 08:25:18 -0400687 } else {
688 None
689 }
690 },
691 })
692 }
693 }
694
695 impl Parse for TypeParamBound {
696 fn parse(input: ParseStream) -> Result<Self> {
697 if input.peek(Lifetime) {
698 return input.parse().map(TypeParamBound::Lifetime);
699 }
700
701 if input.peek(token::Paren) {
702 let content;
703 let paren_token = parenthesized!(content in input);
704 let mut bound: TraitBound = content.parse()?;
705 bound.paren_token = Some(paren_token);
706 return Ok(TypeParamBound::Trait(bound));
707 }
708
709 input.parse().map(TypeParamBound::Trait)
710 }
711 }
712
713 impl Parse for TraitBound {
714 fn parse(input: ParseStream) -> Result<Self> {
715 let modifier: TraitBoundModifier = input.parse()?;
716 let lifetimes: Option<BoundLifetimes> = input.parse()?;
717
718 let mut path: Path = input.parse()?;
719 if path.segments.last().unwrap().value().arguments.is_empty()
720 && input.peek(token::Paren)
721 {
722 let parenthesized = PathArguments::Parenthesized(input.parse()?);
723 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
724 }
725
726 Ok(TraitBound {
727 paren_token: None,
728 modifier: modifier,
729 lifetimes: lifetimes,
730 path: path,
731 })
732 }
733 }
734
735 impl Parse for TraitBoundModifier {
736 fn parse(input: ParseStream) -> Result<Self> {
737 if input.peek(Token![?]) {
738 input.parse().map(TraitBoundModifier::Maybe)
739 } else {
740 Ok(TraitBoundModifier::None)
741 }
742 }
743 }
744
745 impl Parse for ConstParam {
746 fn parse(input: ParseStream) -> Result<Self> {
747 let mut default = None;
748 Ok(ConstParam {
749 attrs: input.call(Attribute::parse_outer)?,
750 const_token: input.parse()?,
751 ident: input.parse()?,
752 colon_token: input.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400753 ty: input.parse()?,
David Tolnay1b8e2852018-08-26 08:25:18 -0400754 eq_token: {
755 if input.peek(Token![=]) {
756 let eq_token = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -0700757 default = Some(input.parse::<Expr>()?);
David Tolnay1b8e2852018-08-26 08:25:18 -0400758 Some(eq_token)
759 } else {
760 None
761 }
762 },
763 default: default,
764 })
765 }
766 }
767
768 impl Parse for WhereClause {
769 fn parse(input: ParseStream) -> Result<Self> {
770 Ok(WhereClause {
771 where_token: input.parse()?,
772 predicates: {
773 let mut predicates = Punctuated::new();
774 loop {
David Tolnay38012de2018-09-02 13:32:47 -0700775 if input.is_empty()
776 || input.peek(token::Brace)
David Tolnay1b8e2852018-08-26 08:25:18 -0400777 || input.peek(Token![,])
778 || input.peek(Token![;])
779 || input.peek(Token![:]) && !input.peek(Token![::])
780 || input.peek(Token![=])
781 {
782 break;
783 }
784 let value = input.parse()?;
785 predicates.push_value(value);
786 if !input.peek(Token![,]) {
787 break;
788 }
789 let punct = input.parse()?;
790 predicates.push_punct(punct);
791 }
792 predicates
793 },
794 })
795 }
796 }
797
798 impl Parse for Option<WhereClause> {
799 fn parse(input: ParseStream) -> Result<Self> {
800 if input.peek(Token![where]) {
801 input.parse().map(Some)
802 } else {
803 Ok(None)
804 }
805 }
806 }
807
808 impl Parse for WherePredicate {
809 fn parse(input: ParseStream) -> Result<Self> {
David Tolnay66cb0c42018-08-31 09:01:30 -0700810 if input.peek(Lifetime) && input.peek2(Token![:]) {
David Tolnay1b8e2852018-08-26 08:25:18 -0400811 Ok(WherePredicate::Lifetime(PredicateLifetime {
812 lifetime: input.parse()?,
813 colon_token: input.parse()?,
814 bounds: {
815 let mut bounds = Punctuated::new();
816 loop {
817 if input.peek(token::Brace)
818 || input.peek(Token![,])
819 || input.peek(Token![;])
820 || input.peek(Token![:])
821 || input.peek(Token![=])
822 {
823 break;
824 }
825 let value = input.parse()?;
826 bounds.push_value(value);
827 if !input.peek(Token![+]) {
828 break;
829 }
830 let punct = input.parse()?;
831 bounds.push_punct(punct);
832 }
833 bounds
834 },
835 }))
836 } else {
837 Ok(WherePredicate::Type(PredicateType {
838 lifetimes: input.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400839 bounded_ty: input.parse()?,
David Tolnay1b8e2852018-08-26 08:25:18 -0400840 colon_token: input.parse()?,
841 bounds: {
842 let mut bounds = Punctuated::new();
843 loop {
844 if input.peek(token::Brace)
845 || input.peek(Token![,])
846 || input.peek(Token![;])
847 || input.peek(Token![:]) && !input.peek(Token![::])
848 || input.peek(Token![=])
849 {
850 break;
851 }
852 let value = input.parse()?;
853 bounds.push_value(value);
854 if !input.peek(Token![+]) {
855 break;
856 }
857 let punct = input.parse()?;
858 bounds.push_punct(punct);
859 }
860 bounds
861 },
862 }))
863 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800864 }
Alex Crichton954046c2017-05-30 21:49:42 -0700865 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700866}
David Tolnay87d0b442016-09-04 11:52:12 -0700867
868#[cfg(feature = "printing")]
869mod printing {
870 use super::*;
David Tolnay64023912018-08-31 09:51:12 -0700871
Alex Crichtona74a1c82018-05-16 10:20:44 -0700872 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700873 use quote::{ToTokens, TokenStreamExt};
David Tolnay87d0b442016-09-04 11:52:12 -0700874
David Tolnay64023912018-08-31 09:51:12 -0700875 use attr::FilterAttrs;
876 use print::TokensOrDefault;
877
David Tolnay8ef93042016-09-04 14:08:40 -0700878 impl ToTokens for Generics {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700879 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -0800880 if self.params.is_empty() {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400881 return;
882 }
883
Alex Crichton259ee532017-07-14 06:51:02 -0700884 TokensOrDefault(&self.lt_token).to_tokens(tokens);
David Tolnay298570b2018-01-11 16:38:36 -0800885
886 // Print lifetimes before types and consts, regardless of their
887 // order in self.params.
888 //
889 // TODO: ordering rules for const parameters vs type parameters have
890 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
891 let mut trailing_or_empty = true;
892 for param in self.params.pairs() {
893 if let GenericParam::Lifetime(_) = **param.value() {
894 param.to_tokens(tokens);
895 trailing_or_empty = param.punct().is_some();
896 }
897 }
898 for param in self.params.pairs() {
899 match **param.value() {
900 GenericParam::Type(_) | GenericParam::Const(_) => {
901 if !trailing_or_empty {
902 <Token![,]>::default().to_tokens(tokens);
903 trailing_or_empty = true;
904 }
905 param.to_tokens(tokens);
906 }
907 GenericParam::Lifetime(_) => {}
908 }
909 }
910
Alex Crichton259ee532017-07-14 06:51:02 -0700911 TokensOrDefault(&self.gt_token).to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700912 }
913 }
914
David Tolnaye7678922016-10-13 20:44:03 -0700915 impl<'a> ToTokens for ImplGenerics<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700916 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -0800917 if self.0.params.is_empty() {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400918 return;
919 }
920
Alex Crichton259ee532017-07-14 06:51:02 -0700921 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnay225aca62018-01-11 20:51:46 -0800922
923 // Print lifetimes before types and consts, regardless of their
924 // order in self.params.
925 //
926 // TODO: ordering rules for const parameters vs type parameters have
927 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
928 let mut trailing_or_empty = true;
David Tolnay56080682018-01-06 14:01:52 -0800929 for param in self.0.params.pairs() {
David Tolnay225aca62018-01-11 20:51:46 -0800930 if let GenericParam::Lifetime(_) = **param.value() {
931 param.to_tokens(tokens);
932 trailing_or_empty = param.punct().is_some();
933 }
934 }
935 for param in self.0.params.pairs() {
936 if let GenericParam::Lifetime(_) = **param.value() {
937 continue;
938 }
939 if !trailing_or_empty {
940 <Token![,]>::default().to_tokens(tokens);
941 trailing_or_empty = true;
942 }
David Tolnay56080682018-01-06 14:01:52 -0800943 match **param.value() {
David Tolnay225aca62018-01-11 20:51:46 -0800944 GenericParam::Lifetime(_) => unreachable!(),
David Tolnayc2f1aba2017-11-12 20:29:22 -0800945 GenericParam::Type(ref param) => {
946 // Leave off the type parameter defaults
947 tokens.append_all(param.attrs.outer());
948 param.ident.to_tokens(tokens);
949 if !param.bounds.is_empty() {
950 TokensOrDefault(&param.colon_token).to_tokens(tokens);
951 param.bounds.to_tokens(tokens);
952 }
953 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500954 GenericParam::Const(ref param) => {
955 // Leave off the const parameter defaults
956 tokens.append_all(param.attrs.outer());
957 param.const_token.to_tokens(tokens);
958 param.ident.to_tokens(tokens);
959 param.colon_token.to_tokens(tokens);
960 param.ty.to_tokens(tokens);
961 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400962 }
David Tolnayf2cfd722017-12-31 18:02:51 -0500963 param.punct().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700964 }
David Tolnay225aca62018-01-11 20:51:46 -0800965
Alex Crichton259ee532017-07-14 06:51:02 -0700966 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700967 }
968 }
969
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800970 impl<'a> ToTokens for TypeGenerics<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700971 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -0800972 if self.0.params.is_empty() {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400973 return;
974 }
975
Alex Crichton259ee532017-07-14 06:51:02 -0700976 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnay225aca62018-01-11 20:51:46 -0800977
978 // Print lifetimes before types and consts, regardless of their
979 // order in self.params.
980 //
981 // TODO: ordering rules for const parameters vs type parameters have
982 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
983 let mut trailing_or_empty = true;
David Tolnay56080682018-01-06 14:01:52 -0800984 for param in self.0.params.pairs() {
David Tolnay225aca62018-01-11 20:51:46 -0800985 if let GenericParam::Lifetime(ref def) = **param.value() {
986 // Leave off the lifetime bounds and attributes
987 def.lifetime.to_tokens(tokens);
988 param.punct().to_tokens(tokens);
989 trailing_or_empty = param.punct().is_some();
990 }
991 }
992 for param in self.0.params.pairs() {
993 if let GenericParam::Lifetime(_) = **param.value() {
994 continue;
995 }
996 if !trailing_or_empty {
997 <Token![,]>::default().to_tokens(tokens);
998 trailing_or_empty = true;
999 }
David Tolnay56080682018-01-06 14:01:52 -08001000 match **param.value() {
David Tolnay225aca62018-01-11 20:51:46 -08001001 GenericParam::Lifetime(_) => unreachable!(),
David Tolnayc2f1aba2017-11-12 20:29:22 -08001002 GenericParam::Type(ref param) => {
1003 // Leave off the type parameter defaults
1004 param.ident.to_tokens(tokens);
1005 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -05001006 GenericParam::Const(ref param) => {
1007 // Leave off the const parameter defaults
1008 param.ident.to_tokens(tokens);
1009 }
David Tolnayc2f1aba2017-11-12 20:29:22 -08001010 }
David Tolnayf2cfd722017-12-31 18:02:51 -05001011 param.punct().to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001012 }
David Tolnay225aca62018-01-11 20:51:46 -08001013
Alex Crichton259ee532017-07-14 06:51:02 -07001014 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -07001015 }
1016 }
1017
David Tolnayc879a502017-01-25 15:51:32 -08001018 impl<'a> ToTokens for Turbofish<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001019 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -08001020 if !self.0.params.is_empty() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001021 <Token![::]>::default().to_tokens(tokens);
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001022 TypeGenerics(self.0).to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -08001023 }
1024 }
1025 }
1026
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001027 impl ToTokens for BoundLifetimes {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001028 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001029 self.for_token.to_tokens(tokens);
1030 self.lt_token.to_tokens(tokens);
1031 self.lifetimes.to_tokens(tokens);
1032 self.gt_token.to_tokens(tokens);
1033 }
1034 }
1035
David Tolnay87d0b442016-09-04 11:52:12 -07001036 impl ToTokens for LifetimeDef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001037 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye7678922016-10-13 20:44:03 -07001038 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -07001039 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001040 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001041 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001042 self.bounds.to_tokens(tokens);
1043 }
David Tolnay87d0b442016-09-04 11:52:12 -07001044 }
1045 }
1046
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001047 impl ToTokens for TypeParam {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001048 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye7678922016-10-13 20:44:03 -07001049 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -07001050 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001051 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001052 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001053 self.bounds.to_tokens(tokens);
1054 }
1055 if self.default.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001056 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001057 self.default.to_tokens(tokens);
1058 }
David Tolnay8ef93042016-09-04 14:08:40 -07001059 }
1060 }
1061
David Tolnay40fb8ce2018-01-02 10:53:46 -08001062 impl ToTokens for TraitBound {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001063 fn to_tokens(&self, tokens: &mut TokenStream) {
1064 let to_tokens = |tokens: &mut TokenStream| {
David Tolnayc1f5d5d2018-03-31 22:17:56 +02001065 self.modifier.to_tokens(tokens);
1066 self.lifetimes.to_tokens(tokens);
1067 self.path.to_tokens(tokens);
1068 };
1069 match self.paren_token {
David Tolnay997c6cb2018-03-31 22:49:52 +02001070 Some(ref paren) => paren.surround(tokens, to_tokens),
David Tolnayc1f5d5d2018-03-31 22:17:56 +02001071 None => to_tokens(tokens),
1072 }
David Tolnay55337722016-09-11 12:58:56 -07001073 }
1074 }
1075
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001076 impl ToTokens for TraitBoundModifier {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001077 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001078 match *self {
1079 TraitBoundModifier::None => {}
1080 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
1081 }
1082 }
1083 }
1084
Nika Layzellf1fdc0b2017-12-04 19:58:32 -05001085 impl ToTokens for ConstParam {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001086 fn to_tokens(&self, tokens: &mut TokenStream) {
Nika Layzellf1fdc0b2017-12-04 19:58:32 -05001087 tokens.append_all(self.attrs.outer());
1088 self.const_token.to_tokens(tokens);
1089 self.ident.to_tokens(tokens);
1090 self.colon_token.to_tokens(tokens);
1091 self.ty.to_tokens(tokens);
1092 if self.default.is_some() {
David Tolnay78ee5202017-12-04 22:17:54 -08001093 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Nika Layzellf1fdc0b2017-12-04 19:58:32 -05001094 self.default.to_tokens(tokens);
1095 }
1096 }
1097 }
1098
David Tolnay55337722016-09-11 12:58:56 -07001099 impl ToTokens for WhereClause {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001100 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2a0b02f2018-03-09 00:50:45 -08001101 if !self.predicates.is_empty() {
1102 self.where_token.to_tokens(tokens);
1103 self.predicates.to_tokens(tokens);
1104 }
David Tolnay87d0b442016-09-04 11:52:12 -07001105 }
1106 }
David Tolnay8ef93042016-09-04 14:08:40 -07001107
David Tolnayd4add852018-01-01 20:13:24 -08001108 impl ToTokens for PredicateType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001109 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay40fb8ce2018-01-02 10:53:46 -08001110 self.lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -07001111 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001112 self.colon_token.to_tokens(tokens);
1113 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -07001114 }
1115 }
1116
David Tolnayd4add852018-01-01 20:13:24 -08001117 impl ToTokens for PredicateLifetime {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001118 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8ef93042016-09-04 14:08:40 -07001119 self.lifetime.to_tokens(tokens);
David Tolnay1b8e2852018-08-26 08:25:18 -04001120 self.colon_token.to_tokens(tokens);
1121 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -07001122 }
1123 }
David Tolnayf8e08832017-01-23 00:04:32 -08001124
David Tolnayd4add852018-01-01 20:13:24 -08001125 impl ToTokens for PredicateEq {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001126 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayf8e08832017-01-23 00:04:32 -08001127 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001128 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -08001129 self.rhs_ty.to_tokens(tokens);
1130 }
1131 }
David Tolnay87d0b442016-09-04 11:52:12 -07001132}