blob: 54761928e1f25fe30d97a14ee4d6584544c3403b [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 /// ```
Alex Crichtonf0fea9a2018-05-17 11:19:34 -0700304 /// # extern crate proc_macro2;
David Tolnayb153dbc2016-10-04 23:39:10 -0700305 /// # extern crate syn;
306 /// # #[macro_use]
307 /// # extern crate quote;
Alex Crichtonf0fea9a2018-05-17 11:19:34 -0700308 /// # use proc_macro2::{Span, Ident};
David Tolnayb153dbc2016-10-04 23:39:10 -0700309 /// # fn main() {
310 /// # let generics: syn::Generics = Default::default();
Alex Crichtonf0fea9a2018-05-17 11:19:34 -0700311 /// # let name = Ident::new("MyType", Span::call_site());
David Tolnayb153dbc2016-10-04 23:39:10 -0700312 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
313 /// quote! {
314 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
315 /// // ...
316 /// }
317 /// }
318 /// # ;
319 /// # }
320 /// ```
David Tolnay461d98e2018-01-07 11:07:19 -0800321 ///
322 /// *This method is available if Syn is built with the `"derive"` or
323 /// `"full"` feature and the `"printing"` feature.*
David Tolnayac997dd2017-12-27 23:18:22 -0500324 pub fn split_for_impl(&self) -> (ImplGenerics, TypeGenerics, Option<&WhereClause>) {
David Tolnay61037c62018-01-05 16:21:03 -0800325 (
326 ImplGenerics(self),
327 TypeGenerics(self),
328 self.where_clause.as_ref(),
329 )
David Tolnayb153dbc2016-10-04 23:39:10 -0700330 }
331}
332
David Tolnaye95cc9f2017-01-25 15:57:09 -0800333#[cfg(feature = "printing")]
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800334impl<'a> TypeGenerics<'a> {
David Tolnayc879a502017-01-25 15:51:32 -0800335 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800336 ///
337 /// *This method is available if Syn is built with the `"derive"` or
338 /// `"full"` feature and the `"printing"` feature.*
David Tolnayc879a502017-01-25 15:51:32 -0800339 pub fn as_turbofish(&self) -> Turbofish {
340 Turbofish(self.0)
341 }
342}
343
Alex Crichton62a0a592017-05-22 13:58:53 -0700344ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800345 /// A set of bound lifetimes: `for<'a, 'b, 'c>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800346 ///
347 /// *This type is available if Syn is built with the `"derive"` or `"full"`
348 /// feature.*
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700349 #[derive(Default)]
350 pub struct BoundLifetimes {
David Tolnayf8db7ba2017-11-11 22:52:16 -0800351 pub for_token: Token![for],
352 pub lt_token: Token![<],
David Tolnayf2cfd722017-12-31 18:02:51 -0500353 pub lifetimes: Punctuated<LifetimeDef, Token![,]>,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800354 pub gt_token: Token![>],
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700355 }
356}
357
David Tolnayf9505b52016-10-02 09:18:52 -0700358impl LifetimeDef {
David Tolnay63e3dee2017-06-03 20:13:17 -0700359 pub fn new(lifetime: Lifetime) -> Self {
David Tolnayf9505b52016-10-02 09:18:52 -0700360 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -0700361 attrs: Vec::new(),
David Tolnay63e3dee2017-06-03 20:13:17 -0700362 lifetime: lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700363 colon_token: None,
David Tolnayf2cfd722017-12-31 18:02:51 -0500364 bounds: Punctuated::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700365 }
366 }
367}
368
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800369impl From<Ident> for TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700370 fn from(ident: Ident) -> Self {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800371 TypeParam {
Ted Driggs0547d0d2017-04-20 10:00:12 -0700372 attrs: vec![],
373 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700374 colon_token: None,
David Tolnayf2cfd722017-12-31 18:02:51 -0500375 bounds: Punctuated::new(),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700376 eq_token: None,
Ted Driggs0547d0d2017-04-20 10:00:12 -0700377 default: None,
378 }
379 }
380}
381
David Tolnay40fb8ce2018-01-02 10:53:46 -0800382ast_enum_of_structs! {
David Tolnayed906d12018-01-07 01:20:29 -0800383 /// A trait or lifetime used as a bound on a type parameter.
David Tolnay461d98e2018-01-07 11:07:19 -0800384 ///
385 /// *This type is available if Syn is built with the `"derive"` or `"full"`
386 /// feature.*
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800387 pub enum TypeParamBound {
David Tolnay40fb8ce2018-01-02 10:53:46 -0800388 pub Trait(TraitBound),
389 pub Lifetime(Lifetime),
390 }
391}
392
393ast_struct! {
David Tolnayed906d12018-01-07 01:20:29 -0800394 /// A trait used as a bound on a type parameter.
David Tolnay461d98e2018-01-07 11:07:19 -0800395 ///
396 /// *This type is available if Syn is built with the `"derive"` or `"full"`
397 /// feature.*
David Tolnay40fb8ce2018-01-02 10:53:46 -0800398 pub struct TraitBound {
David Tolnayc1f5d5d2018-03-31 22:17:56 +0200399 pub paren_token: Option<token::Paren>,
David Tolnay40fb8ce2018-01-02 10:53:46 -0800400 pub modifier: TraitBoundModifier,
401 /// The `for<'a>` in `for<'a> Foo<&'a T>`
402 pub lifetimes: Option<BoundLifetimes>,
403 /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`
404 pub path: Path,
Alex Crichton62a0a592017-05-22 13:58:53 -0700405 }
David Tolnay55337722016-09-11 12:58:56 -0700406}
407
Alex Crichton62a0a592017-05-22 13:58:53 -0700408ast_enum! {
David Tolnayed906d12018-01-07 01:20:29 -0800409 /// A modifier on a trait bound, currently only used for the `?` in
410 /// `?Sized`.
David Tolnay461d98e2018-01-07 11:07:19 -0800411 ///
412 /// *This type is available if Syn is built with the `"derive"` or `"full"`
413 /// feature.*
Alex Crichton2e0229c2017-05-23 09:34:50 -0700414 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700415 pub enum TraitBoundModifier {
416 None,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800417 Maybe(Token![?]),
Alex Crichton62a0a592017-05-22 13:58:53 -0700418 }
David Tolnay55337722016-09-11 12:58:56 -0700419}
420
Alex Crichton62a0a592017-05-22 13:58:53 -0700421ast_struct! {
David Tolnay05658502018-01-07 09:56:37 -0800422 /// A `where` clause in a definition: `where T: Deserialize<'de>, D:
423 /// 'static`.
David Tolnay461d98e2018-01-07 11:07:19 -0800424 ///
425 /// *This type is available if Syn is built with the `"derive"` or `"full"`
426 /// feature.*
Alex Crichton62a0a592017-05-22 13:58:53 -0700427 pub struct WhereClause {
David Tolnayac997dd2017-12-27 23:18:22 -0500428 pub where_token: Token![where],
David Tolnayf2cfd722017-12-31 18:02:51 -0500429 pub predicates: Punctuated<WherePredicate, Token![,]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700430 }
David Tolnayb79ee962016-09-04 09:39:20 -0700431}
432
Alex Crichton62a0a592017-05-22 13:58:53 -0700433ast_enum_of_structs! {
David Tolnay05658502018-01-07 09:56:37 -0800434 /// A single predicate in a `where` clause: `T: Deserialize<'de>`.
David Tolnay614a0142018-01-07 10:25:43 -0800435 ///
David Tolnay461d98e2018-01-07 11:07:19 -0800436 /// *This type is available if Syn is built with the `"derive"` or `"full"`
437 /// feature.*
438 ///
David Tolnay614a0142018-01-07 10:25:43 -0800439 /// # Syntax tree enum
440 ///
441 /// This type is a [syntax tree enum].
442 ///
443 /// [syntax tree enum]: enum.Expr.html#syntax-tree-enums
Alex Crichton62a0a592017-05-22 13:58:53 -0700444 pub enum WherePredicate {
David Tolnayed906d12018-01-07 01:20:29 -0800445 /// A type predicate in a `where` clause: `for<'c> Foo<'c>: Trait<'c>`.
David Tolnay461d98e2018-01-07 11:07:19 -0800446 ///
447 /// *This type is available if Syn is built with the `"derive"` or
448 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800449 pub Type(PredicateType {
Alex Crichton62a0a592017-05-22 13:58:53 -0700450 /// Any lifetimes from a `for` binding
David Tolnay40fb8ce2018-01-02 10:53:46 -0800451 pub lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700452 /// The type being bounded
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800453 pub bounded_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800454 pub colon_token: Token![:],
Alex Crichton62a0a592017-05-22 13:58:53 -0700455 /// Trait and lifetime bounds (`Clone+Send+'static`)
David Tolnayf2cfd722017-12-31 18:02:51 -0500456 pub bounds: Punctuated<TypeParamBound, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700457 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700458
David Tolnayed906d12018-01-07 01:20:29 -0800459 /// A lifetime predicate in a `where` clause: `'a: 'b + 'c`.
David Tolnay461d98e2018-01-07 11:07:19 -0800460 ///
461 /// *This type is available if Syn is built with the `"derive"` or
462 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800463 pub Lifetime(PredicateLifetime {
Alex Crichton62a0a592017-05-22 13:58:53 -0700464 pub lifetime: Lifetime,
David Tolnay1b8e2852018-08-26 08:25:18 -0400465 pub colon_token: Token![:],
David Tolnayf2cfd722017-12-31 18:02:51 -0500466 pub bounds: Punctuated<Lifetime, Token![+]>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700467 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700468
David Tolnayed906d12018-01-07 01:20:29 -0800469 /// An equality predicate in a `where` clause (unsupported).
David Tolnay461d98e2018-01-07 11:07:19 -0800470 ///
471 /// *This type is available if Syn is built with the `"derive"` or
472 /// `"full"` feature.*
David Tolnayd4add852018-01-01 20:13:24 -0800473 pub Eq(PredicateEq {
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800474 pub lhs_ty: Type,
David Tolnayf8db7ba2017-11-11 22:52:16 -0800475 pub eq_token: Token![=],
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800476 pub rhs_ty: Type,
Alex Crichton62a0a592017-05-22 13:58:53 -0700477 }),
478 }
David Tolnayf8e08832017-01-23 00:04:32 -0800479}
480
David Tolnay86eca752016-09-04 11:26:41 -0700481#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700482pub mod parsing {
483 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700484
David Tolnay1b8e2852018-08-26 08:25:18 -0400485 use parse::{Parse, ParseStream, Result};
Alex Crichton954046c2017-05-30 21:49:42 -0700486
David Tolnay1b8e2852018-08-26 08:25:18 -0400487 impl Parse for Generics {
488 fn parse(input: ParseStream) -> Result<Self> {
489 let mut params = Punctuated::new();
490
491 if !input.peek(Token![<]) {
492 return Ok(Generics {
493 lt_token: None,
494 params: params,
495 gt_token: None,
496 where_clause: None,
497 });
Michael Layzell416724e2017-05-24 21:12:34 -0400498 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800499
David Tolnay1b8e2852018-08-26 08:25:18 -0400500 let lt_token: Token![<] = input.parse()?;
Alex Crichton954046c2017-05-30 21:49:42 -0700501
David Tolnay1b8e2852018-08-26 08:25:18 -0400502 let mut has_type_param = false;
503 loop {
504 if input.peek(Token![>]) {
505 break;
David Tolnay40fb8ce2018-01-02 10:53:46 -0800506 }
David Tolnay1b8e2852018-08-26 08:25:18 -0400507
508 let attrs = input.call(Attribute::parse_outer)?;
509 let lookahead = input.lookahead1();
510 if !has_type_param && lookahead.peek(Lifetime) {
511 params.push_value(GenericParam::Lifetime(LifetimeDef {
512 attrs: attrs,
513 ..input.parse()?
514 }));
515 } else if lookahead.peek(Ident) {
516 has_type_param = true;
517 params.push_value(GenericParam::Type(TypeParam {
518 attrs: attrs,
519 ..input.parse()?
520 }));
521 } else {
522 return Err(lookahead.error());
David Tolnay40fb8ce2018-01-02 10:53:46 -0800523 }
David Tolnay1b8e2852018-08-26 08:25:18 -0400524
525 if input.peek(Token![>]) {
526 break;
527 }
528 let punct = input.parse()?;
529 params.push_punct(punct);
530 }
531
532 let gt_token: Token![>] = input.parse()?;
533
534 Ok(Generics {
535 lt_token: Some(lt_token),
536 params: params,
537 gt_token: Some(gt_token),
538 where_clause: None,
David Tolnay40fb8ce2018-01-02 10:53:46 -0800539 })
David Tolnay40fb8ce2018-01-02 10:53:46 -0800540 }
541 }
542
David Tolnay1b8e2852018-08-26 08:25:18 -0400543 impl Parse for GenericParam {
544 fn parse(input: ParseStream) -> Result<Self> {
545 let attrs = input.call(Attribute::parse_outer)?;
David Tolnay40fb8ce2018-01-02 10:53:46 -0800546
David Tolnay1b8e2852018-08-26 08:25:18 -0400547 let lookahead = input.lookahead1();
548 if lookahead.peek(Ident) {
549 Ok(GenericParam::Type(TypeParam {
David Tolnay78ee5202017-12-04 22:17:54 -0800550 attrs: attrs,
David Tolnay1b8e2852018-08-26 08:25:18 -0400551 ..input.parse()?
552 }))
553 } else if lookahead.peek(Lifetime) {
554 Ok(GenericParam::Lifetime(LifetimeDef {
555 attrs: attrs,
556 ..input.parse()?
557 }))
558 } else if lookahead.peek(Token![const]) {
559 Ok(GenericParam::Const(ConstParam {
560 attrs: attrs,
561 ..input.parse()?
562 }))
563 } else {
564 Err(lookahead.error())
565 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800566 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500567 }
568
David Tolnay1b8e2852018-08-26 08:25:18 -0400569 impl Parse for LifetimeDef {
570 fn parse(input: ParseStream) -> Result<Self> {
571 let has_colon;
572 Ok(LifetimeDef {
573 attrs: input.call(Attribute::parse_outer)?,
574 lifetime: input.parse()?,
575 colon_token: {
576 if input.peek(Token![:]) {
577 has_colon = true;
578 Some(input.parse()?)
579 } else {
580 has_colon = false;
581 None
582 }
583 },
584 bounds: {
585 let mut bounds = Punctuated::new();
586 if has_colon {
587 loop {
588 if input.peek(Token![,]) || input.peek(Token![>]) {
589 break;
590 }
591 let value = input.parse()?;
592 bounds.push_value(value);
593 if !input.peek(Token![+]) {
594 break;
595 }
596 let punct = input.parse()?;
597 bounds.push_punct(punct);
598 }
599 }
600 bounds
601 },
David Tolnayac997dd2017-12-27 23:18:22 -0500602 })
Alex Crichton954046c2017-05-30 21:49:42 -0700603 }
604 }
605
David Tolnay1b8e2852018-08-26 08:25:18 -0400606 impl Parse for BoundLifetimes {
607 fn parse(input: ParseStream) -> Result<Self> {
608 Ok(BoundLifetimes {
609 for_token: input.parse()?,
610 lt_token: input.parse()?,
David Tolnayf5ebc192018-08-30 18:23:46 -0700611 lifetimes: {
612 let mut lifetimes = Punctuated::new();
613 while !input.peek(Token![>]) {
614 lifetimes.push_value(input.parse()?);
615 if input.peek(Token![>]) {
616 break;
617 }
618 lifetimes.push_punct(input.parse()?);
619 }
620 lifetimes
621 },
David Tolnay1b8e2852018-08-26 08:25:18 -0400622 gt_token: input.parse()?,
623 })
624 }
625 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800626
David Tolnay1b8e2852018-08-26 08:25:18 -0400627 impl Parse for Option<BoundLifetimes> {
628 fn parse(input: ParseStream) -> Result<Self> {
629 if input.peek(Token![for]) {
630 input.parse().map(Some)
631 } else {
632 Ok(None)
633 }
634 }
635 }
636
637 impl Parse for TypeParam {
638 fn parse(input: ParseStream) -> Result<Self> {
639 let has_colon;
640 let has_default;
641 Ok(TypeParam {
642 attrs: input.call(Attribute::parse_outer)?,
643 ident: input.parse()?,
644 colon_token: {
645 if input.peek(Token![:]) {
646 has_colon = true;
647 Some(input.parse()?)
648 } else {
649 has_colon = false;
650 None
651 }
652 },
653 bounds: {
654 let mut bounds = Punctuated::new();
655 if has_colon {
656 loop {
657 if input.peek(Token![,]) || input.peek(Token![>]) {
658 break;
659 }
660 let value = input.parse()?;
661 bounds.push_value(value);
662 if !input.peek(Token![+]) {
663 break;
664 }
665 let punct = input.parse()?;
666 bounds.push_punct(punct);
667 }
668 }
669 bounds
670 },
671 eq_token: {
672 if input.peek(Token![=]) {
673 has_default = true;
674 Some(input.parse()?)
675 } else {
676 has_default = false;
677 None
678 }
679 },
680 default: {
681 if has_default {
David Tolnaya7d69fc2018-08-26 13:30:24 -0400682 Some(input.parse()?)
David Tolnay1b8e2852018-08-26 08:25:18 -0400683 } else {
684 None
685 }
686 },
687 })
688 }
689 }
690
691 impl Parse for TypeParamBound {
692 fn parse(input: ParseStream) -> Result<Self> {
693 if input.peek(Lifetime) {
694 return input.parse().map(TypeParamBound::Lifetime);
695 }
696
697 if input.peek(token::Paren) {
698 let content;
699 let paren_token = parenthesized!(content in input);
700 let mut bound: TraitBound = content.parse()?;
701 bound.paren_token = Some(paren_token);
702 return Ok(TypeParamBound::Trait(bound));
703 }
704
705 input.parse().map(TypeParamBound::Trait)
706 }
707 }
708
709 impl Parse for TraitBound {
710 fn parse(input: ParseStream) -> Result<Self> {
711 let modifier: TraitBoundModifier = input.parse()?;
712 let lifetimes: Option<BoundLifetimes> = input.parse()?;
713
714 let mut path: Path = input.parse()?;
715 if path.segments.last().unwrap().value().arguments.is_empty()
716 && input.peek(token::Paren)
717 {
718 let parenthesized = PathArguments::Parenthesized(input.parse()?);
719 path.segments.last_mut().unwrap().value_mut().arguments = parenthesized;
720 }
721
722 Ok(TraitBound {
723 paren_token: None,
724 modifier: modifier,
725 lifetimes: lifetimes,
726 path: path,
727 })
728 }
729 }
730
731 impl Parse for TraitBoundModifier {
732 fn parse(input: ParseStream) -> Result<Self> {
733 if input.peek(Token![?]) {
734 input.parse().map(TraitBoundModifier::Maybe)
735 } else {
736 Ok(TraitBoundModifier::None)
737 }
738 }
739 }
740
741 impl Parse for ConstParam {
742 fn parse(input: ParseStream) -> Result<Self> {
743 let mut default = None;
744 Ok(ConstParam {
745 attrs: input.call(Attribute::parse_outer)?,
746 const_token: input.parse()?,
747 ident: input.parse()?,
748 colon_token: input.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400749 ty: input.parse()?,
David Tolnay1b8e2852018-08-26 08:25:18 -0400750 eq_token: {
751 if input.peek(Token![=]) {
752 let eq_token = input.parse()?;
David Tolnay9389c382018-08-27 09:13:37 -0700753 default = Some(input.parse::<Expr>()?);
David Tolnay1b8e2852018-08-26 08:25:18 -0400754 Some(eq_token)
755 } else {
756 None
757 }
758 },
759 default: default,
760 })
761 }
762 }
763
764 impl Parse for WhereClause {
765 fn parse(input: ParseStream) -> Result<Self> {
766 Ok(WhereClause {
767 where_token: input.parse()?,
768 predicates: {
769 let mut predicates = Punctuated::new();
770 loop {
771 if input.peek(token::Brace)
772 || input.peek(Token![,])
773 || input.peek(Token![;])
774 || input.peek(Token![:]) && !input.peek(Token![::])
775 || input.peek(Token![=])
776 {
777 break;
778 }
779 let value = input.parse()?;
780 predicates.push_value(value);
781 if !input.peek(Token![,]) {
782 break;
783 }
784 let punct = input.parse()?;
785 predicates.push_punct(punct);
786 }
787 predicates
788 },
789 })
790 }
791 }
792
793 impl Parse for Option<WhereClause> {
794 fn parse(input: ParseStream) -> Result<Self> {
795 if input.peek(Token![where]) {
796 input.parse().map(Some)
797 } else {
798 Ok(None)
799 }
800 }
801 }
802
803 impl Parse for WherePredicate {
804 fn parse(input: ParseStream) -> Result<Self> {
805 if input.peek(Lifetime) && input.peek3(Token![:]) {
806 Ok(WherePredicate::Lifetime(PredicateLifetime {
807 lifetime: input.parse()?,
808 colon_token: input.parse()?,
809 bounds: {
810 let mut bounds = Punctuated::new();
811 loop {
812 if input.peek(token::Brace)
813 || input.peek(Token![,])
814 || input.peek(Token![;])
815 || input.peek(Token![:])
816 || input.peek(Token![=])
817 {
818 break;
819 }
820 let value = input.parse()?;
821 bounds.push_value(value);
822 if !input.peek(Token![+]) {
823 break;
824 }
825 let punct = input.parse()?;
826 bounds.push_punct(punct);
827 }
828 bounds
829 },
830 }))
831 } else {
832 Ok(WherePredicate::Type(PredicateType {
833 lifetimes: input.parse()?,
David Tolnaya7d69fc2018-08-26 13:30:24 -0400834 bounded_ty: input.parse()?,
David Tolnay1b8e2852018-08-26 08:25:18 -0400835 colon_token: input.parse()?,
836 bounds: {
837 let mut bounds = Punctuated::new();
838 loop {
839 if input.peek(token::Brace)
840 || input.peek(Token![,])
841 || input.peek(Token![;])
842 || input.peek(Token![:]) && !input.peek(Token![::])
843 || input.peek(Token![=])
844 {
845 break;
846 }
847 let value = input.parse()?;
848 bounds.push_value(value);
849 if !input.peek(Token![+]) {
850 break;
851 }
852 let punct = input.parse()?;
853 bounds.push_punct(punct);
854 }
855 bounds
856 },
857 }))
858 }
Sergio Benitez5680d6a2017-12-29 11:20:29 -0800859 }
Alex Crichton954046c2017-05-30 21:49:42 -0700860 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700861}
David Tolnay87d0b442016-09-04 11:52:12 -0700862
863#[cfg(feature = "printing")]
864mod printing {
865 use super::*;
David Tolnaye7678922016-10-13 20:44:03 -0700866 use attr::FilterAttrs;
Alex Crichtona74a1c82018-05-16 10:20:44 -0700867 use proc_macro2::TokenStream;
David Tolnay65fb5662018-05-20 20:02:28 -0700868 use quote::{ToTokens, TokenStreamExt};
David Tolnay87d0b442016-09-04 11:52:12 -0700869
David Tolnay8ef93042016-09-04 14:08:40 -0700870 impl ToTokens for Generics {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700871 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -0800872 if self.params.is_empty() {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400873 return;
874 }
875
Alex Crichton259ee532017-07-14 06:51:02 -0700876 TokensOrDefault(&self.lt_token).to_tokens(tokens);
David Tolnay298570b2018-01-11 16:38:36 -0800877
878 // Print lifetimes before types and consts, regardless of their
879 // order in self.params.
880 //
881 // TODO: ordering rules for const parameters vs type parameters have
882 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
883 let mut trailing_or_empty = true;
884 for param in self.params.pairs() {
885 if let GenericParam::Lifetime(_) = **param.value() {
886 param.to_tokens(tokens);
887 trailing_or_empty = param.punct().is_some();
888 }
889 }
890 for param in self.params.pairs() {
891 match **param.value() {
892 GenericParam::Type(_) | GenericParam::Const(_) => {
893 if !trailing_or_empty {
894 <Token![,]>::default().to_tokens(tokens);
895 trailing_or_empty = true;
896 }
897 param.to_tokens(tokens);
898 }
899 GenericParam::Lifetime(_) => {}
900 }
901 }
902
Alex Crichton259ee532017-07-14 06:51:02 -0700903 TokensOrDefault(&self.gt_token).to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700904 }
905 }
906
David Tolnaye7678922016-10-13 20:44:03 -0700907 impl<'a> ToTokens for ImplGenerics<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700908 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -0800909 if self.0.params.is_empty() {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400910 return;
911 }
912
Alex Crichton259ee532017-07-14 06:51:02 -0700913 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnay225aca62018-01-11 20:51:46 -0800914
915 // Print lifetimes before types and consts, regardless of their
916 // order in self.params.
917 //
918 // TODO: ordering rules for const parameters vs type parameters have
919 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
920 let mut trailing_or_empty = true;
David Tolnay56080682018-01-06 14:01:52 -0800921 for param in self.0.params.pairs() {
David Tolnay225aca62018-01-11 20:51:46 -0800922 if let GenericParam::Lifetime(_) = **param.value() {
923 param.to_tokens(tokens);
924 trailing_or_empty = param.punct().is_some();
925 }
926 }
927 for param in self.0.params.pairs() {
928 if let GenericParam::Lifetime(_) = **param.value() {
929 continue;
930 }
931 if !trailing_or_empty {
932 <Token![,]>::default().to_tokens(tokens);
933 trailing_or_empty = true;
934 }
David Tolnay56080682018-01-06 14:01:52 -0800935 match **param.value() {
David Tolnay225aca62018-01-11 20:51:46 -0800936 GenericParam::Lifetime(_) => unreachable!(),
David Tolnayc2f1aba2017-11-12 20:29:22 -0800937 GenericParam::Type(ref param) => {
938 // Leave off the type parameter defaults
939 tokens.append_all(param.attrs.outer());
940 param.ident.to_tokens(tokens);
941 if !param.bounds.is_empty() {
942 TokensOrDefault(&param.colon_token).to_tokens(tokens);
943 param.bounds.to_tokens(tokens);
944 }
945 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500946 GenericParam::Const(ref param) => {
947 // Leave off the const parameter defaults
948 tokens.append_all(param.attrs.outer());
949 param.const_token.to_tokens(tokens);
950 param.ident.to_tokens(tokens);
951 param.colon_token.to_tokens(tokens);
952 param.ty.to_tokens(tokens);
953 }
Michael Layzell3936ceb2017-07-08 00:28:36 -0400954 }
David Tolnayf2cfd722017-12-31 18:02:51 -0500955 param.punct().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700956 }
David Tolnay225aca62018-01-11 20:51:46 -0800957
Alex Crichton259ee532017-07-14 06:51:02 -0700958 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700959 }
960 }
961
David Tolnayfd6bf5c2017-11-12 09:41:14 -0800962 impl<'a> ToTokens for TypeGenerics<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -0700963 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -0800964 if self.0.params.is_empty() {
Michael Layzell3936ceb2017-07-08 00:28:36 -0400965 return;
966 }
967
Alex Crichton259ee532017-07-14 06:51:02 -0700968 TokensOrDefault(&self.0.lt_token).to_tokens(tokens);
David Tolnay225aca62018-01-11 20:51:46 -0800969
970 // Print lifetimes before types and consts, regardless of their
971 // order in self.params.
972 //
973 // TODO: ordering rules for const parameters vs type parameters have
974 // not been settled yet. https://github.com/rust-lang/rust/issues/44580
975 let mut trailing_or_empty = true;
David Tolnay56080682018-01-06 14:01:52 -0800976 for param in self.0.params.pairs() {
David Tolnay225aca62018-01-11 20:51:46 -0800977 if let GenericParam::Lifetime(ref def) = **param.value() {
978 // Leave off the lifetime bounds and attributes
979 def.lifetime.to_tokens(tokens);
980 param.punct().to_tokens(tokens);
981 trailing_or_empty = param.punct().is_some();
982 }
983 }
984 for param in self.0.params.pairs() {
985 if let GenericParam::Lifetime(_) = **param.value() {
986 continue;
987 }
988 if !trailing_or_empty {
989 <Token![,]>::default().to_tokens(tokens);
990 trailing_or_empty = true;
991 }
David Tolnay56080682018-01-06 14:01:52 -0800992 match **param.value() {
David Tolnay225aca62018-01-11 20:51:46 -0800993 GenericParam::Lifetime(_) => unreachable!(),
David Tolnayc2f1aba2017-11-12 20:29:22 -0800994 GenericParam::Type(ref param) => {
995 // Leave off the type parameter defaults
996 param.ident.to_tokens(tokens);
997 }
Nika Layzellf1fdc0b2017-12-04 19:58:32 -0500998 GenericParam::Const(ref param) => {
999 // Leave off the const parameter defaults
1000 param.ident.to_tokens(tokens);
1001 }
David Tolnayc2f1aba2017-11-12 20:29:22 -08001002 }
David Tolnayf2cfd722017-12-31 18:02:51 -05001003 param.punct().to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001004 }
David Tolnay225aca62018-01-11 20:51:46 -08001005
Alex Crichton259ee532017-07-14 06:51:02 -07001006 TokensOrDefault(&self.0.gt_token).to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -07001007 }
1008 }
1009
David Tolnayc879a502017-01-25 15:51:32 -08001010 impl<'a> ToTokens for Turbofish<'a> {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001011 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay7c6e0f42018-01-11 20:52:52 -08001012 if !self.0.params.is_empty() {
David Tolnayf8db7ba2017-11-11 22:52:16 -08001013 <Token![::]>::default().to_tokens(tokens);
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001014 TypeGenerics(self.0).to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -08001015 }
1016 }
1017 }
1018
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001019 impl ToTokens for BoundLifetimes {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001020 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001021 self.for_token.to_tokens(tokens);
1022 self.lt_token.to_tokens(tokens);
1023 self.lifetimes.to_tokens(tokens);
1024 self.gt_token.to_tokens(tokens);
1025 }
1026 }
1027
David Tolnay87d0b442016-09-04 11:52:12 -07001028 impl ToTokens for LifetimeDef {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001029 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye7678922016-10-13 20:44:03 -07001030 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -07001031 self.lifetime.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001032 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001033 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001034 self.bounds.to_tokens(tokens);
1035 }
David Tolnay87d0b442016-09-04 11:52:12 -07001036 }
1037 }
1038
David Tolnayfd6bf5c2017-11-12 09:41:14 -08001039 impl ToTokens for TypeParam {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001040 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnaye7678922016-10-13 20:44:03 -07001041 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -07001042 self.ident.to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001043 if !self.bounds.is_empty() {
Alex Crichton259ee532017-07-14 06:51:02 -07001044 TokensOrDefault(&self.colon_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001045 self.bounds.to_tokens(tokens);
1046 }
1047 if self.default.is_some() {
Alex Crichton259ee532017-07-14 06:51:02 -07001048 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Michael Layzell3936ceb2017-07-08 00:28:36 -04001049 self.default.to_tokens(tokens);
1050 }
David Tolnay8ef93042016-09-04 14:08:40 -07001051 }
1052 }
1053
David Tolnay40fb8ce2018-01-02 10:53:46 -08001054 impl ToTokens for TraitBound {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001055 fn to_tokens(&self, tokens: &mut TokenStream) {
1056 let to_tokens = |tokens: &mut TokenStream| {
David Tolnayc1f5d5d2018-03-31 22:17:56 +02001057 self.modifier.to_tokens(tokens);
1058 self.lifetimes.to_tokens(tokens);
1059 self.path.to_tokens(tokens);
1060 };
1061 match self.paren_token {
David Tolnay997c6cb2018-03-31 22:49:52 +02001062 Some(ref paren) => paren.surround(tokens, to_tokens),
David Tolnayc1f5d5d2018-03-31 22:17:56 +02001063 None => to_tokens(tokens),
1064 }
David Tolnay55337722016-09-11 12:58:56 -07001065 }
1066 }
1067
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001068 impl ToTokens for TraitBoundModifier {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001069 fn to_tokens(&self, tokens: &mut TokenStream) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001070 match *self {
1071 TraitBoundModifier::None => {}
1072 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
1073 }
1074 }
1075 }
1076
Nika Layzellf1fdc0b2017-12-04 19:58:32 -05001077 impl ToTokens for ConstParam {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001078 fn to_tokens(&self, tokens: &mut TokenStream) {
Nika Layzellf1fdc0b2017-12-04 19:58:32 -05001079 tokens.append_all(self.attrs.outer());
1080 self.const_token.to_tokens(tokens);
1081 self.ident.to_tokens(tokens);
1082 self.colon_token.to_tokens(tokens);
1083 self.ty.to_tokens(tokens);
1084 if self.default.is_some() {
David Tolnay78ee5202017-12-04 22:17:54 -08001085 TokensOrDefault(&self.eq_token).to_tokens(tokens);
Nika Layzellf1fdc0b2017-12-04 19:58:32 -05001086 self.default.to_tokens(tokens);
1087 }
1088 }
1089 }
1090
David Tolnay55337722016-09-11 12:58:56 -07001091 impl ToTokens for WhereClause {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001092 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay2a0b02f2018-03-09 00:50:45 -08001093 if !self.predicates.is_empty() {
1094 self.where_token.to_tokens(tokens);
1095 self.predicates.to_tokens(tokens);
1096 }
David Tolnay87d0b442016-09-04 11:52:12 -07001097 }
1098 }
David Tolnay8ef93042016-09-04 14:08:40 -07001099
David Tolnayd4add852018-01-01 20:13:24 -08001100 impl ToTokens for PredicateType {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001101 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay40fb8ce2018-01-02 10:53:46 -08001102 self.lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -07001103 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001104 self.colon_token.to_tokens(tokens);
1105 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -07001106 }
1107 }
1108
David Tolnayd4add852018-01-01 20:13:24 -08001109 impl ToTokens for PredicateLifetime {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001110 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnay8ef93042016-09-04 14:08:40 -07001111 self.lifetime.to_tokens(tokens);
David Tolnay1b8e2852018-08-26 08:25:18 -04001112 self.colon_token.to_tokens(tokens);
1113 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -07001114 }
1115 }
David Tolnayf8e08832017-01-23 00:04:32 -08001116
David Tolnayd4add852018-01-01 20:13:24 -08001117 impl ToTokens for PredicateEq {
Alex Crichtona74a1c82018-05-16 10:20:44 -07001118 fn to_tokens(&self, tokens: &mut TokenStream) {
David Tolnayf8e08832017-01-23 00:04:32 -08001119 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001120 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -08001121 self.rhs_ty.to_tokens(tokens);
1122 }
1123 }
David Tolnay87d0b442016-09-04 11:52:12 -07001124}