David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 1 | use {Generics, Lifetime, MutTy, Mutability, Path, QSelf, Ty, TyParamBound}; |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 2 | use aster::ident::ToIdent; |
| 3 | use aster::invoke::{Invoke, Identity}; |
| 4 | use aster::lifetime::IntoLifetime; |
| 5 | use aster::path::PathBuilder; |
| 6 | use aster::qpath::QPathBuilder; |
| 7 | use aster::ty_param::TyParamBoundBuilder; |
| 8 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 9 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 10 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 11 | pub struct TyBuilder<F = Identity> { |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 12 | callback: F, |
| 13 | } |
| 14 | |
| 15 | impl TyBuilder { |
| 16 | pub fn new() -> Self { |
| 17 | TyBuilder::with_callback(Identity) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | impl<F> TyBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 22 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 23 | { |
| 24 | pub fn with_callback(callback: F) -> Self { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 25 | TyBuilder { callback: callback } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | pub fn build(self, ty: Ty) -> F::Result { |
| 29 | self.callback.invoke(ty) |
| 30 | } |
| 31 | |
| 32 | pub fn id<I>(self, id: I) -> F::Result |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 33 | where I: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 34 | { |
| 35 | self.path().id(id).build() |
| 36 | } |
| 37 | |
| 38 | pub fn build_path(self, path: Path) -> F::Result { |
| 39 | self.build(Ty::Path(None, path)) |
| 40 | } |
| 41 | |
| 42 | pub fn build_qpath(self, qself: QSelf, path: Path) -> F::Result { |
| 43 | self.build(Ty::Path(Some(qself), path)) |
| 44 | } |
| 45 | |
| 46 | pub fn path(self) -> PathBuilder<TyPathBuilder<F>> { |
| 47 | PathBuilder::with_callback(TyPathBuilder(self)) |
| 48 | } |
| 49 | |
| 50 | pub fn qpath(self) -> QPathBuilder<TyQPathBuilder<F>> { |
| 51 | QPathBuilder::with_callback(TyQPathBuilder(self)) |
| 52 | } |
| 53 | |
| 54 | pub fn isize(self) -> F::Result { |
| 55 | self.id("isize") |
| 56 | } |
| 57 | |
| 58 | pub fn i8(self) -> F::Result { |
| 59 | self.id("i8") |
| 60 | } |
| 61 | |
| 62 | pub fn i16(self) -> F::Result { |
| 63 | self.id("i16") |
| 64 | } |
| 65 | |
| 66 | pub fn i32(self) -> F::Result { |
| 67 | self.id("i32") |
| 68 | } |
| 69 | |
| 70 | pub fn i64(self) -> F::Result { |
| 71 | self.id("i64") |
| 72 | } |
| 73 | |
| 74 | pub fn usize(self) -> F::Result { |
| 75 | self.id("usize") |
| 76 | } |
| 77 | |
| 78 | pub fn u8(self) -> F::Result { |
| 79 | self.id("u8") |
| 80 | } |
| 81 | |
| 82 | pub fn u16(self) -> F::Result { |
| 83 | self.id("u16") |
| 84 | } |
| 85 | |
| 86 | pub fn u32(self) -> F::Result { |
| 87 | self.id("u32") |
| 88 | } |
| 89 | |
| 90 | pub fn u64(self) -> F::Result { |
| 91 | self.id("u64") |
| 92 | } |
| 93 | |
| 94 | pub fn f32(self) -> F::Result { |
| 95 | self.id("f32") |
| 96 | } |
| 97 | |
| 98 | pub fn f64(self) -> F::Result { |
| 99 | self.id("f64") |
| 100 | } |
| 101 | |
| 102 | pub fn bool(self) -> F::Result { |
| 103 | self.id("bool") |
| 104 | } |
| 105 | |
| 106 | pub fn unit(self) -> F::Result { |
| 107 | self.tuple().build() |
| 108 | } |
| 109 | |
| 110 | pub fn tuple(self) -> TyTupleBuilder<F> { |
| 111 | TyTupleBuilder { |
| 112 | builder: self, |
| 113 | tys: vec![], |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | pub fn build_slice(self, ty: Ty) -> F::Result { |
David Tolnay | 429168f | 2016-10-05 23:41:04 -0700 | [diff] [blame] | 118 | self.build(Ty::Slice(Box::new(ty))) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | pub fn slice(self) -> TyBuilder<TySliceBuilder<F>> { |
| 122 | TyBuilder::with_callback(TySliceBuilder(self)) |
| 123 | } |
| 124 | |
| 125 | pub fn ref_(self) -> TyRefBuilder<F> { |
| 126 | TyRefBuilder { |
| 127 | builder: self, |
| 128 | lifetime: None, |
| 129 | mutability: Mutability::Immutable, |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub fn never(self) -> F::Result { |
| 134 | self.build(Ty::Never) |
| 135 | } |
| 136 | |
| 137 | pub fn infer(self) -> F::Result { |
| 138 | self.build(Ty::Infer) |
| 139 | } |
| 140 | |
| 141 | pub fn option(self) -> TyBuilder<TyOptionBuilder<F>> { |
| 142 | TyBuilder::with_callback(TyOptionBuilder(self)) |
| 143 | } |
| 144 | |
| 145 | pub fn result(self) -> TyBuilder<TyResultOkBuilder<F>> { |
| 146 | TyBuilder::with_callback(TyResultOkBuilder(self)) |
| 147 | } |
| 148 | |
| 149 | pub fn phantom_data(self) -> TyBuilder<TyPhantomDataBuilder<F>> { |
| 150 | TyBuilder::with_callback(TyPhantomDataBuilder(self)) |
| 151 | } |
| 152 | |
| 153 | pub fn box_(self) -> TyBuilder<TyBoxBuilder<F>> { |
| 154 | TyBuilder::with_callback(TyBoxBuilder(self)) |
| 155 | } |
| 156 | |
| 157 | pub fn iterator(self) -> TyBuilder<TyIteratorBuilder<F>> { |
| 158 | TyBuilder::with_callback(TyIteratorBuilder(self)) |
| 159 | } |
| 160 | |
| 161 | pub fn object_sum(self) -> TyBuilder<TyObjectSumBuilder<F>> { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 162 | TyBuilder::with_callback(TyObjectSumBuilder { builder: self }) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | pub fn impl_trait(self) -> TyImplTraitTyBuilder<F> { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 166 | TyImplTraitTyBuilder { |
| 167 | builder: self, |
| 168 | bounds: Vec::new(), |
| 169 | } |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 173 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 174 | |
| 175 | pub struct TyPathBuilder<F>(TyBuilder<F>); |
| 176 | |
| 177 | impl<F> Invoke<Path> for TyPathBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 178 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 179 | { |
| 180 | type Result = F::Result; |
| 181 | |
| 182 | fn invoke(self, path: Path) -> F::Result { |
| 183 | self.0.build_path(path) |
| 184 | } |
| 185 | } |
| 186 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 187 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 188 | |
| 189 | pub struct TyQPathBuilder<F>(TyBuilder<F>); |
| 190 | |
| 191 | impl<F> Invoke<(QSelf, Path)> for TyQPathBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 192 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 193 | { |
| 194 | type Result = F::Result; |
| 195 | |
| 196 | fn invoke(self, (qself, path): (QSelf, Path)) -> F::Result { |
| 197 | self.0.build_qpath(qself, path) |
| 198 | } |
| 199 | } |
| 200 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 201 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 202 | |
| 203 | pub struct TySliceBuilder<F>(TyBuilder<F>); |
| 204 | |
| 205 | impl<F> Invoke<Ty> for TySliceBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 206 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 207 | { |
| 208 | type Result = F::Result; |
| 209 | |
| 210 | fn invoke(self, ty: Ty) -> F::Result { |
| 211 | self.0.build_slice(ty) |
| 212 | } |
| 213 | } |
| 214 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 215 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 216 | |
| 217 | pub struct TyRefBuilder<F> { |
| 218 | builder: TyBuilder<F>, |
| 219 | lifetime: Option<Lifetime>, |
| 220 | mutability: Mutability, |
| 221 | } |
| 222 | |
| 223 | impl<F> TyRefBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 224 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 225 | { |
| 226 | pub fn mut_(mut self) -> Self { |
| 227 | self.mutability = Mutability::Mutable; |
| 228 | self |
| 229 | } |
| 230 | |
| 231 | pub fn lifetime<N>(mut self, name: N) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 232 | where N: ToIdent |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 233 | { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 234 | self.lifetime = Some(Lifetime { ident: name.to_ident() }); |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 235 | self |
| 236 | } |
| 237 | |
| 238 | pub fn build_ty(self, ty: Ty) -> F::Result { |
| 239 | let ty = MutTy { |
| 240 | ty: ty, |
| 241 | mutability: self.mutability, |
| 242 | }; |
| 243 | self.builder.build(Ty::Rptr(self.lifetime, Box::new(ty))) |
| 244 | } |
| 245 | |
| 246 | pub fn ty(self) -> TyBuilder<Self> { |
| 247 | TyBuilder::with_callback(self) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | impl<F> Invoke<Ty> for TyRefBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 252 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 253 | { |
| 254 | type Result = F::Result; |
| 255 | |
| 256 | fn invoke(self, ty: Ty) -> F::Result { |
| 257 | self.build_ty(ty) |
| 258 | } |
| 259 | } |
| 260 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 261 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 262 | |
| 263 | pub struct TyOptionBuilder<F>(TyBuilder<F>); |
| 264 | |
| 265 | impl<F> Invoke<Ty> for TyOptionBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 266 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 267 | { |
| 268 | type Result = F::Result; |
| 269 | |
| 270 | fn invoke(self, ty: Ty) -> F::Result { |
| 271 | let path = PathBuilder::new() |
| 272 | .global() |
| 273 | .id("std") |
| 274 | .id("option") |
| 275 | .segment("Option") |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 276 | .with_ty(ty) |
| 277 | .build() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 278 | .build(); |
| 279 | |
| 280 | self.0.build_path(path) |
| 281 | } |
| 282 | } |
| 283 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 284 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 285 | |
| 286 | pub struct TyResultOkBuilder<F>(TyBuilder<F>); |
| 287 | |
| 288 | impl<F> Invoke<Ty> for TyResultOkBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 289 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 290 | { |
| 291 | type Result = TyBuilder<TyResultErrBuilder<F>>; |
| 292 | |
| 293 | fn invoke(self, ty: Ty) -> TyBuilder<TyResultErrBuilder<F>> { |
| 294 | TyBuilder::with_callback(TyResultErrBuilder(self.0, ty)) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | pub struct TyResultErrBuilder<F>(TyBuilder<F>, Ty); |
| 299 | |
| 300 | impl<F> Invoke<Ty> for TyResultErrBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 301 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 302 | { |
| 303 | type Result = F::Result; |
| 304 | |
| 305 | fn invoke(self, ty: Ty) -> F::Result { |
| 306 | let path = PathBuilder::new() |
| 307 | .global() |
| 308 | .id("std") |
| 309 | .id("result") |
| 310 | .segment("Result") |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 311 | .with_ty(self.1) |
| 312 | .with_ty(ty) |
| 313 | .build() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 314 | .build(); |
| 315 | |
| 316 | self.0.build_path(path) |
| 317 | } |
| 318 | } |
| 319 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 320 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 321 | |
| 322 | pub struct TyPhantomDataBuilder<F>(TyBuilder<F>); |
| 323 | |
| 324 | impl<F> Invoke<Ty> for TyPhantomDataBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 325 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 326 | { |
| 327 | type Result = F::Result; |
| 328 | |
| 329 | fn invoke(self, ty: Ty) -> F::Result { |
| 330 | let path = PathBuilder::new() |
| 331 | .global() |
| 332 | .id("std") |
| 333 | .id("marker") |
| 334 | .segment("PhantomData") |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 335 | .with_ty(ty) |
| 336 | .build() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 337 | .build(); |
| 338 | |
| 339 | self.0.build_path(path) |
| 340 | } |
| 341 | } |
| 342 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 343 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 344 | |
| 345 | pub struct TyBoxBuilder<F>(TyBuilder<F>); |
| 346 | |
| 347 | impl<F> Invoke<Ty> for TyBoxBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 348 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 349 | { |
| 350 | type Result = F::Result; |
| 351 | |
| 352 | fn invoke(self, ty: Ty) -> F::Result { |
| 353 | let path = PathBuilder::new() |
| 354 | .global() |
| 355 | .id("std") |
| 356 | .id("boxed") |
| 357 | .segment("Box") |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 358 | .with_ty(ty) |
| 359 | .build() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 360 | .build(); |
| 361 | |
| 362 | self.0.build_path(path) |
| 363 | } |
| 364 | } |
| 365 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 366 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 367 | |
| 368 | pub struct TyIteratorBuilder<F>(TyBuilder<F>); |
| 369 | |
| 370 | impl<F> Invoke<Ty> for TyIteratorBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 371 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 372 | { |
| 373 | type Result = F::Result; |
| 374 | |
| 375 | fn invoke(self, ty: Ty) -> F::Result { |
| 376 | let path = PathBuilder::new() |
| 377 | .global() |
| 378 | .id("std") |
| 379 | .id("iter") |
| 380 | .segment("Iterator") |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 381 | .binding("Item") |
| 382 | .build(ty.clone()) |
| 383 | .build() |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 384 | .build(); |
| 385 | |
| 386 | self.0.build_path(path) |
| 387 | } |
| 388 | } |
| 389 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 390 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 391 | |
| 392 | pub struct TyObjectSumBuilder<F> { |
| 393 | builder: TyBuilder<F>, |
| 394 | } |
| 395 | |
| 396 | impl<F> Invoke<Ty> for TyObjectSumBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 397 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 398 | { |
| 399 | type Result = TyObjectSumTyBuilder<F>; |
| 400 | |
| 401 | fn invoke(self, ty: Ty) -> Self::Result { |
| 402 | TyObjectSumTyBuilder { |
| 403 | builder: self.builder, |
| 404 | ty: ty, |
| 405 | bounds: Vec::new(), |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | pub struct TyObjectSumTyBuilder<F> { |
| 411 | builder: TyBuilder<F>, |
| 412 | ty: Ty, |
| 413 | bounds: Vec<TyParamBound>, |
| 414 | } |
| 415 | |
| 416 | impl<F> TyObjectSumTyBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 417 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 418 | { |
| 419 | pub fn with_bounds<I>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 420 | where I: Iterator<Item = TyParamBound> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 421 | { |
| 422 | self.bounds.extend(iter); |
| 423 | self |
| 424 | } |
| 425 | |
| 426 | pub fn with_bound(mut self, bound: TyParamBound) -> Self { |
| 427 | self.bounds.push(bound); |
| 428 | self |
| 429 | } |
| 430 | |
| 431 | pub fn bound(self) -> TyParamBoundBuilder<Self> { |
| 432 | TyParamBoundBuilder::with_callback(self) |
| 433 | } |
| 434 | |
| 435 | pub fn with_generics(self, generics: Generics) -> Self { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 436 | self.with_lifetimes(generics.lifetimes |
| 437 | .into_iter() |
| 438 | .map(|def| def.lifetime)) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | pub fn with_lifetimes<I, L>(mut self, lifetimes: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 442 | where I: Iterator<Item = L>, |
| 443 | L: IntoLifetime |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 444 | { |
| 445 | for lifetime in lifetimes { |
| 446 | self = self.lifetime(lifetime); |
| 447 | } |
| 448 | |
| 449 | self |
| 450 | } |
| 451 | |
| 452 | pub fn lifetime<L>(self, lifetime: L) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 453 | where L: IntoLifetime |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 454 | { |
| 455 | self.bound().lifetime(lifetime) |
| 456 | } |
| 457 | |
| 458 | pub fn build(self) -> F::Result { |
| 459 | let bounds = self.bounds; |
| 460 | self.builder.build(Ty::ObjectSum(Box::new(self.ty), bounds)) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | impl<F> Invoke<TyParamBound> for TyObjectSumTyBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 465 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 466 | { |
| 467 | type Result = Self; |
| 468 | |
| 469 | fn invoke(self, bound: TyParamBound) -> Self { |
| 470 | self.with_bound(bound) |
| 471 | } |
| 472 | } |
| 473 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 474 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 475 | |
| 476 | pub struct TyImplTraitTyBuilder<F> { |
| 477 | builder: TyBuilder<F>, |
| 478 | bounds: Vec<TyParamBound>, |
| 479 | } |
| 480 | |
| 481 | impl<F> TyImplTraitTyBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 482 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 483 | { |
| 484 | pub fn with_bounds<I>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 485 | where I: Iterator<Item = TyParamBound> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 486 | { |
| 487 | self.bounds.extend(iter); |
| 488 | self |
| 489 | } |
| 490 | |
| 491 | pub fn with_bound(mut self, bound: TyParamBound) -> Self { |
| 492 | self.bounds.push(bound); |
| 493 | self |
| 494 | } |
| 495 | |
| 496 | pub fn bound(self) -> TyParamBoundBuilder<Self> { |
| 497 | TyParamBoundBuilder::with_callback(self) |
| 498 | } |
| 499 | |
| 500 | pub fn with_generics(self, generics: Generics) -> Self { |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 501 | self.with_lifetimes(generics.lifetimes |
| 502 | .into_iter() |
| 503 | .map(|def| def.lifetime)) |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | pub fn with_lifetimes<I, L>(mut self, lifetimes: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 507 | where I: Iterator<Item = L>, |
| 508 | L: IntoLifetime |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 509 | { |
| 510 | for lifetime in lifetimes { |
| 511 | self = self.lifetime(lifetime); |
| 512 | } |
| 513 | |
| 514 | self |
| 515 | } |
| 516 | |
| 517 | pub fn lifetime<L>(self, lifetime: L) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 518 | where L: IntoLifetime |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 519 | { |
| 520 | self.bound().lifetime(lifetime) |
| 521 | } |
| 522 | |
| 523 | pub fn build(self) -> F::Result { |
| 524 | let bounds = self.bounds; |
| 525 | self.builder.build(Ty::ImplTrait(bounds)) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | impl<F> Invoke<TyParamBound> for TyImplTraitTyBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 530 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 531 | { |
| 532 | type Result = Self; |
| 533 | |
| 534 | fn invoke(self, bound: TyParamBound) -> Self { |
| 535 | self.with_bound(bound) |
| 536 | } |
| 537 | } |
| 538 | |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 539 | // //////////////////////////////////////////////////////////////////////////// |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 540 | |
| 541 | pub struct TyTupleBuilder<F> { |
| 542 | builder: TyBuilder<F>, |
| 543 | tys: Vec<Ty>, |
| 544 | } |
| 545 | |
| 546 | impl<F> TyTupleBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 547 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 548 | { |
| 549 | pub fn with_tys<I>(mut self, iter: I) -> Self |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 550 | where I: IntoIterator<Item = Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 551 | { |
| 552 | self.tys.extend(iter); |
| 553 | self |
| 554 | } |
| 555 | |
| 556 | pub fn with_ty(mut self, ty: Ty) -> Self { |
| 557 | self.tys.push(ty); |
| 558 | self |
| 559 | } |
| 560 | |
| 561 | pub fn ty(self) -> TyBuilder<Self> { |
| 562 | TyBuilder::with_callback(self) |
| 563 | } |
| 564 | |
| 565 | pub fn build(self) -> F::Result { |
| 566 | self.builder.build(Ty::Tup(self.tys)) |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | impl<F> Invoke<Ty> for TyTupleBuilder<F> |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 571 | where F: Invoke<Ty> |
David Tolnay | 5533772 | 2016-09-11 12:58:56 -0700 | [diff] [blame] | 572 | { |
| 573 | type Result = Self; |
| 574 | |
| 575 | fn invoke(self, ty: Ty) -> Self { |
| 576 | self.with_ty(ty) |
| 577 | } |
| 578 | } |