blob: 0dc6eafe60873aad60068e0c31042e3dbefeb9b9 [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>,
157 pub decl: FnDecl
158}
159
160/// Header (not the body) of a function declaration.
161///
162/// E.g. `fn foo(bar: baz)`
163#[derive(Debug, Clone, Eq, PartialEq)]
164pub struct FnDecl {
David Tolnay66daf742016-09-07 08:21:49 -0700165 pub inputs: Vec<FnArg>,
David Tolnayb79ee962016-09-04 09:39:20 -0700166 pub output: FunctionRetTy,
167}
168
169/// An argument in a function header.
170///
171/// E.g. `bar: usize` as in `fn foo(bar: usize)`
172#[derive(Debug, Clone, Eq, PartialEq)]
David Tolnay66daf742016-09-07 08:21:49 -0700173pub struct FnArg {
David Tolnayb79ee962016-09-04 09:39:20 -0700174 pub pat: Option<Ident>,
175 pub ty: Ty,
176}
177
178#[derive(Debug, Clone, Eq, PartialEq)]
179pub enum FunctionRetTy {
180 /// Return type is not specified.
181 ///
182 /// Functions default to `()` and
183 /// closures default to inference. Span points to where return
184 /// type would be inserted.
185 Default,
186 /// Everything else
187 Ty(Ty),
188}
189
David Tolnay86eca752016-09-04 11:26:41 -0700190#[cfg(feature = "parsing")]
David Tolnay9d8f1972016-09-04 11:58:48 -0700191pub mod parsing {
192 use super::*;
David Tolnay9d8f1972016-09-04 11:58:48 -0700193 use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
David Tolnay55337722016-09-11 12:58:56 -0700194 use ident::parsing::ident;
David Tolnay9d8f1972016-09-04 11:58:48 -0700195 use nom::{digit, multispace};
196 use std::str;
David Tolnayda4049b2016-09-04 10:59:23 -0700197
David Tolnayb5a7b142016-09-13 22:46:39 -0700198 named!(pub ty -> Ty, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700199 ty_vec
David Tolnayda4049b2016-09-04 10:59:23 -0700200 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700201 ty_fixed_length_vec
David Tolnayb79ee962016-09-04 09:39:20 -0700202 |
David Tolnay9d8f1972016-09-04 11:58:48 -0700203 ty_ptr
204 |
205 ty_rptr
206 |
207 ty_bare_fn
208 |
209 ty_never
210 |
211 ty_tup
212 |
213 ty_path
214 |
215 ty_qpath
216 |
217 ty_impl_trait
218 |
219 ty_paren
220 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700221
David Tolnayb5a7b142016-09-13 22:46:39 -0700222 named!(ty_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700223 punct!("[") >>
224 elem: ty >>
225 punct!("]") >>
226 (Ty::Vec(Box::new(elem)))
227 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700228
David Tolnayb5a7b142016-09-13 22:46:39 -0700229 named!(ty_fixed_length_vec -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700230 punct!("[") >>
231 elem: ty >>
232 punct!(";") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700233 option!(multispace) >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700234 len: map_res!(digit, str::parse) >>
235 punct!("]") >>
236 (Ty::FixedLengthVec(Box::new(elem), len))
David Tolnay9d8f1972016-09-04 11:58:48 -0700237 ));
238
David Tolnayb5a7b142016-09-13 22:46:39 -0700239 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700240 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700241 mutability: alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700242 punct!("const") => { |_| Mutability::Immutable }
243 |
244 punct!("mut") => { |_| Mutability::Mutable }
245 ) >>
246 target: ty >>
247 (Ty::Ptr(Box::new(MutTy {
248 ty: target,
249 mutability: mutability,
250 })))
251 ));
252
David Tolnayb5a7b142016-09-13 22:46:39 -0700253 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700254 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700255 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700256 mutability: mutability >>
257 target: ty >>
258 (Ty::Rptr(life, Box::new(MutTy {
259 ty: target,
260 mutability: mutability,
261 })))
262 ));
263
David Tolnayb5a7b142016-09-13 22:46:39 -0700264 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700265 punct!("fn") >>
266 multispace >>
267 lifetimes: opt_vec!(delimited!(
268 punct!("<"),
269 separated_list!(punct!(","), lifetime_def),
270 punct!(">")
David Tolnay6b7aaf02016-09-04 10:39:25 -0700271 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700272 punct!("(") >>
273 inputs: separated_list!(punct!(","), fn_arg) >>
274 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700275 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700276 punct!("->"),
277 ty
278 )) >>
279 (Ty::BareFn(Box::new(BareFnTy {
280 lifetimes: lifetimes,
281 decl: FnDecl {
282 inputs: inputs,
283 output: match output {
284 Some(ty) => FunctionRetTy::Ty(ty),
285 None => FunctionRetTy::Default,
286 },
287 },
288 })))
289 ));
290
David Tolnayb5a7b142016-09-13 22:46:39 -0700291 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700292
David Tolnayb5a7b142016-09-13 22:46:39 -0700293 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700294 punct!("(") >>
295 elems: separated_list!(punct!(","), ty) >>
296 punct!(")") >>
297 (Ty::Tup(elems))
298 ));
299
David Tolnayb5a7b142016-09-13 22:46:39 -0700300 named!(ty_path -> Ty, map!(path, |p| Ty::Path(None, p)));
David Tolnay9d8f1972016-09-04 11:58:48 -0700301
David Tolnayb5a7b142016-09-13 22:46:39 -0700302 named!(ty_qpath -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700303 punct!("<") >>
304 this: map!(ty, Box::new) >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700305 path: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700306 tuple!(punct!("as"), multispace),
307 path
David Tolnay6b7aaf02016-09-04 10:39:25 -0700308 )) >>
309 punct!(">") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700310 punct!("::") >>
311 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
312 ({
313 match path {
314 Some(mut path) => {
315 let pos = path.segments.len();
316 path.segments.extend(rest);
317 Ty::Path(Some(QSelf { ty: this, position: pos }), path)
David Tolnayb79ee962016-09-04 09:39:20 -0700318 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700319 None => {
320 Ty::Path(Some(QSelf { ty: this, position: 0 }), Path {
321 global: false,
322 segments: rest,
323 })
324 }
325 }
David Tolnay6b7aaf02016-09-04 10:39:25 -0700326 })
David Tolnay9d8f1972016-09-04 11:58:48 -0700327 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700328
David Tolnayb5a7b142016-09-13 22:46:39 -0700329 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700330 punct!("impl") >>
331 multispace >>
332 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
333 (Ty::ImplTrait(elem))
334 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700335
David Tolnayb5a7b142016-09-13 22:46:39 -0700336 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700337 punct!("(") >>
338 elem: ty >>
339 punct!(")") >>
340 (Ty::Paren(Box::new(elem)))
341 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700342
David Tolnayb5a7b142016-09-13 22:46:39 -0700343 named!(mutability -> Mutability, alt!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700344 do_parse!(
345 punct!("mut") >>
346 multispace >>
347 (Mutability::Mutable)
David Tolnay9d8f1972016-09-04 11:58:48 -0700348 )
David Tolnayf6ccb832016-09-04 15:00:56 -0700349 |
350 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700351 ));
352
David Tolnayb5a7b142016-09-13 22:46:39 -0700353 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700354 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700355 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
356 (Path {
357 global: global.is_some(),
358 segments: segments,
359 })
360 ));
361
David Tolnayb5a7b142016-09-13 22:46:39 -0700362 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700363 do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700364 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700365 punct!("<") >>
366 lifetimes: separated_list!(punct!(","), lifetime) >>
367 types: opt_vec!(preceded!(
368 cond!(!lifetimes.is_empty(), punct!(",")),
369 separated_nonempty_list!(
370 punct!(","),
371 terminated!(ty, not!(peek!(punct!("="))))
372 )
373 )) >>
374 bindings: opt_vec!(preceded!(
375 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
376 separated_nonempty_list!(punct!(","), type_binding)
377 )) >>
378 punct!(">") >>
379 (PathSegment {
David Tolnay55337722016-09-11 12:58:56 -0700380 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700381 parameters: PathParameters::AngleBracketed(
382 AngleBracketedParameterData {
383 lifetimes: lifetimes,
384 types: types,
385 bindings: bindings,
386 }
387 ),
388 })
389 )
390 |
David Tolnay55337722016-09-11 12:58:56 -0700391 map!(ident, PathSegment::ident)
David Tolnay9d8f1972016-09-04 11:58:48 -0700392 ));
393
David Tolnayb5a7b142016-09-13 22:46:39 -0700394 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700395 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700396 punct!("=") >>
397 ty: ty >>
398 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700399 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700400 ty: ty,
401 })
402 ));
403
David Tolnayb5a7b142016-09-13 22:46:39 -0700404 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700405 bound_lifetimes: bound_lifetimes >>
406 trait_ref: path >>
407 (PolyTraitRef {
408 bound_lifetimes: bound_lifetimes,
409 trait_ref: trait_ref,
410 })
411 ));
412
David Tolnayb5a7b142016-09-13 22:46:39 -0700413 named!(fn_arg -> FnArg, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700414 pat: option!(terminated!(ident, punct!(":"))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700415 ty: ty >>
David Tolnay66daf742016-09-07 08:21:49 -0700416 (FnArg {
David Tolnay9d8f1972016-09-04 11:58:48 -0700417 pat: pat,
418 ty: ty,
419 })
420 ));
421}
David Tolnay87d0b442016-09-04 11:52:12 -0700422
423#[cfg(feature = "printing")]
424mod printing {
425 use super::*;
426 use quote::{Tokens, ToTokens};
427
428 impl ToTokens for Ty {
429 fn to_tokens(&self, tokens: &mut Tokens) {
430 match *self {
431 Ty::Vec(ref inner) => {
432 tokens.append("[");
433 inner.to_tokens(tokens);
434 tokens.append("]");
435 }
436 Ty::FixedLengthVec(ref inner, len) => {
437 tokens.append("[");
438 inner.to_tokens(tokens);
439 tokens.append(";");
440 len.to_tokens(tokens);
441 tokens.append("]");
442 }
443 Ty::Ptr(ref target) => {
444 tokens.append("*");
445 match target.mutability {
446 Mutability::Mutable => tokens.append("mut"),
447 Mutability::Immutable => tokens.append("const"),
448 }
449 target.ty.to_tokens(tokens);
450 }
451 Ty::Rptr(ref lifetime, ref target) => {
452 tokens.append("&");
453 lifetime.to_tokens(tokens);
454 if let Mutability::Mutable = target.mutability {
455 tokens.append("mut");
456 }
457 target.ty.to_tokens(tokens);
458 }
459 Ty::BareFn(ref func) => {
460 func.to_tokens(tokens);
461 }
462 Ty::Never => {
463 tokens.append("!");
464 }
465 Ty::Tup(ref elems) => {
466 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700467 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700468 if elems.len() == 1 {
469 tokens.append(",");
470 }
471 tokens.append(")");
472 }
David Tolnayf69904a2016-09-04 14:46:07 -0700473 Ty::Path(None, ref path) => {
474 path.to_tokens(tokens);
475 }
476 Ty::Path(Some(ref qself), ref path) => {
477 tokens.append("<");
478 qself.ty.to_tokens(tokens);
479 if qself.position > 0 {
480 tokens.append("as");
481 for (i, segment) in path.segments.iter()
482 .take(qself.position)
483 .enumerate()
484 {
485 if i > 0 || path.global {
486 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700487 }
David Tolnayf69904a2016-09-04 14:46:07 -0700488 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700489 }
David Tolnayf69904a2016-09-04 14:46:07 -0700490 }
491 tokens.append(">");
492 for segment in path.segments.iter().skip(qself.position) {
493 tokens.append("::");
494 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700495 }
496 }
497 Ty::ObjectSum(_, _) => unimplemented!(),
498 Ty::PolyTraitRef(_) => unimplemented!(),
499 Ty::ImplTrait(ref bounds) => {
500 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700501 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700502 }
503 Ty::Paren(ref inner) => {
504 tokens.append("(");
505 inner.to_tokens(tokens);
506 tokens.append(")");
507 }
508 Ty::Infer => {
509 tokens.append("_");
510 }
511 }
512 }
513 }
514
515 impl ToTokens for Path {
516 fn to_tokens(&self, tokens: &mut Tokens) {
517 for (i, segment) in self.segments.iter().enumerate() {
518 if i > 0 || self.global {
519 tokens.append("::");
520 }
521 segment.to_tokens(tokens);
522 }
523 }
524 }
525
526 impl ToTokens for PathSegment {
527 fn to_tokens(&self, tokens: &mut Tokens) {
528 self.ident.to_tokens(tokens);
529 self.parameters.to_tokens(tokens);
530 }
531 }
532
533 impl ToTokens for PathParameters {
534 fn to_tokens(&self, tokens: &mut Tokens) {
535 match *self {
536 PathParameters::AngleBracketed(ref parameters) => {
537 parameters.to_tokens(tokens);
538 }
539 PathParameters::Parenthesized(ref parameters) => {
540 parameters.to_tokens(tokens);
541 }
542 }
543 }
544 }
545
546 impl ToTokens for AngleBracketedParameterData {
547 fn to_tokens(&self, tokens: &mut Tokens) {
548 let has_lifetimes = !self.lifetimes.is_empty();
549 let has_types = !self.types.is_empty();
550 let has_bindings = !self.bindings.is_empty();
551 if !has_lifetimes && !has_types && !has_bindings {
552 return;
553 }
554
555 tokens.append("<");
556
557 let mut first = true;
558 for lifetime in &self.lifetimes {
559 if !first {
560 tokens.append(",");
561 }
562 lifetime.to_tokens(tokens);
563 first = false;
564 }
565 for ty in &self.types {
566 if !first {
567 tokens.append(",");
568 }
569 ty.to_tokens(tokens);
570 first = false;
571 }
572 for binding in &self.bindings {
573 if !first {
574 tokens.append(",");
575 }
576 binding.to_tokens(tokens);
577 first = false;
578 }
579
580 tokens.append(">");
581 }
582 }
583
584 impl ToTokens for TypeBinding {
585 fn to_tokens(&self, tokens: &mut Tokens) {
586 self.ident.to_tokens(tokens);
587 tokens.append("=");
588 self.ty.to_tokens(tokens);
589 }
590 }
591
592 impl ToTokens for ParenthesizedParameterData {
593 fn to_tokens(&self, tokens: &mut Tokens) {
594 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700595 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700596 tokens.append(")");
597 if let Some(ref output) = self.output {
598 tokens.append("->");
599 output.to_tokens(tokens);
600 }
601 }
602 }
603
604 impl ToTokens for PolyTraitRef {
605 fn to_tokens(&self, tokens: &mut Tokens) {
606 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700607 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700608 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700609 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700610 tokens.append(">");
611 }
612 self.trait_ref.to_tokens(tokens);
613 }
614 }
615
616 impl ToTokens for BareFnTy {
617 fn to_tokens(&self, tokens: &mut Tokens) {
618 tokens.append("fn");
619 if !self.lifetimes.is_empty() {
620 tokens.append("<");
621 for (i, lifetime) in self.lifetimes.iter().enumerate() {
622 if i > 0 {
623 tokens.append(",");
624 }
625 lifetime.to_tokens(tokens);
626 }
627 tokens.append(">");
628 }
629 tokens.append("(");
630 tokens.append(")");
631 }
632 }
633}