blob: 7cba5f91cb4f253129307cec41a1967a8694a7f8 [file] [log] [blame]
David Tolnayb79ee962016-09-04 09:39:20 -07001use super::*;
2
David Tolnay771ecf42016-09-23 19:26:37 -07003/// The different kinds of types recognized by the compiler
David Tolnayb79ee962016-09-04 09:39:20 -07004#[derive(Debug, Clone, Eq, PartialEq)]
5pub enum Ty {
6 /// A variable-length array (`[T]`)
7 Vec(Box<Ty>),
8 /// A fixed length array (`[T; n]`)
9 FixedLengthVec(Box<Ty>, usize),
10 /// A raw pointer (`*const T` or `*mut T`)
11 Ptr(Box<MutTy>),
12 /// A reference (`&'a T` or `&'a mut T`)
13 Rptr(Option<Lifetime>, Box<MutTy>),
14 /// A bare function (e.g. `fn(usize) -> bool`)
15 BareFn(Box<BareFnTy>),
16 /// The never type (`!`)
17 Never,
18 /// A tuple (`(A, B, C, D, ...)`)
19 Tup(Vec<Ty>),
20 /// A path (`module::module::...::Type`), optionally
21 /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`.
22 ///
23 /// Type parameters are stored in the Path itself
24 Path(Option<QSelf>, Path),
25 /// Something like `A+B`. Note that `B` must always be a path.
26 ObjectSum(Box<Ty>, Vec<TyParamBound>),
27 /// A type like `for<'a> Foo<&'a Bar>`
28 PolyTraitRef(Vec<TyParamBound>),
29 /// An `impl TraitA+TraitB` type.
30 ImplTrait(Vec<TyParamBound>),
31 /// No-op; kept solely so that we can pretty-print faithfully
32 Paren(Box<Ty>),
33 /// TyKind::Infer means the type should be inferred instead of it having been
34 /// specified. This can appear anywhere in a type.
35 Infer,
36}
37
38#[derive(Debug, Clone, Eq, PartialEq)]
39pub struct MutTy {
40 pub ty: Ty,
41 pub mutability: Mutability,
42}
43
David Tolnayf4bbbd92016-09-23 14:41:55 -070044#[derive(Debug, Copy, Clone, Eq, PartialEq)]
David Tolnayb79ee962016-09-04 09:39:20 -070045pub enum Mutability {
46 Mutable,
47 Immutable,
48}
49
David Tolnay771ecf42016-09-23 19:26:37 -070050/// A "Path" is essentially Rust's notion of a name.
51///
52/// It's represented as a sequence of identifiers,
53/// along with a bunch of supporting information.
54///
55/// E.g. `std::cmp::PartialEq`
David Tolnayb79ee962016-09-04 09:39:20 -070056#[derive(Debug, Clone, Eq, PartialEq)]
57pub struct Path {
58 pub global: bool,
59 pub segments: Vec<PathSegment>,
60}
61
62/// A segment of a path: an identifier, an optional lifetime, and a set of types.
63///
64/// E.g. `std`, `String` or `Box<T>`
65#[derive(Debug, Clone, Eq, PartialEq)]
66pub struct PathSegment {
67 pub ident: Ident,
68 pub parameters: PathParameters,
69}
70
71impl PathSegment {
72 pub fn ident(ident: Ident) -> Self {
73 PathSegment {
74 ident: ident,
75 parameters: PathParameters::none(),
76 }
77 }
78}
79
80/// Parameters of a path segment.
81///
82/// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`
83#[derive(Debug, Clone, Eq, PartialEq)]
84pub enum PathParameters {
85 /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`
86 AngleBracketed(AngleBracketedParameterData),
87 /// The `(A, B)` and `C` in `Foo(A, B) -> C`
88 Parenthesized(ParenthesizedParameterData),
89}
90
91impl PathParameters {
92 pub fn none() -> Self {
93 PathParameters::AngleBracketed(AngleBracketedParameterData::default())
94 }
95}
96
97/// A path like `Foo<'a, T>`
98#[derive(Debug, Clone, Eq, PartialEq, Default)]
99pub struct AngleBracketedParameterData {
100 /// The lifetime parameters for this path segment.
101 pub lifetimes: Vec<Lifetime>,
102 /// The type parameters for this path segment, if present.
103 pub types: Vec<Ty>,
104 /// Bindings (equality constraints) on associated types, if present.
105 ///
106 /// E.g., `Foo<A=Bar>`.
107 pub bindings: Vec<TypeBinding>,
108}
109
110/// Bind a type to an associated type: `A=Foo`.
111#[derive(Debug, Clone, Eq, PartialEq)]
112pub struct TypeBinding {
113 pub ident: Ident,
114 pub ty: Ty,
115}
116
117/// A path like `Foo(A,B) -> C`
118#[derive(Debug, Clone, Eq, PartialEq)]
119pub struct ParenthesizedParameterData {
120 /// `(A, B)`
121 pub inputs: Vec<Ty>,
122 /// `C`
123 pub output: Option<Ty>,
124}
125
126#[derive(Debug, Clone, Eq, PartialEq)]
127pub struct PolyTraitRef {
128 /// The `'a` in `<'a> Foo<&'a T>`
129 pub bound_lifetimes: Vec<LifetimeDef>,
130 /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`
131 pub trait_ref: Path,
132}
133
134/// The explicit Self type in a "qualified path". The actual
135/// path, including the trait and the associated item, is stored
136/// separately. `position` represents the index of the associated
137/// item qualified with this Self type.
138///
139/// ```rust,ignore
140/// <Vec<T> as a::b::Trait>::AssociatedItem
141/// ^~~~~ ~~~~~~~~~~~~~~^
142/// ty position = 3
143///
144/// <Vec<T>>::AssociatedItem
145/// ^~~~~ ^
146/// ty position = 0
147/// ```
148#[derive(Debug, Clone, Eq, PartialEq)]
149pub struct QSelf {
150 pub ty: Box<Ty>,
151 pub position: usize
152}
153
154#[derive(Debug, Clone, Eq, PartialEq)]
155pub struct BareFnTy {
156 pub lifetimes: Vec<LifetimeDef>,
David Tolnay62f374c2016-10-02 13:37:00 -0700157 pub inputs: Vec<BareFnArg>,
David Tolnayb79ee962016-09-04 09:39:20 -0700158 pub output: FunctionRetTy,
159}
160
David Tolnay62f374c2016-10-02 13:37:00 -0700161/// An argument in a function type.
David Tolnayb79ee962016-09-04 09:39:20 -0700162///
163/// E.g. `bar: usize` as in `fn foo(bar: usize)`
164#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnay62f374c2016-10-02 13:37:00 -0700165pub struct BareFnArg {
166 pub name: Option<Ident>,
David Tolnayb79ee962016-09-04 09:39:20 -0700167 pub ty: Ty,
168}
169
170#[derive(Debug, Clone, Eq, PartialEq)]
171pub enum FunctionRetTy {
172 /// Return type is not specified.
173 ///
174 /// Functions default to `()` and
175 /// closures default to inference. Span points to where return
176 /// type would be inserted.
177 Default,
178 /// Everything else
179 Ty(Ty),
180}
181
David Tolnay86eca752016-09-04 11:26:41 -0700182#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700183pub mod parsing {
184 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700185 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700186 use ident::parsing::ident;
David Tolnayde206222016-09-30 11:47:01 -0700187 use lit::parsing::int;
David Tolnay9d8f1972016-09-04 11:58:48 -0700188 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700189
David Tolnayb5a7b142016-09-13 22:46:39 -0700190 named!(pub ty -> Ty, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700191 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700192 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700193 ty_fixed_length_vec
David Tolnayb79ee962016-09-04 09:39:20 -0700194 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700195 ty_ptr
196 |
197 ty_rptr
198 |
199 ty_bare_fn
200 |
201 ty_never
202 |
203 ty_tup
204 |
205 ty_path
206 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700207 ty_impl_trait
208 |
209 ty_paren
210 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700211
David Tolnayb5a7b142016-09-13 22:46:39 -0700212 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700213 punct!("[") >>
214 elem: ty >>
215 punct!("]") >>
216 (Ty::Vec(Box::new(elem)))
217 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700218
David Tolnayb5a7b142016-09-13 22:46:39 -0700219 named!(ty_fixed_length_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700220 punct!("[") >>
221 elem: ty >>
222 punct!(";") >>
David Tolnayde206222016-09-30 11:47:01 -0700223 len: int >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700224 punct!("]") >>
David Tolnayde206222016-09-30 11:47:01 -0700225 (Ty::FixedLengthVec(Box::new(elem), len.0 as usize))
David Tolnay9d8f1972016-09-04 11:58:48 -0700226 ));
227
David Tolnayb5a7b142016-09-13 22:46:39 -0700228 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700229 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700230 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700231 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700232 |
David Tolnay10413f02016-09-30 09:12:02 -0700233 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700234 ) >>
235 target: ty >>
236 (Ty::Ptr(Box::new(MutTy {
237 ty: target,
238 mutability: mutability,
239 })))
240 ));
241
David Tolnayb5a7b142016-09-13 22:46:39 -0700242 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700243 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700244 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700245 mutability: mutability >>
246 target: ty >>
247 (Ty::Rptr(life, Box::new(MutTy {
248 ty: target,
249 mutability: mutability,
250 })))
251 ));
252
David Tolnayb5a7b142016-09-13 22:46:39 -0700253 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700254 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700255 lifetimes: opt_vec!(delimited!(
256 punct!("<"),
257 separated_list!(punct!(","), lifetime_def),
258 punct!(">")
David Tolnay6b7aaf02016-09-04 10:39:25 -0700259 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700260 punct!("(") >>
261 inputs: separated_list!(punct!(","), fn_arg) >>
262 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700263 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700264 punct!("->"),
265 ty
266 )) >>
267 (Ty::BareFn(Box::new(BareFnTy {
268 lifetimes: lifetimes,
David Tolnay62f374c2016-10-02 13:37:00 -0700269 inputs: inputs,
270 output: match output {
271 Some(ty) => FunctionRetTy::Ty(ty),
272 None => FunctionRetTy::Default,
David Tolnay9d8f1972016-09-04 11:58:48 -0700273 },
274 })))
275 ));
276
David Tolnayb5a7b142016-09-13 22:46:39 -0700277 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700278
David Tolnayb5a7b142016-09-13 22:46:39 -0700279 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700280 punct!("(") >>
281 elems: separated_list!(punct!(","), ty) >>
282 punct!(")") >>
283 (Ty::Tup(elems))
284 ));
285
David Tolnay9636c052016-10-02 17:11:17 -0700286 named!(ty_path -> Ty, map!(qpath, |(qself, p)| Ty::Path(qself, p)));
David Tolnay9d8f1972016-09-04 11:58:48 -0700287
David Tolnay9636c052016-10-02 17:11:17 -0700288 named!(pub qpath -> (Option<QSelf>, Path), alt!(
289 map!(path, |p| (None, p))
290 |
291 do_parse!(
292 punct!("<") >>
293 this: map!(ty, Box::new) >>
294 path: option!(preceded!(
295 keyword!("as"),
296 path
297 )) >>
298 punct!(">") >>
299 punct!("::") >>
300 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
301 ({
302 match path {
303 Some(mut path) => {
304 let pos = path.segments.len();
305 path.segments.extend(rest);
306 (Some(QSelf { ty: this, position: pos }), path)
307 }
308 None => {
309 (Some(QSelf { ty: this, position: 0 }), Path {
310 global: false,
311 segments: rest,
312 })
313 }
David Tolnayb79ee962016-09-04 09:39:20 -0700314 }
David Tolnay9636c052016-10-02 17:11:17 -0700315 })
316 )
David Tolnay9d8f1972016-09-04 11:58:48 -0700317 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700318
David Tolnayb5a7b142016-09-13 22:46:39 -0700319 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700320 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700321 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
322 (Ty::ImplTrait(elem))
323 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700324
David Tolnayb5a7b142016-09-13 22:46:39 -0700325 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700326 punct!("(") >>
327 elem: ty >>
328 punct!(")") >>
329 (Ty::Paren(Box::new(elem)))
330 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700331
David Tolnay47a877c2016-10-01 16:50:55 -0700332 named!(pub mutability -> Mutability, alt!(
David Tolnaybd76e572016-10-02 13:43:16 -0700333 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnayf6ccb832016-09-04 15:00:56 -0700334 |
335 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700336 ));
337
David Tolnayb5a7b142016-09-13 22:46:39 -0700338 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700339 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700340 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
341 (Path {
342 global: global.is_some(),
343 segments: segments,
344 })
345 ));
346
David Tolnay9636c052016-10-02 17:11:17 -0700347 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700348 do_parse!(
David Tolnay9636c052016-10-02 17:11:17 -0700349 id: option!(ident) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700350 punct!("<") >>
351 lifetimes: separated_list!(punct!(","), lifetime) >>
352 types: opt_vec!(preceded!(
353 cond!(!lifetimes.is_empty(), punct!(",")),
354 separated_nonempty_list!(
355 punct!(","),
356 terminated!(ty, not!(peek!(punct!("="))))
357 )
358 )) >>
359 bindings: opt_vec!(preceded!(
360 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
361 separated_nonempty_list!(punct!(","), type_binding)
362 )) >>
363 punct!(">") >>
364 (PathSegment {
David Tolnay9636c052016-10-02 17:11:17 -0700365 ident: id.unwrap_or_else(|| "".into()),
David Tolnay9d8f1972016-09-04 11:58:48 -0700366 parameters: PathParameters::AngleBracketed(
367 AngleBracketedParameterData {
368 lifetimes: lifetimes,
369 types: types,
370 bindings: bindings,
371 }
372 ),
373 })
374 )
375 |
David Tolnay55337722016-09-11 12:58:56 -0700376 map!(ident, PathSegment::ident)
David Tolnay9d8f1972016-09-04 11:58:48 -0700377 ));
378
David Tolnayb5a7b142016-09-13 22:46:39 -0700379 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700380 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700381 punct!("=") >>
382 ty: ty >>
383 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700384 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700385 ty: ty,
386 })
387 ));
388
David Tolnayb5a7b142016-09-13 22:46:39 -0700389 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700390 bound_lifetimes: bound_lifetimes >>
391 trait_ref: path >>
392 (PolyTraitRef {
393 bound_lifetimes: bound_lifetimes,
394 trait_ref: trait_ref,
395 })
396 ));
397
David Tolnay62f374c2016-10-02 13:37:00 -0700398 named!(pub fn_arg -> BareFnArg, do_parse!(
399 name: option!(terminated!(ident, punct!(":"))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700400 ty: ty >>
David Tolnay62f374c2016-10-02 13:37:00 -0700401 (BareFnArg {
402 name: name,
David Tolnay9d8f1972016-09-04 11:58:48 -0700403 ty: ty,
404 })
405 ));
406}
David Tolnay87d0b442016-09-04 11:52:12 -0700407
408#[cfg(feature = "printing")]
409mod printing {
410 use super::*;
411 use quote::{Tokens, ToTokens};
412
413 impl ToTokens for Ty {
414 fn to_tokens(&self, tokens: &mut Tokens) {
415 match *self {
416 Ty::Vec(ref inner) => {
417 tokens.append("[");
418 inner.to_tokens(tokens);
419 tokens.append("]");
420 }
421 Ty::FixedLengthVec(ref inner, len) => {
422 tokens.append("[");
423 inner.to_tokens(tokens);
424 tokens.append(";");
David Tolnayde206222016-09-30 11:47:01 -0700425 tokens.append(&len.to_string());
David Tolnay87d0b442016-09-04 11:52:12 -0700426 tokens.append("]");
427 }
428 Ty::Ptr(ref target) => {
429 tokens.append("*");
430 match target.mutability {
431 Mutability::Mutable => tokens.append("mut"),
432 Mutability::Immutable => tokens.append("const"),
433 }
434 target.ty.to_tokens(tokens);
435 }
436 Ty::Rptr(ref lifetime, ref target) => {
437 tokens.append("&");
438 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700439 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700440 target.ty.to_tokens(tokens);
441 }
442 Ty::BareFn(ref func) => {
443 func.to_tokens(tokens);
444 }
445 Ty::Never => {
446 tokens.append("!");
447 }
448 Ty::Tup(ref elems) => {
449 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700450 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700451 if elems.len() == 1 {
452 tokens.append(",");
453 }
454 tokens.append(")");
455 }
David Tolnayf69904a2016-09-04 14:46:07 -0700456 Ty::Path(None, ref path) => {
457 path.to_tokens(tokens);
458 }
459 Ty::Path(Some(ref qself), ref path) => {
460 tokens.append("<");
461 qself.ty.to_tokens(tokens);
462 if qself.position > 0 {
463 tokens.append("as");
464 for (i, segment) in path.segments.iter()
465 .take(qself.position)
466 .enumerate()
467 {
468 if i > 0 || path.global {
469 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700470 }
David Tolnayf69904a2016-09-04 14:46:07 -0700471 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700472 }
David Tolnayf69904a2016-09-04 14:46:07 -0700473 }
474 tokens.append(">");
475 for segment in path.segments.iter().skip(qself.position) {
476 tokens.append("::");
477 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700478 }
479 }
480 Ty::ObjectSum(_, _) => unimplemented!(),
481 Ty::PolyTraitRef(_) => unimplemented!(),
482 Ty::ImplTrait(ref bounds) => {
483 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700484 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700485 }
486 Ty::Paren(ref inner) => {
487 tokens.append("(");
488 inner.to_tokens(tokens);
489 tokens.append(")");
490 }
491 Ty::Infer => {
492 tokens.append("_");
493 }
494 }
495 }
496 }
497
David Tolnay47a877c2016-10-01 16:50:55 -0700498 impl ToTokens for Mutability {
499 fn to_tokens(&self, tokens: &mut Tokens) {
500 if let Mutability::Mutable = *self {
501 tokens.append("mut");
502 }
503 }
504 }
505
David Tolnay87d0b442016-09-04 11:52:12 -0700506 impl ToTokens for Path {
507 fn to_tokens(&self, tokens: &mut Tokens) {
508 for (i, segment) in self.segments.iter().enumerate() {
509 if i > 0 || self.global {
510 tokens.append("::");
511 }
512 segment.to_tokens(tokens);
513 }
514 }
515 }
516
517 impl ToTokens for PathSegment {
518 fn to_tokens(&self, tokens: &mut Tokens) {
519 self.ident.to_tokens(tokens);
520 self.parameters.to_tokens(tokens);
521 }
522 }
523
524 impl ToTokens for PathParameters {
525 fn to_tokens(&self, tokens: &mut Tokens) {
526 match *self {
527 PathParameters::AngleBracketed(ref parameters) => {
528 parameters.to_tokens(tokens);
529 }
530 PathParameters::Parenthesized(ref parameters) => {
531 parameters.to_tokens(tokens);
532 }
533 }
534 }
535 }
536
537 impl ToTokens for AngleBracketedParameterData {
538 fn to_tokens(&self, tokens: &mut Tokens) {
539 let has_lifetimes = !self.lifetimes.is_empty();
540 let has_types = !self.types.is_empty();
541 let has_bindings = !self.bindings.is_empty();
542 if !has_lifetimes && !has_types && !has_bindings {
543 return;
544 }
545
546 tokens.append("<");
547
548 let mut first = true;
549 for lifetime in &self.lifetimes {
550 if !first {
551 tokens.append(",");
552 }
553 lifetime.to_tokens(tokens);
554 first = false;
555 }
556 for ty in &self.types {
557 if !first {
558 tokens.append(",");
559 }
560 ty.to_tokens(tokens);
561 first = false;
562 }
563 for binding in &self.bindings {
564 if !first {
565 tokens.append(",");
566 }
567 binding.to_tokens(tokens);
568 first = false;
569 }
570
571 tokens.append(">");
572 }
573 }
574
575 impl ToTokens for TypeBinding {
576 fn to_tokens(&self, tokens: &mut Tokens) {
577 self.ident.to_tokens(tokens);
578 tokens.append("=");
579 self.ty.to_tokens(tokens);
580 }
581 }
582
583 impl ToTokens for ParenthesizedParameterData {
584 fn to_tokens(&self, tokens: &mut Tokens) {
585 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700586 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700587 tokens.append(")");
588 if let Some(ref output) = self.output {
589 tokens.append("->");
590 output.to_tokens(tokens);
591 }
592 }
593 }
594
595 impl ToTokens for PolyTraitRef {
596 fn to_tokens(&self, tokens: &mut Tokens) {
597 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700598 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700599 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700600 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700601 tokens.append(">");
602 }
603 self.trait_ref.to_tokens(tokens);
604 }
605 }
606
607 impl ToTokens for BareFnTy {
608 fn to_tokens(&self, tokens: &mut Tokens) {
609 tokens.append("fn");
610 if !self.lifetimes.is_empty() {
611 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700612 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700613 tokens.append(">");
614 }
615 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700616 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700617 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700618 if let FunctionRetTy::Ty(ref ty) = self.output {
619 tokens.append("->");
620 ty.to_tokens(tokens);
621 }
622 }
623 }
624
David Tolnay62f374c2016-10-02 13:37:00 -0700625 impl ToTokens for BareFnArg {
David Tolnay42602292016-10-01 22:25:45 -0700626 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay62f374c2016-10-02 13:37:00 -0700627 if let Some(ref name) = self.name {
628 name.to_tokens(tokens);
David Tolnay42602292016-10-01 22:25:45 -0700629 tokens.append(":");
630 }
631 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700632 }
633 }
634}