blob: 71cd8f6387618ffe972a03f7a922f850e7066721 [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
Alex Crichton954046c2017-05-30 21:49:42 -0700214 use synom::{IResult, Synom};
215 use synom::tokens::*;
216 use proc_macro2::{TokenTree, TokenKind};
217
218 impl Synom for Generics {
219 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
220 map! {
221 input,
222 alt!(
223 do_parse!(
224 lt: syn!(Lt) >>
225 lifetimes: call!(Delimited::parse_terminated) >>
226 ty_params: cond!(
227 lifetimes.is_empty() || lifetimes.trailing_delim(),
228 call!(Delimited::parse_terminated)
229 ) >>
230 gt: syn!(Gt) >>
231 (lifetimes, ty_params, Some(lt), Some(gt))
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700232 )
Alex Crichton954046c2017-05-30 21:49:42 -0700233 |
234 epsilon!() => { |_| (Delimited::new(), None, None, None) }
235 ),
236 |(lifetimes, ty_params, lt, gt): (_, Option<_>, _, _)| Generics {
237 lifetimes: lifetimes,
238 ty_params: ty_params.unwrap_or_default(),
239 where_clause: WhereClause::default(),
240 gt_token: gt,
241 lt_token: lt,
242 }
Michael Layzell416724e2017-05-24 21:12:34 -0400243 }
Alex Crichton954046c2017-05-30 21:49:42 -0700244 }
245 }
246
247 impl Synom for Lifetime {
248 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
249 let mut tokens = input.iter();
250 let token = match tokens.next() {
251 Some(token) => token,
252 None => return IResult::Error,
253 };
254 if let TokenKind::Word(s) = token.kind {
255 if s.as_str().starts_with('\'') {
256 return IResult::Done(tokens.as_slice(), Lifetime {
257 ident: Ident {
258 span: Span(token.span),
259 sym: s,
260 },
261 })
262 }
263 }
Michael Layzell416724e2017-05-24 21:12:34 -0400264 IResult::Error
265 }
266 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700267
Alex Crichton954046c2017-05-30 21:49:42 -0700268 impl Synom for LifetimeDef {
269 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
270 do_parse! {
271 input,
272 attrs: many0!(call!(Attribute::parse_outer)) >>
273 life: syn!(Lifetime) >>
274 colon: option!(syn!(Colon)) >>
275 bounds: cond!(
276 colon.is_some(),
277 call!(Delimited::parse_separated_nonempty)
278 ) >>
279 (LifetimeDef {
280 attrs: attrs,
281 lifetime: life,
282 bounds: bounds.unwrap_or_default(),
283 colon_token: colon.map(|_| tokens::Colon::default()),
284 })
285 }
David Tolnay55337722016-09-11 12:58:56 -0700286 }
Alex Crichton954046c2017-05-30 21:49:42 -0700287 }
288
289 impl Synom for BoundLifetimes {
290 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
291 do_parse! {
292 input,
293 for_: syn!(For) >>
294 lt: syn!(Lt) >>
295 lifetimes: call!(Delimited::parse_terminated) >>
296 gt: syn!(Gt) >>
297 (BoundLifetimes {
298 for_token: for_,
299 lt_token: lt,
300 gt_token: gt,
301 lifetimes: lifetimes,
302 })
303 }
David Tolnay55337722016-09-11 12:58:56 -0700304 }
Alex Crichton954046c2017-05-30 21:49:42 -0700305 }
David Tolnay55337722016-09-11 12:58:56 -0700306
Alex Crichton954046c2017-05-30 21:49:42 -0700307 impl Synom for TyParam {
308 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
309 do_parse! {
310 input,
311 attrs: many0!(call!(Attribute::parse_outer)) >>
312 id: syn!(Ident) >>
313 colon: option!(syn!(Colon)) >>
314 bounds: cond!(
315 colon.is_some(),
316 call!(Delimited::parse_separated_nonempty)
317 ) >>
318 default: option!(do_parse!(
319 eq: syn!(Eq) >>
320 ty: syn!(Ty) >>
321 (eq, ty)
322 )) >>
323 (TyParam {
324 attrs: attrs,
325 ident: id,
326 bounds: bounds.unwrap_or_default(),
327 colon_token: colon,
328 eq_token: default.as_ref().map(|d| tokens::Eq((d.0).0)),
329 default: default.map(|d| d.1),
330 })
331 }
332 }
333 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700334
Alex Crichton954046c2017-05-30 21:49:42 -0700335 impl Synom for TyParamBound {
336 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
337 alt! {
338 input,
339 do_parse!(
340 question: syn!(Question) >>
341 poly: syn!(PolyTraitRef) >>
342 (TyParamBound::Trait(poly, TraitBoundModifier::Maybe(question)))
343 )
344 |
345 syn!(Lifetime) => { TyParamBound::Region }
346 |
347 syn!(PolyTraitRef) => {
348 |poly| TyParamBound::Trait(poly, TraitBoundModifier::None)
349 }
350 }
351 }
352
353 fn description() -> Option<&'static str> {
354 Some("type parameter buond")
355 }
356 }
357
358 impl Synom for WhereClause {
359 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
360 alt! {
361 input,
362 do_parse!(
363 where_: syn!(Where) >>
364 predicates: call!(Delimited::parse_terminated) >>
365 (WhereClause {
366 predicates: predicates,
367 where_token: Some(where_),
368 })
369 )
370 |
371 epsilon!() => { |_| WhereClause::default() }
372 }
373 }
374
375 fn description() -> Option<&'static str> {
376 Some("where clause")
377 }
378 }
379
380 impl Synom for WherePredicate {
381 fn parse(input: &[TokenTree]) -> IResult<&[TokenTree], Self> {
382 alt! {
383 input,
384 do_parse!(
385 ident: syn!(Lifetime) >>
386 colon: option!(syn!(Colon)) >>
387 bounds: cond!(
388 colon.is_some(),
389 call!(Delimited::parse_separated)
390 ) >>
391 (WherePredicate::RegionPredicate(WhereRegionPredicate {
392 lifetime: ident,
393 bounds: bounds.unwrap_or_default(),
394 colon_token: colon,
395 }))
396 )
397 |
398 do_parse!(
399 bound_lifetimes: option!(syn!(BoundLifetimes)) >>
400 bounded_ty: syn!(Ty) >>
401 colon: syn!(Colon) >>
402 bounds: call!(Delimited::parse_separated_nonempty) >>
403 (WherePredicate::BoundPredicate(WhereBoundPredicate {
404 bound_lifetimes: bound_lifetimes,
405 bounded_ty: bounded_ty,
406 bounds: bounds,
407 colon_token: colon,
408 }))
409 )
410 }
411 }
412 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700413}
David Tolnay87d0b442016-09-04 11:52:12 -0700414
415#[cfg(feature = "printing")]
416mod printing {
417 use super::*;
David Tolnaye7678922016-10-13 20:44:03 -0700418 use attr::FilterAttrs;
David Tolnay87d0b442016-09-04 11:52:12 -0700419 use quote::{Tokens, ToTokens};
420
David Tolnay8ef93042016-09-04 14:08:40 -0700421 impl ToTokens for Generics {
422 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700423 self.lt_token.to_tokens(tokens);
424 self.lifetimes.to_tokens(tokens);
425 self.ty_params.to_tokens(tokens);
426 self.gt_token.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700427 }
428 }
429
David Tolnaye7678922016-10-13 20:44:03 -0700430 impl<'a> ToTokens for ImplGenerics<'a> {
431 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700432 self.0.lt_token.to_tokens(tokens);
433 self.0.lifetimes.to_tokens(tokens);
434 for param in self.0.ty_params.iter() {
435 // Leave off the type parameter defaults
436 let item = param.item();
437 tokens.append_all(item.attrs.outer());
438 item.ident.to_tokens(tokens);
439 item.colon_token.to_tokens(tokens);
440 item.bounds.to_tokens(tokens);
441 param.delimiter().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700442 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700443 self.0.gt_token.to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700444 }
445 }
446
447 impl<'a> ToTokens for TyGenerics<'a> {
448 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700449 self.0.lt_token.to_tokens(tokens);
450 // Leave off the lifetime bounds and attributes
451 for param in self.0.lifetimes.iter() {
452 param.item().lifetime.to_tokens(tokens);
453 param.delimiter().to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700454 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700455 // Leave off the type parameter defaults
456 for param in self.0.ty_params.iter() {
457 param.item().ident.to_tokens(tokens);
458 param.delimiter().to_tokens(tokens);
459 }
460 self.0.gt_token.to_tokens(tokens);
David Tolnaye7678922016-10-13 20:44:03 -0700461 }
462 }
463
David Tolnayc879a502017-01-25 15:51:32 -0800464 impl<'a> ToTokens for Turbofish<'a> {
465 fn to_tokens(&self, tokens: &mut Tokens) {
466 let has_lifetimes = !self.0.lifetimes.is_empty();
467 let has_ty_params = !self.0.ty_params.is_empty();
468 if has_lifetimes || has_ty_params {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700469 tokens::Colon2::default().to_tokens(tokens);
David Tolnayc879a502017-01-25 15:51:32 -0800470 TyGenerics(self.0).to_tokens(tokens);
471 }
472 }
473 }
474
David Tolnay87d0b442016-09-04 11:52:12 -0700475 impl ToTokens for Lifetime {
476 fn to_tokens(&self, tokens: &mut Tokens) {
477 self.ident.to_tokens(tokens);
478 }
479 }
480
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700481 impl ToTokens for BoundLifetimes {
482 fn to_tokens(&self, tokens: &mut Tokens) {
483 self.for_token.to_tokens(tokens);
484 self.lt_token.to_tokens(tokens);
485 self.lifetimes.to_tokens(tokens);
486 self.gt_token.to_tokens(tokens);
487 }
488 }
489
David Tolnay87d0b442016-09-04 11:52:12 -0700490 impl ToTokens for LifetimeDef {
491 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700492 tokens.append_all(self.attrs.outer());
David Tolnay87d0b442016-09-04 11:52:12 -0700493 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700494 self.colon_token.to_tokens(tokens);
495 self.bounds.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700496 }
497 }
498
David Tolnay8ef93042016-09-04 14:08:40 -0700499 impl ToTokens for TyParam {
500 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnaye7678922016-10-13 20:44:03 -0700501 tokens.append_all(self.attrs.outer());
David Tolnay8ef93042016-09-04 14:08:40 -0700502 self.ident.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700503 self.colon_token.to_tokens(tokens);
504 self.bounds.to_tokens(tokens);
505 self.eq_token.to_tokens(tokens);
506 self.default.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700507 }
508 }
509
David Tolnay87d0b442016-09-04 11:52:12 -0700510 impl ToTokens for TyParamBound {
511 fn to_tokens(&self, tokens: &mut Tokens) {
512 match *self {
David Tolnay87d0b442016-09-04 11:52:12 -0700513 TyParamBound::Region(ref lifetime) => lifetime.to_tokens(tokens),
Alex Crichton62a0a592017-05-22 13:58:53 -0700514 TyParamBound::Trait(ref trait_ref, ref modifier) => {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700515 modifier.to_tokens(tokens);
David Tolnay55337722016-09-11 12:58:56 -0700516 trait_ref.to_tokens(tokens);
517 }
518 }
519 }
520 }
521
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700522 impl ToTokens for TraitBoundModifier {
523 fn to_tokens(&self, tokens: &mut Tokens) {
524 match *self {
525 TraitBoundModifier::None => {}
526 TraitBoundModifier::Maybe(ref t) => t.to_tokens(tokens),
527 }
528 }
529 }
530
David Tolnay55337722016-09-11 12:58:56 -0700531 impl ToTokens for WhereClause {
532 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700533 self.where_token.to_tokens(tokens);
534 self.predicates.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700535 }
536 }
David Tolnay8ef93042016-09-04 14:08:40 -0700537
David Tolnay8ef93042016-09-04 14:08:40 -0700538 impl ToTokens for WhereBoundPredicate {
539 fn to_tokens(&self, tokens: &mut Tokens) {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700540 self.bound_lifetimes.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700541 self.bounded_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700542 self.colon_token.to_tokens(tokens);
543 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700544 }
545 }
546
547 impl ToTokens for WhereRegionPredicate {
548 fn to_tokens(&self, tokens: &mut Tokens) {
549 self.lifetime.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700550 self.colon_token.to_tokens(tokens);
551 self.bounds.to_tokens(tokens);
David Tolnay8ef93042016-09-04 14:08:40 -0700552 }
553 }
David Tolnayf8e08832017-01-23 00:04:32 -0800554
555 impl ToTokens for WhereEqPredicate {
556 fn to_tokens(&self, tokens: &mut Tokens) {
557 self.lhs_ty.to_tokens(tokens);
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700558 self.eq_token.to_tokens(tokens);
David Tolnayf8e08832017-01-23 00:04:32 -0800559 self.rhs_ty.to_tokens(tokens);
560 }
561 }
David Tolnay87d0b442016-09-04 11:52:12 -0700562}