David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 1 | use super::*; |
| 2 | |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 3 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 4 | pub enum Ty { |
| 5 | /// A variable-length array (`[T]`) |
| 6 | Vec(Box<Ty>), |
| 7 | /// A fixed length array (`[T; n]`) |
| 8 | FixedLengthVec(Box<Ty>, usize), |
| 9 | /// A raw pointer (`*const T` or `*mut T`) |
| 10 | Ptr(Box<MutTy>), |
| 11 | /// A reference (`&'a T` or `&'a mut T`) |
| 12 | Rptr(Option<Lifetime>, Box<MutTy>), |
| 13 | /// A bare function (e.g. `fn(usize) -> bool`) |
| 14 | BareFn(Box<BareFnTy>), |
| 15 | /// The never type (`!`) |
| 16 | Never, |
| 17 | /// A tuple (`(A, B, C, D, ...)`) |
| 18 | Tup(Vec<Ty>), |
| 19 | /// A path (`module::module::...::Type`), optionally |
| 20 | /// "qualified", e.g. `<Vec<T> as SomeTrait>::SomeType`. |
| 21 | /// |
| 22 | /// Type parameters are stored in the Path itself |
| 23 | Path(Option<QSelf>, Path), |
| 24 | /// Something like `A+B`. Note that `B` must always be a path. |
| 25 | ObjectSum(Box<Ty>, Vec<TyParamBound>), |
| 26 | /// A type like `for<'a> Foo<&'a Bar>` |
| 27 | PolyTraitRef(Vec<TyParamBound>), |
| 28 | /// An `impl TraitA+TraitB` type. |
| 29 | ImplTrait(Vec<TyParamBound>), |
| 30 | /// No-op; kept solely so that we can pretty-print faithfully |
| 31 | Paren(Box<Ty>), |
| 32 | /// TyKind::Infer means the type should be inferred instead of it having been |
| 33 | /// specified. This can appear anywhere in a type. |
| 34 | Infer, |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 38 | pub struct MutTy { |
| 39 | pub ty: Ty, |
| 40 | pub mutability: Mutability, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 44 | pub enum Mutability { |
| 45 | Mutable, |
| 46 | Immutable, |
| 47 | } |
| 48 | |
| 49 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 50 | pub struct Path { |
| 51 | pub global: bool, |
| 52 | pub segments: Vec<PathSegment>, |
| 53 | } |
| 54 | |
| 55 | /// A segment of a path: an identifier, an optional lifetime, and a set of types. |
| 56 | /// |
| 57 | /// E.g. `std`, `String` or `Box<T>` |
| 58 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 59 | pub struct PathSegment { |
| 60 | pub ident: Ident, |
| 61 | pub parameters: PathParameters, |
| 62 | } |
| 63 | |
| 64 | impl PathSegment { |
| 65 | pub fn ident(ident: Ident) -> Self { |
| 66 | PathSegment { |
| 67 | ident: ident, |
| 68 | parameters: PathParameters::none(), |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// Parameters of a path segment. |
| 74 | /// |
| 75 | /// E.g. `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)` |
| 76 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 77 | pub enum PathParameters { |
| 78 | /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>` |
| 79 | AngleBracketed(AngleBracketedParameterData), |
| 80 | /// The `(A, B)` and `C` in `Foo(A, B) -> C` |
| 81 | Parenthesized(ParenthesizedParameterData), |
| 82 | } |
| 83 | |
| 84 | impl PathParameters { |
| 85 | pub fn none() -> Self { |
| 86 | PathParameters::AngleBracketed(AngleBracketedParameterData::default()) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// A path like `Foo<'a, T>` |
| 91 | #[derive(Debug, Clone, Eq, PartialEq, Default)] |
| 92 | pub struct AngleBracketedParameterData { |
| 93 | /// The lifetime parameters for this path segment. |
| 94 | pub lifetimes: Vec<Lifetime>, |
| 95 | /// The type parameters for this path segment, if present. |
| 96 | pub types: Vec<Ty>, |
| 97 | /// Bindings (equality constraints) on associated types, if present. |
| 98 | /// |
| 99 | /// E.g., `Foo<A=Bar>`. |
| 100 | pub bindings: Vec<TypeBinding>, |
| 101 | } |
| 102 | |
| 103 | /// Bind a type to an associated type: `A=Foo`. |
| 104 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 105 | pub struct TypeBinding { |
| 106 | pub ident: Ident, |
| 107 | pub ty: Ty, |
| 108 | } |
| 109 | |
| 110 | /// A path like `Foo(A,B) -> C` |
| 111 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 112 | pub struct ParenthesizedParameterData { |
| 113 | /// `(A, B)` |
| 114 | pub inputs: Vec<Ty>, |
| 115 | /// `C` |
| 116 | pub output: Option<Ty>, |
| 117 | } |
| 118 | |
| 119 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 120 | pub struct PolyTraitRef { |
| 121 | /// The `'a` in `<'a> Foo<&'a T>` |
| 122 | pub bound_lifetimes: Vec<LifetimeDef>, |
| 123 | /// The `Foo<&'a T>` in `<'a> Foo<&'a T>` |
| 124 | pub trait_ref: Path, |
| 125 | } |
| 126 | |
| 127 | /// The explicit Self type in a "qualified path". The actual |
| 128 | /// path, including the trait and the associated item, is stored |
| 129 | /// separately. `position` represents the index of the associated |
| 130 | /// item qualified with this Self type. |
| 131 | /// |
| 132 | /// ```rust,ignore |
| 133 | /// <Vec<T> as a::b::Trait>::AssociatedItem |
| 134 | /// ^~~~~ ~~~~~~~~~~~~~~^ |
| 135 | /// ty position = 3 |
| 136 | /// |
| 137 | /// <Vec<T>>::AssociatedItem |
| 138 | /// ^~~~~ ^ |
| 139 | /// ty position = 0 |
| 140 | /// ``` |
| 141 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 142 | pub struct QSelf { |
| 143 | pub ty: Box<Ty>, |
| 144 | pub position: usize |
| 145 | } |
| 146 | |
| 147 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 148 | pub struct BareFnTy { |
| 149 | pub lifetimes: Vec<LifetimeDef>, |
| 150 | pub decl: FnDecl |
| 151 | } |
| 152 | |
| 153 | /// Header (not the body) of a function declaration. |
| 154 | /// |
| 155 | /// E.g. `fn foo(bar: baz)` |
| 156 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 157 | pub struct FnDecl { |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame^] | 158 | pub inputs: Vec<FnArg>, |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 159 | pub output: FunctionRetTy, |
| 160 | } |
| 161 | |
| 162 | /// An argument in a function header. |
| 163 | /// |
| 164 | /// E.g. `bar: usize` as in `fn foo(bar: usize)` |
| 165 | #[derive(Debug, Clone, Eq, PartialEq)] |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame^] | 166 | pub struct FnArg { |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 167 | pub pat: Option<Ident>, |
| 168 | pub ty: Ty, |
| 169 | } |
| 170 | |
| 171 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 172 | pub enum FunctionRetTy { |
| 173 | /// Return type is not specified. |
| 174 | /// |
| 175 | /// Functions default to `()` and |
| 176 | /// closures default to inference. Span points to where return |
| 177 | /// type would be inserted. |
| 178 | Default, |
| 179 | /// Everything else |
| 180 | Ty(Ty), |
| 181 | } |
| 182 | |
David Tolnay | 86eca75 | 2016-09-04 11:26:41 -0700 | [diff] [blame] | 183 | #[cfg(feature = "parsing")] |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 184 | pub mod parsing { |
| 185 | use super::*; |
| 186 | use common::parsing::word; |
| 187 | use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes}; |
| 188 | use nom::{digit, multispace}; |
| 189 | use std::str; |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 190 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 191 | named!(pub ty<&str, Ty>, alt_complete!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 192 | ty_vec |
David Tolnay | da4049b | 2016-09-04 10:59:23 -0700 | [diff] [blame] | 193 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 194 | ty_fixed_length_vec |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 195 | | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 196 | ty_ptr |
| 197 | | |
| 198 | ty_rptr |
| 199 | | |
| 200 | ty_bare_fn |
| 201 | | |
| 202 | ty_never |
| 203 | | |
| 204 | ty_tup |
| 205 | | |
| 206 | ty_path |
| 207 | | |
| 208 | ty_qpath |
| 209 | | |
| 210 | ty_impl_trait |
| 211 | | |
| 212 | ty_paren |
| 213 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 214 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 215 | named!(ty_vec<&str, Ty>, do_parse!( |
| 216 | punct!("[") >> |
| 217 | elem: ty >> |
| 218 | punct!("]") >> |
| 219 | (Ty::Vec(Box::new(elem))) |
| 220 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 221 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 222 | named!(ty_fixed_length_vec<&str, Ty>, do_parse!( |
| 223 | punct!("[") >> |
| 224 | elem: ty >> |
| 225 | punct!(";") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 226 | option!(multispace) >> |
David Tolnay | c94c38a | 2016-09-05 17:02:03 -0700 | [diff] [blame] | 227 | len: map_res!(digit, str::parse) >> |
| 228 | punct!("]") >> |
| 229 | (Ty::FixedLengthVec(Box::new(elem), len)) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 230 | )); |
| 231 | |
| 232 | named!(ty_ptr<&str, Ty>, do_parse!( |
| 233 | punct!("*") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 234 | mutability: alt_complete!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 235 | punct!("const") => { |_| Mutability::Immutable } |
| 236 | | |
| 237 | punct!("mut") => { |_| Mutability::Mutable } |
| 238 | ) >> |
| 239 | target: ty >> |
| 240 | (Ty::Ptr(Box::new(MutTy { |
| 241 | ty: target, |
| 242 | mutability: mutability, |
| 243 | }))) |
| 244 | )); |
| 245 | |
| 246 | named!(ty_rptr<&str, Ty>, do_parse!( |
| 247 | punct!("&") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 248 | life: option!(lifetime) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 249 | mutability: mutability >> |
| 250 | target: ty >> |
| 251 | (Ty::Rptr(life, Box::new(MutTy { |
| 252 | ty: target, |
| 253 | mutability: mutability, |
| 254 | }))) |
| 255 | )); |
| 256 | |
| 257 | named!(ty_bare_fn<&str, Ty>, do_parse!( |
| 258 | punct!("fn") >> |
| 259 | multispace >> |
| 260 | lifetimes: opt_vec!(delimited!( |
| 261 | punct!("<"), |
| 262 | separated_list!(punct!(","), lifetime_def), |
| 263 | punct!(">") |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 264 | )) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 265 | punct!("(") >> |
| 266 | inputs: separated_list!(punct!(","), fn_arg) >> |
| 267 | punct!(")") >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 268 | output: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 269 | punct!("->"), |
| 270 | ty |
| 271 | )) >> |
| 272 | (Ty::BareFn(Box::new(BareFnTy { |
| 273 | lifetimes: lifetimes, |
| 274 | decl: FnDecl { |
| 275 | inputs: inputs, |
| 276 | output: match output { |
| 277 | Some(ty) => FunctionRetTy::Ty(ty), |
| 278 | None => FunctionRetTy::Default, |
| 279 | }, |
| 280 | }, |
| 281 | }))) |
| 282 | )); |
| 283 | |
| 284 | named!(ty_never<&str, Ty>, map!(punct!("!"), |_| Ty::Never)); |
| 285 | |
| 286 | named!(ty_tup<&str, Ty>, do_parse!( |
| 287 | punct!("(") >> |
| 288 | elems: separated_list!(punct!(","), ty) >> |
| 289 | punct!(")") >> |
| 290 | (Ty::Tup(elems)) |
| 291 | )); |
| 292 | |
| 293 | named!(ty_path<&str, Ty>, map!(path, |p| Ty::Path(None, p))); |
| 294 | |
| 295 | named!(ty_qpath<&str, Ty>, do_parse!( |
| 296 | punct!("<") >> |
| 297 | this: map!(ty, Box::new) >> |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 298 | path: option!(preceded!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 299 | tuple!(punct!("as"), multispace), |
| 300 | path |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 301 | )) >> |
| 302 | punct!(">") >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 303 | punct!("::") >> |
| 304 | rest: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 305 | ({ |
| 306 | match path { |
| 307 | Some(mut path) => { |
| 308 | let pos = path.segments.len(); |
| 309 | path.segments.extend(rest); |
| 310 | Ty::Path(Some(QSelf { ty: this, position: pos }), path) |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 311 | } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 312 | None => { |
| 313 | Ty::Path(Some(QSelf { ty: this, position: 0 }), Path { |
| 314 | global: false, |
| 315 | segments: rest, |
| 316 | }) |
| 317 | } |
| 318 | } |
David Tolnay | 6b7aaf0 | 2016-09-04 10:39:25 -0700 | [diff] [blame] | 319 | }) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 320 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 321 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 322 | named!(ty_impl_trait<&str, Ty>, do_parse!( |
| 323 | punct!("impl") >> |
| 324 | multispace >> |
| 325 | elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >> |
| 326 | (Ty::ImplTrait(elem)) |
| 327 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 328 | |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 329 | named!(ty_paren<&str, Ty>, do_parse!( |
| 330 | punct!("(") >> |
| 331 | elem: ty >> |
| 332 | punct!(")") >> |
| 333 | (Ty::Paren(Box::new(elem))) |
| 334 | )); |
David Tolnay | b79ee96 | 2016-09-04 09:39:20 -0700 | [diff] [blame] | 335 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 336 | named!(mutability<&str, Mutability>, alt_complete!( |
| 337 | do_parse!( |
| 338 | punct!("mut") >> |
| 339 | multispace >> |
| 340 | (Mutability::Mutable) |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 341 | ) |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 342 | | |
| 343 | epsilon!() => { |_| Mutability::Immutable } |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 344 | )); |
| 345 | |
| 346 | named!(path<&str, Path>, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 347 | global: option!(punct!("::")) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 348 | segments: separated_nonempty_list!(punct!("::"), path_segment) >> |
| 349 | (Path { |
| 350 | global: global.is_some(), |
| 351 | segments: segments, |
| 352 | }) |
| 353 | )); |
| 354 | |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 355 | named!(path_segment<&str, PathSegment>, alt_complete!( |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 356 | do_parse!( |
| 357 | ident: word >> |
| 358 | punct!("<") >> |
| 359 | lifetimes: separated_list!(punct!(","), lifetime) >> |
| 360 | types: opt_vec!(preceded!( |
| 361 | cond!(!lifetimes.is_empty(), punct!(",")), |
| 362 | separated_nonempty_list!( |
| 363 | punct!(","), |
| 364 | terminated!(ty, not!(peek!(punct!("=")))) |
| 365 | ) |
| 366 | )) >> |
| 367 | bindings: opt_vec!(preceded!( |
| 368 | cond!(!lifetimes.is_empty() || !types.is_empty(), punct!(",")), |
| 369 | separated_nonempty_list!(punct!(","), type_binding) |
| 370 | )) >> |
| 371 | punct!(">") >> |
| 372 | (PathSegment { |
| 373 | ident: ident, |
| 374 | parameters: PathParameters::AngleBracketed( |
| 375 | AngleBracketedParameterData { |
| 376 | lifetimes: lifetimes, |
| 377 | types: types, |
| 378 | bindings: bindings, |
| 379 | } |
| 380 | ), |
| 381 | }) |
| 382 | ) |
| 383 | | |
| 384 | map!(word, PathSegment::ident) |
| 385 | )); |
| 386 | |
| 387 | named!(type_binding<&str, TypeBinding>, do_parse!( |
| 388 | ident: word >> |
| 389 | punct!("=") >> |
| 390 | ty: ty >> |
| 391 | (TypeBinding { |
| 392 | ident: ident, |
| 393 | ty: ty, |
| 394 | }) |
| 395 | )); |
| 396 | |
| 397 | named!(pub poly_trait_ref<&str, PolyTraitRef>, do_parse!( |
| 398 | bound_lifetimes: bound_lifetimes >> |
| 399 | trait_ref: path >> |
| 400 | (PolyTraitRef { |
| 401 | bound_lifetimes: bound_lifetimes, |
| 402 | trait_ref: trait_ref, |
| 403 | }) |
| 404 | )); |
| 405 | |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame^] | 406 | named!(fn_arg<&str, FnArg>, do_parse!( |
David Tolnay | f6ccb83 | 2016-09-04 15:00:56 -0700 | [diff] [blame] | 407 | pat: option!(terminated!(word, punct!(":"))) >> |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 408 | ty: ty >> |
David Tolnay | 66daf74 | 2016-09-07 08:21:49 -0700 | [diff] [blame^] | 409 | (FnArg { |
David Tolnay | 9d8f197 | 2016-09-04 11:58:48 -0700 | [diff] [blame] | 410 | pat: pat, |
| 411 | ty: ty, |
| 412 | }) |
| 413 | )); |
| 414 | } |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 415 | |
| 416 | #[cfg(feature = "printing")] |
| 417 | mod printing { |
| 418 | use super::*; |
| 419 | use quote::{Tokens, ToTokens}; |
| 420 | |
| 421 | impl ToTokens for Ty { |
| 422 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 423 | match *self { |
| 424 | Ty::Vec(ref inner) => { |
| 425 | tokens.append("["); |
| 426 | inner.to_tokens(tokens); |
| 427 | tokens.append("]"); |
| 428 | } |
| 429 | Ty::FixedLengthVec(ref inner, len) => { |
| 430 | tokens.append("["); |
| 431 | inner.to_tokens(tokens); |
| 432 | tokens.append(";"); |
| 433 | len.to_tokens(tokens); |
| 434 | tokens.append("]"); |
| 435 | } |
| 436 | Ty::Ptr(ref target) => { |
| 437 | tokens.append("*"); |
| 438 | match target.mutability { |
| 439 | Mutability::Mutable => tokens.append("mut"), |
| 440 | Mutability::Immutable => tokens.append("const"), |
| 441 | } |
| 442 | target.ty.to_tokens(tokens); |
| 443 | } |
| 444 | Ty::Rptr(ref lifetime, ref target) => { |
| 445 | tokens.append("&"); |
| 446 | lifetime.to_tokens(tokens); |
| 447 | if let Mutability::Mutable = target.mutability { |
| 448 | tokens.append("mut"); |
| 449 | } |
| 450 | target.ty.to_tokens(tokens); |
| 451 | } |
| 452 | Ty::BareFn(ref func) => { |
| 453 | func.to_tokens(tokens); |
| 454 | } |
| 455 | Ty::Never => { |
| 456 | tokens.append("!"); |
| 457 | } |
| 458 | Ty::Tup(ref elems) => { |
| 459 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 460 | tokens.append_separated(elems, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 461 | if elems.len() == 1 { |
| 462 | tokens.append(","); |
| 463 | } |
| 464 | tokens.append(")"); |
| 465 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 466 | Ty::Path(None, ref path) => { |
| 467 | path.to_tokens(tokens); |
| 468 | } |
| 469 | Ty::Path(Some(ref qself), ref path) => { |
| 470 | tokens.append("<"); |
| 471 | qself.ty.to_tokens(tokens); |
| 472 | if qself.position > 0 { |
| 473 | tokens.append("as"); |
| 474 | for (i, segment) in path.segments.iter() |
| 475 | .take(qself.position) |
| 476 | .enumerate() |
| 477 | { |
| 478 | if i > 0 || path.global { |
| 479 | tokens.append("::"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 480 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 481 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 482 | } |
David Tolnay | f69904a | 2016-09-04 14:46:07 -0700 | [diff] [blame] | 483 | } |
| 484 | tokens.append(">"); |
| 485 | for segment in path.segments.iter().skip(qself.position) { |
| 486 | tokens.append("::"); |
| 487 | segment.to_tokens(tokens); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | Ty::ObjectSum(_, _) => unimplemented!(), |
| 491 | Ty::PolyTraitRef(_) => unimplemented!(), |
| 492 | Ty::ImplTrait(ref bounds) => { |
| 493 | tokens.append("impl"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 494 | tokens.append_separated(bounds, "+"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 495 | } |
| 496 | Ty::Paren(ref inner) => { |
| 497 | tokens.append("("); |
| 498 | inner.to_tokens(tokens); |
| 499 | tokens.append(")"); |
| 500 | } |
| 501 | Ty::Infer => { |
| 502 | tokens.append("_"); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | impl ToTokens for Path { |
| 509 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 510 | for (i, segment) in self.segments.iter().enumerate() { |
| 511 | if i > 0 || self.global { |
| 512 | tokens.append("::"); |
| 513 | } |
| 514 | segment.to_tokens(tokens); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | impl ToTokens for PathSegment { |
| 520 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 521 | self.ident.to_tokens(tokens); |
| 522 | self.parameters.to_tokens(tokens); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | impl ToTokens for PathParameters { |
| 527 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 528 | match *self { |
| 529 | PathParameters::AngleBracketed(ref parameters) => { |
| 530 | parameters.to_tokens(tokens); |
| 531 | } |
| 532 | PathParameters::Parenthesized(ref parameters) => { |
| 533 | parameters.to_tokens(tokens); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | impl ToTokens for AngleBracketedParameterData { |
| 540 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 541 | let has_lifetimes = !self.lifetimes.is_empty(); |
| 542 | let has_types = !self.types.is_empty(); |
| 543 | let has_bindings = !self.bindings.is_empty(); |
| 544 | if !has_lifetimes && !has_types && !has_bindings { |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | tokens.append("<"); |
| 549 | |
| 550 | let mut first = true; |
| 551 | for lifetime in &self.lifetimes { |
| 552 | if !first { |
| 553 | tokens.append(","); |
| 554 | } |
| 555 | lifetime.to_tokens(tokens); |
| 556 | first = false; |
| 557 | } |
| 558 | for ty in &self.types { |
| 559 | if !first { |
| 560 | tokens.append(","); |
| 561 | } |
| 562 | ty.to_tokens(tokens); |
| 563 | first = false; |
| 564 | } |
| 565 | for binding in &self.bindings { |
| 566 | if !first { |
| 567 | tokens.append(","); |
| 568 | } |
| 569 | binding.to_tokens(tokens); |
| 570 | first = false; |
| 571 | } |
| 572 | |
| 573 | tokens.append(">"); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | impl ToTokens for TypeBinding { |
| 578 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 579 | self.ident.to_tokens(tokens); |
| 580 | tokens.append("="); |
| 581 | self.ty.to_tokens(tokens); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | impl ToTokens for ParenthesizedParameterData { |
| 586 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 587 | tokens.append("("); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 588 | tokens.append_separated(&self.inputs, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 589 | tokens.append(")"); |
| 590 | if let Some(ref output) = self.output { |
| 591 | tokens.append("->"); |
| 592 | output.to_tokens(tokens); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | impl ToTokens for PolyTraitRef { |
| 598 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 599 | if !self.bound_lifetimes.is_empty() { |
David Tolnay | e8796aa | 2016-09-04 14:48:22 -0700 | [diff] [blame] | 600 | tokens.append("for"); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 601 | tokens.append("<"); |
David Tolnay | 94ebdf9 | 2016-09-04 13:33:16 -0700 | [diff] [blame] | 602 | tokens.append_separated(&self.bound_lifetimes, ","); |
David Tolnay | 87d0b44 | 2016-09-04 11:52:12 -0700 | [diff] [blame] | 603 | tokens.append(">"); |
| 604 | } |
| 605 | self.trait_ref.to_tokens(tokens); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | impl ToTokens for BareFnTy { |
| 610 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 611 | tokens.append("fn"); |
| 612 | if !self.lifetimes.is_empty() { |
| 613 | tokens.append("<"); |
| 614 | for (i, lifetime) in self.lifetimes.iter().enumerate() { |
| 615 | if i > 0 { |
| 616 | tokens.append(","); |
| 617 | } |
| 618 | lifetime.to_tokens(tokens); |
| 619 | } |
| 620 | tokens.append(">"); |
| 621 | } |
| 622 | tokens.append("("); |
| 623 | tokens.append(")"); |
| 624 | } |
| 625 | } |
| 626 | } |