blob: aa9c9df3e883d943d19a7db76e29636c38af89f9 [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 Tolnay42602292016-10-01 22:25:45 -0700157 pub decl: FnDecl,
David Tolnayb79ee962016-09-04 09:39:20 -0700158}
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 Tolnayde206222016-09-30 11:47:01 -0700195 use lit::parsing::int;
David Tolnay9d8f1972016-09-04 11:58:48 -0700196 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 Tolnayde206222016-09-30 11:47:01 -0700233 len: int >>
David Tolnayc94c38a2016-09-05 17:02:03 -0700234 punct!("]") >>
David Tolnayde206222016-09-30 11:47:01 -0700235 (Ty::FixedLengthVec(Box::new(elem), len.0 as usize))
David Tolnay9d8f1972016-09-04 11:58:48 -0700236 ));
237
David Tolnayb5a7b142016-09-13 22:46:39 -0700238 named!(ty_ptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700239 punct!("*") >>
David Tolnayb5a7b142016-09-13 22:46:39 -0700240 mutability: alt!(
David Tolnay10413f02016-09-30 09:12:02 -0700241 keyword!("const") => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700242 |
David Tolnay10413f02016-09-30 09:12:02 -0700243 keyword!("mut") => { |_| Mutability::Mutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700244 ) >>
245 target: ty >>
246 (Ty::Ptr(Box::new(MutTy {
247 ty: target,
248 mutability: mutability,
249 })))
250 ));
251
David Tolnayb5a7b142016-09-13 22:46:39 -0700252 named!(ty_rptr -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700253 punct!("&") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700254 life: option!(lifetime) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700255 mutability: mutability >>
256 target: ty >>
257 (Ty::Rptr(life, Box::new(MutTy {
258 ty: target,
259 mutability: mutability,
260 })))
261 ));
262
David Tolnayb5a7b142016-09-13 22:46:39 -0700263 named!(ty_bare_fn -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700264 keyword!("fn") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700265 lifetimes: opt_vec!(delimited!(
266 punct!("<"),
267 separated_list!(punct!(","), lifetime_def),
268 punct!(">")
David Tolnay6b7aaf02016-09-04 10:39:25 -0700269 )) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700270 punct!("(") >>
271 inputs: separated_list!(punct!(","), fn_arg) >>
272 punct!(")") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700273 output: option!(preceded!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700274 punct!("->"),
275 ty
276 )) >>
277 (Ty::BareFn(Box::new(BareFnTy {
278 lifetimes: lifetimes,
279 decl: FnDecl {
280 inputs: inputs,
281 output: match output {
282 Some(ty) => FunctionRetTy::Ty(ty),
283 None => FunctionRetTy::Default,
284 },
285 },
286 })))
287 ));
288
David Tolnayb5a7b142016-09-13 22:46:39 -0700289 named!(ty_never -> Ty, map!(punct!("!"), |_| Ty::Never));
David Tolnay9d8f1972016-09-04 11:58:48 -0700290
David Tolnayb5a7b142016-09-13 22:46:39 -0700291 named!(ty_tup -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700292 punct!("(") >>
293 elems: separated_list!(punct!(","), ty) >>
294 punct!(")") >>
295 (Ty::Tup(elems))
296 ));
297
David Tolnayb5a7b142016-09-13 22:46:39 -0700298 named!(ty_path -> Ty, map!(path, |p| Ty::Path(None, p)));
David Tolnay9d8f1972016-09-04 11:58:48 -0700299
David Tolnayb5a7b142016-09-13 22:46:39 -0700300 named!(ty_qpath -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700301 punct!("<") >>
302 this: map!(ty, Box::new) >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700303 path: option!(preceded!(
David Tolnay10413f02016-09-30 09:12:02 -0700304 keyword!("as"),
David Tolnay9d8f1972016-09-04 11:58:48 -0700305 path
David Tolnay6b7aaf02016-09-04 10:39:25 -0700306 )) >>
307 punct!(">") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700308 punct!("::") >>
309 rest: separated_nonempty_list!(punct!("::"), path_segment) >>
310 ({
311 match path {
312 Some(mut path) => {
313 let pos = path.segments.len();
314 path.segments.extend(rest);
315 Ty::Path(Some(QSelf { ty: this, position: pos }), path)
David Tolnayb79ee962016-09-04 09:39:20 -0700316 }
David Tolnay9d8f1972016-09-04 11:58:48 -0700317 None => {
318 Ty::Path(Some(QSelf { ty: this, position: 0 }), Path {
319 global: false,
320 segments: rest,
321 })
322 }
323 }
David Tolnay6b7aaf02016-09-04 10:39:25 -0700324 })
David Tolnay9d8f1972016-09-04 11:58:48 -0700325 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700326
David Tolnayb5a7b142016-09-13 22:46:39 -0700327 named!(ty_impl_trait -> Ty, do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700328 keyword!("impl") >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700329 elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
330 (Ty::ImplTrait(elem))
331 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700332
David Tolnayb5a7b142016-09-13 22:46:39 -0700333 named!(ty_paren -> Ty, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700334 punct!("(") >>
335 elem: ty >>
336 punct!(")") >>
337 (Ty::Paren(Box::new(elem)))
338 ));
David Tolnayb79ee962016-09-04 09:39:20 -0700339
David Tolnay47a877c2016-10-01 16:50:55 -0700340 named!(pub mutability -> Mutability, alt!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700341 do_parse!(
David Tolnay10413f02016-09-30 09:12:02 -0700342 keyword!("mut") >>
David Tolnayf6ccb832016-09-04 15:00:56 -0700343 (Mutability::Mutable)
David Tolnay9d8f1972016-09-04 11:58:48 -0700344 )
David Tolnayf6ccb832016-09-04 15:00:56 -0700345 |
346 epsilon!() => { |_| Mutability::Immutable }
David Tolnay9d8f1972016-09-04 11:58:48 -0700347 ));
348
David Tolnayb5a7b142016-09-13 22:46:39 -0700349 named!(pub path -> Path, do_parse!(
David Tolnayf6ccb832016-09-04 15:00:56 -0700350 global: option!(punct!("::")) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700351 segments: separated_nonempty_list!(punct!("::"), path_segment) >>
352 (Path {
353 global: global.is_some(),
354 segments: segments,
355 })
356 ));
357
David Tolnayb5a7b142016-09-13 22:46:39 -0700358 named!(path_segment -> PathSegment, alt!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700359 do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700360 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700361 punct!("<") >>
362 lifetimes: separated_list!(punct!(","), lifetime) >>
363 types: opt_vec!(preceded!(
364 cond!(!lifetimes.is_empty(), punct!(",")),
365 separated_nonempty_list!(
366 punct!(","),
367 terminated!(ty, not!(peek!(punct!("="))))
368 )
369 )) >>
370 bindings: opt_vec!(preceded!(
371 cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")),
372 separated_nonempty_list!(punct!(","), type_binding)
373 )) >>
374 punct!(">") >>
375 (PathSegment {
David Tolnay55337722016-09-11 12:58:56 -0700376 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700377 parameters: PathParameters::AngleBracketed(
378 AngleBracketedParameterData {
379 lifetimes: lifetimes,
380 types: types,
381 bindings: bindings,
382 }
383 ),
384 })
385 )
386 |
David Tolnay55337722016-09-11 12:58:56 -0700387 map!(ident, PathSegment::ident)
David Tolnay9d8f1972016-09-04 11:58:48 -0700388 ));
389
David Tolnayb5a7b142016-09-13 22:46:39 -0700390 named!(type_binding -> TypeBinding, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700391 id: ident >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700392 punct!("=") >>
393 ty: ty >>
394 (TypeBinding {
David Tolnay55337722016-09-11 12:58:56 -0700395 ident: id,
David Tolnay9d8f1972016-09-04 11:58:48 -0700396 ty: ty,
397 })
398 ));
399
David Tolnayb5a7b142016-09-13 22:46:39 -0700400 named!(pub poly_trait_ref -> PolyTraitRef, do_parse!(
David Tolnay9d8f1972016-09-04 11:58:48 -0700401 bound_lifetimes: bound_lifetimes >>
402 trait_ref: path >>
403 (PolyTraitRef {
404 bound_lifetimes: bound_lifetimes,
405 trait_ref: trait_ref,
406 })
407 ));
408
David Tolnay42602292016-10-01 22:25:45 -0700409 named!(pub fn_arg -> FnArg, do_parse!(
David Tolnay55337722016-09-11 12:58:56 -0700410 pat: option!(terminated!(ident, punct!(":"))) >>
David Tolnay9d8f1972016-09-04 11:58:48 -0700411 ty: ty >>
David Tolnay66daf742016-09-07 08:21:49 -0700412 (FnArg {
David Tolnay9d8f1972016-09-04 11:58:48 -0700413 pat: pat,
414 ty: ty,
415 })
416 ));
417}
David Tolnay87d0b442016-09-04 11:52:12 -0700418
419#[cfg(feature = "printing")]
420mod printing {
421 use super::*;
422 use quote::{Tokens, ToTokens};
423
424 impl ToTokens for Ty {
425 fn to_tokens(&self, tokens: &mut Tokens) {
426 match *self {
427 Ty::Vec(ref inner) => {
428 tokens.append("[");
429 inner.to_tokens(tokens);
430 tokens.append("]");
431 }
432 Ty::FixedLengthVec(ref inner, len) => {
433 tokens.append("[");
434 inner.to_tokens(tokens);
435 tokens.append(";");
David Tolnayde206222016-09-30 11:47:01 -0700436 tokens.append(&len.to_string());
David Tolnay87d0b442016-09-04 11:52:12 -0700437 tokens.append("]");
438 }
439 Ty::Ptr(ref target) => {
440 tokens.append("*");
441 match target.mutability {
442 Mutability::Mutable => tokens.append("mut"),
443 Mutability::Immutable => tokens.append("const"),
444 }
445 target.ty.to_tokens(tokens);
446 }
447 Ty::Rptr(ref lifetime, ref target) => {
448 tokens.append("&");
449 lifetime.to_tokens(tokens);
David Tolnay47a877c2016-10-01 16:50:55 -0700450 target.mutability.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700451 target.ty.to_tokens(tokens);
452 }
453 Ty::BareFn(ref func) => {
454 func.to_tokens(tokens);
455 }
456 Ty::Never => {
457 tokens.append("!");
458 }
459 Ty::Tup(ref elems) => {
460 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700461 tokens.append_separated(elems, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700462 if elems.len() == 1 {
463 tokens.append(",");
464 }
465 tokens.append(")");
466 }
David Tolnayf69904a2016-09-04 14:46:07 -0700467 Ty::Path(None, ref path) => {
468 path.to_tokens(tokens);
469 }
470 Ty::Path(Some(ref qself), ref path) => {
471 tokens.append("<");
472 qself.ty.to_tokens(tokens);
473 if qself.position > 0 {
474 tokens.append("as");
475 for (i, segment) in path.segments.iter()
476 .take(qself.position)
477 .enumerate()
478 {
479 if i > 0 || path.global {
480 tokens.append("::");
David Tolnay87d0b442016-09-04 11:52:12 -0700481 }
David Tolnayf69904a2016-09-04 14:46:07 -0700482 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700483 }
David Tolnayf69904a2016-09-04 14:46:07 -0700484 }
485 tokens.append(">");
486 for segment in path.segments.iter().skip(qself.position) {
487 tokens.append("::");
488 segment.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700489 }
490 }
491 Ty::ObjectSum(_, _) => unimplemented!(),
492 Ty::PolyTraitRef(_) => unimplemented!(),
493 Ty::ImplTrait(ref bounds) => {
494 tokens.append("impl");
David Tolnay94ebdf92016-09-04 13:33:16 -0700495 tokens.append_separated(bounds, "+");
David Tolnay87d0b442016-09-04 11:52:12 -0700496 }
497 Ty::Paren(ref inner) => {
498 tokens.append("(");
499 inner.to_tokens(tokens);
500 tokens.append(")");
501 }
502 Ty::Infer => {
503 tokens.append("_");
504 }
505 }
506 }
507 }
508
David Tolnay47a877c2016-10-01 16:50:55 -0700509 impl ToTokens for Mutability {
510 fn to_tokens(&self, tokens: &mut Tokens) {
511 if let Mutability::Mutable = *self {
512 tokens.append("mut");
513 }
514 }
515 }
516
David Tolnay87d0b442016-09-04 11:52:12 -0700517 impl ToTokens for Path {
518 fn to_tokens(&self, tokens: &mut Tokens) {
519 for (i, segment) in self.segments.iter().enumerate() {
520 if i > 0 || self.global {
521 tokens.append("::");
522 }
523 segment.to_tokens(tokens);
524 }
525 }
526 }
527
528 impl ToTokens for PathSegment {
529 fn to_tokens(&self, tokens: &mut Tokens) {
530 self.ident.to_tokens(tokens);
531 self.parameters.to_tokens(tokens);
532 }
533 }
534
535 impl ToTokens for PathParameters {
536 fn to_tokens(&self, tokens: &mut Tokens) {
537 match *self {
538 PathParameters::AngleBracketed(ref parameters) => {
539 parameters.to_tokens(tokens);
540 }
541 PathParameters::Parenthesized(ref parameters) => {
542 parameters.to_tokens(tokens);
543 }
544 }
545 }
546 }
547
548 impl ToTokens for AngleBracketedParameterData {
549 fn to_tokens(&self, tokens: &mut Tokens) {
550 let has_lifetimes = !self.lifetimes.is_empty();
551 let has_types = !self.types.is_empty();
552 let has_bindings = !self.bindings.is_empty();
553 if !has_lifetimes && !has_types && !has_bindings {
554 return;
555 }
556
557 tokens.append("<");
558
559 let mut first = true;
560 for lifetime in &self.lifetimes {
561 if !first {
562 tokens.append(",");
563 }
564 lifetime.to_tokens(tokens);
565 first = false;
566 }
567 for ty in &self.types {
568 if !first {
569 tokens.append(",");
570 }
571 ty.to_tokens(tokens);
572 first = false;
573 }
574 for binding in &self.bindings {
575 if !first {
576 tokens.append(",");
577 }
578 binding.to_tokens(tokens);
579 first = false;
580 }
581
582 tokens.append(">");
583 }
584 }
585
586 impl ToTokens for TypeBinding {
587 fn to_tokens(&self, tokens: &mut Tokens) {
588 self.ident.to_tokens(tokens);
589 tokens.append("=");
590 self.ty.to_tokens(tokens);
591 }
592 }
593
594 impl ToTokens for ParenthesizedParameterData {
595 fn to_tokens(&self, tokens: &mut Tokens) {
596 tokens.append("(");
David Tolnay94ebdf92016-09-04 13:33:16 -0700597 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700598 tokens.append(")");
599 if let Some(ref output) = self.output {
600 tokens.append("->");
601 output.to_tokens(tokens);
602 }
603 }
604 }
605
606 impl ToTokens for PolyTraitRef {
607 fn to_tokens(&self, tokens: &mut Tokens) {
608 if !self.bound_lifetimes.is_empty() {
David Tolnaye8796aa2016-09-04 14:48:22 -0700609 tokens.append("for");
David Tolnay87d0b442016-09-04 11:52:12 -0700610 tokens.append("<");
David Tolnay94ebdf92016-09-04 13:33:16 -0700611 tokens.append_separated(&self.bound_lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700612 tokens.append(">");
613 }
614 self.trait_ref.to_tokens(tokens);
615 }
616 }
617
618 impl ToTokens for BareFnTy {
619 fn to_tokens(&self, tokens: &mut Tokens) {
620 tokens.append("fn");
621 if !self.lifetimes.is_empty() {
622 tokens.append("<");
David Tolnay42602292016-10-01 22:25:45 -0700623 tokens.append_separated(&self.lifetimes, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700624 tokens.append(">");
625 }
David Tolnay42602292016-10-01 22:25:45 -0700626 self.decl.to_tokens(tokens);
627 }
628 }
629
630 impl ToTokens for FnDecl {
631 fn to_tokens(&self, tokens: &mut Tokens) {
David Tolnay87d0b442016-09-04 11:52:12 -0700632 tokens.append("(");
David Tolnay42602292016-10-01 22:25:45 -0700633 tokens.append_separated(&self.inputs, ",");
David Tolnay87d0b442016-09-04 11:52:12 -0700634 tokens.append(")");
David Tolnay42602292016-10-01 22:25:45 -0700635 if let FunctionRetTy::Ty(ref ty) = self.output {
636 tokens.append("->");
637 ty.to_tokens(tokens);
638 }
639 }
640 }
641
642 impl ToTokens for FnArg {
643 fn to_tokens(&self, tokens: &mut Tokens) {
644 if let Some(ref pat) = self.pat {
645 pat.to_tokens(tokens);
646 tokens.append(":");
647 }
648 self.ty.to_tokens(tokens);
David Tolnay87d0b442016-09-04 11:52:12 -0700649 }
650 }
651}