blob: 2c30a5f1c596aac944112a564b5cb84af7a373c3 [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 {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07009 pub lt_token: Option<tokens::Lt>,
10 pub gt_token: Option<tokens::Gt>,
11 pub lifetimes: Delimited<LifetimeDef, tokens::Comma>,
12 pub ty_params: Delimited<TyParam, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -070013 pub where_clause: WhereClause,
14 }
David Tolnayb79ee962016-09-04 09:39:20 -070015}
16
David Tolnaye7678922016-10-13 20:44:03 -070017#[cfg(feature = "printing")]
Alex Crichton2e0229c2017-05-23 09:34:50 -070018ast_struct! {
19 /// Returned by `Generics::split_for_impl`.
20 pub struct ImplGenerics<'a>(&'a Generics);
21}
David Tolnaye7678922016-10-13 20:44:03 -070022
23#[cfg(feature = "printing")]
Alex Crichton2e0229c2017-05-23 09:34:50 -070024ast_struct! {
25 /// Returned by `Generics::split_for_impl`.
26 pub struct TyGenerics<'a>(&'a Generics);
27}
David Tolnaye7678922016-10-13 20:44:03 -070028
David Tolnayc879a502017-01-25 15:51:32 -080029#[cfg(feature = "printing")]
Alex Crichton2e0229c2017-05-23 09:34:50 -070030ast_struct! {
31 /// Returned by `TyGenerics::as_turbofish`.
32 pub struct Turbofish<'a>(&'a Generics);
33}
David Tolnayc879a502017-01-25 15:51:32 -080034
David Tolnaye95cc9f2017-01-25 15:57:09 -080035#[cfg(feature = "printing")]
David Tolnayb153dbc2016-10-04 23:39:10 -070036impl Generics {
37 /// Split a type's generics into the pieces required for impl'ing a trait
38 /// for that type.
39 ///
40 /// ```
41 /// # extern crate syn;
42 /// # #[macro_use]
43 /// # extern crate quote;
44 /// # fn main() {
45 /// # let generics: syn::Generics = Default::default();
Alex Crichtonccbb45d2017-05-23 10:58:24 -070046 /// # let name = syn::Ident::from("MyType");
David Tolnayb153dbc2016-10-04 23:39:10 -070047 /// let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
48 /// quote! {
49 /// impl #impl_generics MyTrait for #name #ty_generics #where_clause {
50 /// // ...
51 /// }
52 /// }
53 /// # ;
54 /// # }
55 /// ```
David Tolnaye7678922016-10-13 20:44:03 -070056 pub fn split_for_impl(&self) -> (ImplGenerics, TyGenerics, &WhereClause) {
57 (ImplGenerics(self), TyGenerics(self), &self.where_clause)
David Tolnayb153dbc2016-10-04 23:39:10 -070058 }
59}
60
David Tolnaye95cc9f2017-01-25 15:57:09 -080061#[cfg(feature = "printing")]
David Tolnayc879a502017-01-25 15:51:32 -080062impl<'a> TyGenerics<'a> {
David Tolnayc879a502017-01-25 15:51:32 -080063 /// Turn a type's generics like `<X, Y>` into a turbofish like `::<X, Y>`.
64 pub fn as_turbofish(&self) -> Turbofish {
65 Turbofish(self.0)
66 }
67}
68
Alex Crichton62a0a592017-05-22 13:58:53 -070069ast_struct! {
70 pub struct Lifetime {
71 pub ident: Ident,
72 }
David Tolnayb79ee962016-09-04 09:39:20 -070073}
74
David Tolnay01405f02016-10-02 09:05:02 -070075impl Lifetime {
76 pub fn new<T: Into<Ident>>(t: T) -> Self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070077 let id = t.into();
David Tolnayff3b8ae2016-10-08 11:54:18 -070078 if !id.as_ref().starts_with('\'') {
79 panic!("lifetime name must start with apostrophe as in \"'a\", \
David Tolnay3bcfb722016-10-08 11:58:36 -070080 got {:?}",
81 id.as_ref());
David Tolnayff3b8ae2016-10-08 11:54:18 -070082 }
83 Lifetime { ident: id }
David Tolnay01405f02016-10-02 09:05:02 -070084 }
85}
86
Alex Crichton62a0a592017-05-22 13:58:53 -070087ast_struct! {
Alex Crichtonccbb45d2017-05-23 10:58:24 -070088 /// A set of bound lifetimes, e.g. `for<'a, 'b, 'c>`
89 #[derive(Default)]
90 pub struct BoundLifetimes {
91 pub for_token: tokens::For,
92 pub lt_token: tokens::Lt,
93 pub lifetimes: Delimited<LifetimeDef, tokens::Comma>,
94 pub gt_token: tokens::Gt,
95 }
96}
97
98ast_struct! {
Alex Crichton62a0a592017-05-22 13:58:53 -070099 /// A lifetime definition, e.g. `'a: 'b+'c+'d`
100 pub struct LifetimeDef {
101 pub attrs: Vec<Attribute>,
102 pub lifetime: Lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700103 pub colon_token: Option<tokens::Colon>,
104 pub bounds: Delimited<Lifetime, tokens::Add>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700105 }
David Tolnayb79ee962016-09-04 09:39:20 -0700106}
107
David Tolnayf9505b52016-10-02 09:18:52 -0700108impl LifetimeDef {
109 pub fn new<T: Into<Ident>>(t: T) -> Self {
110 LifetimeDef {
David Tolnaye7678922016-10-13 20:44:03 -0700111 attrs: Vec::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700112 lifetime: Lifetime::new(t),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700113 colon_token: None,
114 bounds: Delimited::new(),
David Tolnayf9505b52016-10-02 09:18:52 -0700115 }
116 }
117}
118
Alex Crichton62a0a592017-05-22 13:58:53 -0700119ast_struct! {
120 /// A generic type parameter, e.g. `T: Into<String>`.
121 pub struct TyParam {
122 pub attrs: Vec<Attribute>,
123 pub ident: Ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700124 pub colon_token: Option<tokens::Colon>,
125 pub bounds: Delimited<TyParamBound, tokens::Add>,
126 pub eq_token: Option<tokens::Eq>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700127 pub default: Option<Ty>,
128 }
David Tolnayb79ee962016-09-04 09:39:20 -0700129}
130
Ted Driggs0547d0d2017-04-20 10:00:12 -0700131impl From<Ident> for TyParam {
132 fn from(ident: Ident) -> Self {
133 TyParam {
134 attrs: vec![],
135 ident: ident,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700136 colon_token: None,
137 bounds: Delimited::new(),
138 eq_token: None,
Ted Driggs0547d0d2017-04-20 10:00:12 -0700139 default: None,
140 }
141 }
142}
143
Alex Crichton62a0a592017-05-22 13:58:53 -0700144ast_enum! {
145 /// The AST represents all type param bounds as types.
146 /// `typeck::collect::compute_bounds` matches these against
147 /// the "special" built-in traits (see `middle::lang_items`) and
148 /// detects Copy, Send and Sync.
149 pub enum TyParamBound {
150 Trait(PolyTraitRef, TraitBoundModifier),
151 Region(Lifetime),
152 }
David Tolnay55337722016-09-11 12:58:56 -0700153}
154
Alex Crichton62a0a592017-05-22 13:58:53 -0700155ast_enum! {
156 /// A modifier on a bound, currently this is only used for `?Sized`, where the
157 /// modifier is `Maybe`. Negative bounds should also be handled here.
Alex Crichton2e0229c2017-05-23 09:34:50 -0700158 #[cfg_attr(feature = "clone-impls", derive(Copy))]
Alex Crichton62a0a592017-05-22 13:58:53 -0700159 pub enum TraitBoundModifier {
160 None,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700161 Maybe(tokens::Question),
Alex Crichton62a0a592017-05-22 13:58:53 -0700162 }
David Tolnay55337722016-09-11 12:58:56 -0700163}
164
Alex Crichton62a0a592017-05-22 13:58:53 -0700165ast_struct! {
166 /// A `where` clause in a definition
167 #[derive(Default)]
168 pub struct WhereClause {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700169 pub where_token: Option<tokens::Where>,
170 pub predicates: Delimited<WherePredicate, tokens::Comma>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700171 }
David Tolnayb79ee962016-09-04 09:39:20 -0700172}
173
David Tolnayb153dbc2016-10-04 23:39:10 -0700174impl WhereClause {
175 pub fn none() -> Self {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700176 WhereClause::default()
David Tolnayb153dbc2016-10-04 23:39:10 -0700177 }
178}
179
Alex Crichton62a0a592017-05-22 13:58:53 -0700180ast_enum_of_structs! {
181 /// A single predicate in a `where` clause
182 pub enum WherePredicate {
183 /// A type binding, e.g. `for<'c> Foo: Send+Clone+'c`
184 pub BoundPredicate(WhereBoundPredicate {
185 /// Any lifetimes from a `for` binding
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700186 pub bound_lifetimes: Option<BoundLifetimes>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700187 /// The type being bounded
188 pub bounded_ty: Ty,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700189 pub colon_token: tokens::Colon,
Alex Crichton62a0a592017-05-22 13:58:53 -0700190 /// Trait and lifetime bounds (`Clone+Send+'static`)
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700191 pub bounds: Delimited<TyParamBound, tokens::Add>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700192 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700193
Alex Crichton62a0a592017-05-22 13:58:53 -0700194 /// A lifetime predicate, e.g. `'a: 'b+'c`
195 pub RegionPredicate(WhereRegionPredicate {
196 pub lifetime: Lifetime,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700197 pub colon_token: Option<tokens::Colon>,
198 pub bounds: Delimited<Lifetime, tokens::Add>,
Alex Crichton62a0a592017-05-22 13:58:53 -0700199 }),
David Tolnayb79ee962016-09-04 09:39:20 -0700200
Alex Crichton62a0a592017-05-22 13:58:53 -0700201 /// An equality predicate (unsupported)
202 pub EqPredicate(WhereEqPredicate {
203 pub lhs_ty: Ty,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700204 pub eq_token: tokens::Eq,
Alex Crichton62a0a592017-05-22 13:58:53 -0700205 pub rhs_ty: Ty,
206 }),
207 }
David Tolnayf8e08832017-01-23 00:04:32 -0800208}
209
David Tolnay86eca752016-09-04 11:26:41 -0700210#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700211pub mod parsing {
212 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700213
Michael Layzell92639a52017-06-01 00:07:44 -0400214 use synom::{PResult, Cursor, Synom, parse_error};
Alex Crichton954046c2017-05-30 21:49:42 -0700215 use synom::tokens::*;
Alex Crichton954046c2017-05-30 21:49:42 -0700216
217 impl Synom for Generics {
Michael Layzell92639a52017-06-01 00:07:44 -0400218 named!(parse -> Self, map!(
219 alt!(
220 do_parse!(
221 lt: syn!(Lt) >>
222 lifetimes: call!(Delimited::parse_terminated) >>
223 ty_params: cond!(
224 lifetimes.is_empty() || lifetimes.trailing_delim(),
225 call!(Delimited::parse_terminated)
226 ) >>
227 gt: syn!(Gt) >>
228 (lifetimes, ty_params, Some(lt), Some(gt))
229 )
230 |
231 epsilon!() => { |_| (Delimited::new(), None, None, None) }
232 ),
233 |(lifetimes, ty_params, lt, gt): (_, Option<_>, _, _)| Generics {
234 lifetimes: lifetimes,
235 ty_params: ty_params.unwrap_or_default(),
236 where_clause: WhereClause::default(),
237 gt_token: gt,
238 lt_token: lt,
Michael Layzell416724e2017-05-24 21:12:34 -0400239 }
Michael Layzell92639a52017-06-01 00:07:44 -0400240 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700241 }
242
243 impl Synom for Lifetime {
Michael Layzell92639a52017-06-01 00:07:44 -0400244 fn parse(input: Cursor) -> PResult<Self> {
Michael Layzell589a8f42017-06-02 19:47:01 -0400245 match input.word() {
246 Some((rest, span, sym)) => {
247 if sym.as_str().starts_with('\'') {
248 return Ok((rest, Lifetime {
249 ident: Ident {
250 span: Span(span),
251 sym: sym
252 }
253 }));
254 }
Alex Crichton954046c2017-05-30 21:49:42 -0700255 }
Michael Layzell589a8f42017-06-02 19:47:01 -0400256 _ => {}
Alex Crichton954046c2017-05-30 21:49:42 -0700257 }
Michael Layzell92639a52017-06-01 00:07:44 -0400258 parse_error()
Michael Layzell416724e2017-05-24 21:12:34 -0400259 }
260 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700261
Alex Crichton954046c2017-05-30 21:49:42 -0700262 impl Synom for LifetimeDef {
Michael Layzell92639a52017-06-01 00:07:44 -0400263 named!(parse -> Self, do_parse!(
264 attrs: many0!(call!(Attribute::parse_outer)) >>
265 life: syn!(Lifetime) >>
266 colon: option!(syn!(Colon)) >>
267 bounds: cond!(
268 colon.is_some(),
269 call!(Delimited::parse_separated_nonempty)
270 ) >>
271 (LifetimeDef {
272 attrs: attrs,
273 lifetime: life,
274 bounds: bounds.unwrap_or_default(),
275 colon_token: colon.map(|_| tokens::Colon::default()),
276 })
277 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700278 }
279
280 impl Synom for BoundLifetimes {
Michael Layzell92639a52017-06-01 00:07:44 -0400281 named!(parse -> Self, do_parse!(
282 for_: syn!(For) >>
283 lt: syn!(Lt) >>
284 lifetimes: call!(Delimited::parse_terminated) >>
285 gt: syn!(Gt) >>
286 (BoundLifetimes {
287 for_token: for_,
288 lt_token: lt,
289 gt_token: gt,
290 lifetimes: lifetimes,
291 })
292 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700293 }
David Tolnay55337722016-09-11 12:58:56 -0700294
Alex Crichton954046c2017-05-30 21:49:42 -0700295 impl Synom for TyParam {
Michael Layzell92639a52017-06-01 00:07:44 -0400296 named!(parse -> Self, do_parse!(
297 attrs: many0!(call!(Attribute::parse_outer)) >>
298 id: syn!(Ident) >>
299 colon: option!(syn!(Colon)) >>
300 bounds: cond!(
301 colon.is_some(),
302 call!(Delimited::parse_separated_nonempty)
303 ) >>
304 default: option!(do_parse!(
305 eq: syn!(Eq) >>
306 ty: syn!(Ty) >>
307 (eq, ty)
308 )) >>
309 (TyParam {
310 attrs: attrs,
311 ident: id,
312 bounds: bounds.unwrap_or_default(),
313 colon_token: colon,
314 eq_token: default.as_ref().map(|d| tokens::Eq((d.0).0)),
315 default: default.map(|d| d.1),
316 })
317 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700318 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700319
Alex Crichton954046c2017-05-30 21:49:42 -0700320 impl Synom for TyParamBound {
Michael Layzell92639a52017-06-01 00:07:44 -0400321 named!(parse -> Self, alt!(
322 do_parse!(
323 question: syn!(Question) >>
324 poly: syn!(PolyTraitRef) >>
325 (TyParamBound::Trait(poly, TraitBoundModifier::Maybe(question)))
326 )
327 |
328 syn!(Lifetime) => { TyParamBound::Region }
329 |
330 syn!(PolyTraitRef) => {
331 |poly| TyParamBound::Trait(poly, TraitBoundModifier::None)
Alex Crichton954046c2017-05-30 21:49:42 -0700332 }
Michael Layzell92639a52017-06-01 00:07:44 -0400333 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700334
335 fn description() -> Option<&'static str> {
336 Some("type parameter buond")
337 }
338 }
339
340 impl Synom for WhereClause {
Michael Layzell92639a52017-06-01 00:07:44 -0400341 named!(parse -> Self, alt!(
342 do_parse!(
343 where_: syn!(Where) >>
344 predicates: call!(Delimited::parse_terminated) >>
345 (WhereClause {
346 predicates: predicates,
347 where_token: Some(where_),
348 })
349 )
350 |
351 epsilon!() => { |_| WhereClause::default() }
352 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700353
354 fn description() -> Option<&'static str> {
355 Some("where clause")
356 }
357 }
358
359 impl Synom for WherePredicate {
Michael Layzell92639a52017-06-01 00:07:44 -0400360 named!(parse -> Self, alt!(
361 do_parse!(
362 ident: syn!(Lifetime) >>
363 colon: option!(syn!(Colon)) >>
364 bounds: cond!(
365 colon.is_some(),
366 call!(Delimited::parse_separated)
367 ) >>
368 (WherePredicate::RegionPredicate(WhereRegionPredicate {
369 lifetime: ident,
370 bounds: bounds.unwrap_or_default(),
371 colon_token: colon,
372 }))
373 )
374 |
375 do_parse!(
376 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
377 bounded_ty: syn!(Ty) >>
378 colon: syn!(Colon) >>
379 bounds: call!(Delimited::parse_separated_nonempty) >>
380 (WherePredicate::BoundPredicate(WhereBoundPredicate {
381 bound_lifetimes: bound_lifetimes,
382 bounded_ty: bounded_ty,
383 bounds: bounds,
384 colon_token: colon,
385 }))
386 )
387 ));
Alex Crichton954046c2017-05-30 21:49:42 -0700388 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700389}
David Tolnay87d0b442016-09-04 11:52:12 -0700390
391#[cfg(feature = "printing")]
392mod printing {
393 use super::*;
David Tolnaye7678922016-10-13 20:44:03 -0700394 use attr::FilterAttrs;
David Tolnay87d0b442016-09-04 11:52:12 -0700395 use quote::{Tokens, ToTokens};
396
David Tolnay8ef93042016-09-04 14:08:40 -0700397 impl ToTokens for Generics {
398 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700399 self.lt_token.to_tokens(tokens);
400 self.lifetimes.to_tokens(tokens);
401 self.ty_params.to_tokens(tokens);
402 self.gt_token.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700403 }
404 }
405
David Tolnaye7678922016-10-13 20:44:03 -0700406 impl<'a> ToTokens for ImplGenerics<'a> {
407 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700408 self.0.lt_token.to_tokens(tokens);
409 self.0.lifetimes.to_tokens(tokens);
410 for param in self.0.ty_params.iter() {
411 // Leave off the type parameter defaults
412 let item = param.item();
413 tokens.append_all(item.attrs.outer());
414 item.ident.to_tokens(tokens);
415 item.colon_token.to_tokens(tokens);
416 item.bounds.to_tokens(tokens);
417 param.delimiter().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700418 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700419 self.0.gt_token.to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700420 }
421 }
422
423 impl<'a> ToTokens for TyGenerics<'a> {
424 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700425 self.0.lt_token.to_tokens(tokens);
426 // Leave off the lifetime bounds and attributes
427 for param in self.0.lifetimes.iter() {
428 param.item().lifetime.to_tokens(tokens);
429 param.delimiter().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700430 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700431 // Leave off the type parameter defaults
432 for param in self.0.ty_params.iter() {
433 param.item().ident.to_tokens(tokens);
434 param.delimiter().to_tokens(tokens);
435 }
436 self.0.gt_token.to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700437 }
438 }
439
David Tolnayc879a502017-01-25 15:51:32 -0800440 impl<'a> ToTokens for Turbofish<'a> {
441 fn to_tokens(&self, tokens: &mut Tokens) {
442 let has_lifetimes = !self.0.lifetimes.is_empty();
443 let has_ty_params = !self.0.ty_params.is_empty();
444 if has_lifetimes || has_ty_params {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700445 tokens::Colon2::default().to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -0800446 TyGenerics(self.0).to_tokens(tokens);
447 }
448 }
449 }
450
David Tolnay87d0b442016-09-04 11:52:12 -0700451 impl ToTokens for Lifetime {
452 fn to_tokens(&self, tokens: &mut Tokens) {
453 self.ident.to_tokens(tokens);
454 }
455 }
456
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700457 impl ToTokens for BoundLifetimes {
458 fn to_tokens(&self, tokens: &mut Tokens) {
459 self.for_token.to_tokens(tokens);
460 self.lt_token.to_tokens(tokens);
461 self.lifetimes.to_tokens(tokens);
462 self.gt_token.to_tokens(tokens);
463 }
464 }
465
David Tolnay87d0b442016-09-04 11:52:12 -0700466 impl ToTokens for LifetimeDef {
467 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700468 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -0700469 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700470 self.colon_token.to_tokens(tokens);
471 self.bounds.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700472 }
473 }
474
David Tolnay8ef93042016-09-04 14:08:40 -0700475 impl ToTokens for TyParam {
476 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700477 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -0700478 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700479 self.colon_token.to_tokens(tokens);
480 self.bounds.to_tokens(tokens);
481 self.eq_token.to_tokens(tokens);
482 self.default.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700483 }
484 }
485
David Tolnay87d0b442016-09-04 11:52:12 -0700486 impl ToTokens for TyParamBound {
487 fn to_tokens(&self, tokens: &mut Tokens) {
488 match *self {
David Tolnay87d0b442016-09-04 11:52:12 -0700489 TyParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700490 TyParamBound::Trait(ref trait_ref, ref modifier) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700491 modifier.to_tokens(tokens);
David Tolnay55337722016-09-11 12:58:56 -0700492 trait_ref.to_tokens(tokens);
493 }
494 }
495 }
496 }
497
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700498 impl ToTokens for TraitBoundModifier {
499 fn to_tokens(&self, tokens: &mut Tokens) {
500 match *self {
501 TraitBoundModifier::None => {}
502 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
503 }
504 }
505 }
506
David Tolnay55337722016-09-11 12:58:56 -0700507 impl ToTokens for WhereClause {
508 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700509 self.where_token.to_tokens(tokens);
510 self.predicates.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700511 }
512 }
David Tolnay8ef93042016-09-04 14:08:40 -0700513
David Tolnay8ef93042016-09-04 14:08:40 -0700514 impl ToTokens for WhereBoundPredicate {
515 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700516 self.bound_lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700517 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700518 self.colon_token.to_tokens(tokens);
519 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700520 }
521 }
522
523 impl ToTokens for WhereRegionPredicate {
524 fn to_tokens(&self, tokens: &mut Tokens) {
525 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700526 self.colon_token.to_tokens(tokens);
527 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700528 }
529 }
David Tolnayf8e08832017-01-23 00:04:32 -0800530
531 impl ToTokens for WhereEqPredicate {
532 fn to_tokens(&self, tokens: &mut Tokens) {
533 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700534 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -0800535 self.rhs_ty.to_tokens(tokens);
536 }
537 }
David Tolnay87d0b442016-09-04 11:52:12 -0700538}