|
| 1 | +use collections::Bound::{self, Excluded, Included, Unbounded}; |
| 2 | + |
1 | 3 | /// An unbounded range. Use `..` (two dots) for its shorthand.
|
2 | 4 | ///
|
3 | 5 | /// Its primary use case is slicing index. It cannot serve as an iterator
|
@@ -352,3 +354,156 @@ impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
|
352 | 354 |
|
353 | 355 | // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
|
354 | 356 | // because underflow would be possible with (..0).into()
|
| 357 | + |
| 358 | +/// `RangeArgument` is implemented by Rust's built-in range types, produced |
| 359 | +/// by range syntax like `..`, `a..`, `..b` or `c..d`. |
| 360 | +#![unstable(feature = "collections_range", |
| 361 | + reason = "waiting for dust to settle on inclusive ranges", |
| 362 | + issue = "30877")] |
| 363 | +pub trait RangeArgument<T: ?Sized> { |
| 364 | + /// Start index bound. |
| 365 | + /// |
| 366 | + /// Returns the start value as a `Bound`. |
| 367 | + /// |
| 368 | + /// # Examples |
| 369 | + /// |
| 370 | + /// ``` |
| 371 | + /// #![feature(collections)] |
| 372 | + /// #![feature(collections_range)] |
| 373 | + /// |
| 374 | + /// extern crate collections; |
| 375 | + /// |
| 376 | + /// # fn main() { |
| 377 | + /// use collections::range::RangeArgument; |
| 378 | + /// use collections::Bound::*; |
| 379 | + /// |
| 380 | + /// assert_eq!((..10).start(), Unbounded); |
| 381 | + /// assert_eq!((3..10).start(), Included(&3)); |
| 382 | + /// # } |
| 383 | + /// ``` |
| 384 | + fn start(&self) -> Bound<&T>; |
| 385 | + |
| 386 | + /// End index bound. |
| 387 | + /// |
| 388 | + /// Returns the end value as a `Bound`. |
| 389 | + /// |
| 390 | + /// # Examples |
| 391 | + /// |
| 392 | + /// ``` |
| 393 | + /// #![feature(collections)] |
| 394 | + /// #![feature(collections_range)] |
| 395 | + /// |
| 396 | + /// extern crate collections; |
| 397 | + /// |
| 398 | + /// # fn main() { |
| 399 | + /// use collections::range::RangeArgument; |
| 400 | + /// use collections::Bound::*; |
| 401 | + /// |
| 402 | + /// assert_eq!((3..).end(), Unbounded); |
| 403 | + /// assert_eq!((3..10).end(), Excluded(&10)); |
| 404 | + /// # } |
| 405 | + /// ``` |
| 406 | + fn end(&self) -> Bound<&T>; |
| 407 | +} |
| 408 | + |
| 409 | +#![unstable(feature = "collections_range", |
| 410 | + reason = "waiting for dust to settle on inclusive ranges", |
| 411 | + issue = "30877")] |
| 412 | +impl<T: ?Sized> RangeArgument<T> for RangeFull { |
| 413 | + fn start(&self) -> Bound<&T> { |
| 414 | + Unbounded |
| 415 | + } |
| 416 | + fn end(&self) -> Bound<&T> { |
| 417 | + Unbounded |
| 418 | + } |
| 419 | +} |
| 420 | + |
| 421 | +#![unstable(feature = "collections_range", |
| 422 | + reason = "waiting for dust to settle on inclusive ranges", |
| 423 | + issue = "30877")] |
| 424 | +impl<T> RangeArgument<T> for RangeFrom<T> { |
| 425 | + fn start(&self) -> Bound<&T> { |
| 426 | + Included(&self.start) |
| 427 | + } |
| 428 | + fn end(&self) -> Bound<&T> { |
| 429 | + Unbounded |
| 430 | + } |
| 431 | +} |
| 432 | + |
| 433 | +#![unstable(feature = "collections_range", |
| 434 | + reason = "waiting for dust to settle on inclusive ranges", |
| 435 | + issue = "30877")] |
| 436 | +impl<T> RangeArgument<T> for RangeTo<T> { |
| 437 | + fn start(&self) -> Bound<&T> { |
| 438 | + Unbounded |
| 439 | + } |
| 440 | + fn end(&self) -> Bound<&T> { |
| 441 | + Excluded(&self.end) |
| 442 | + } |
| 443 | +} |
| 444 | + |
| 445 | +#![unstable(feature = "collections_range", |
| 446 | + reason = "waiting for dust to settle on inclusive ranges", |
| 447 | + issue = "30877")] |
| 448 | +impl<T> RangeArgument<T> for Range<T> { |
| 449 | + fn start(&self) -> Bound<&T> { |
| 450 | + Included(&self.start) |
| 451 | + } |
| 452 | + fn end(&self) -> Bound<&T> { |
| 453 | + Excluded(&self.end) |
| 454 | + } |
| 455 | +} |
| 456 | + |
| 457 | +#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
| 458 | +impl<T> RangeArgument<T> for RangeInclusive<T> { |
| 459 | + fn start(&self) -> Bound<&T> { |
| 460 | + Included(&self.start) |
| 461 | + } |
| 462 | + fn end(&self) -> Bound<&T> { |
| 463 | + Included(&self.end) |
| 464 | + } |
| 465 | +} |
| 466 | + |
| 467 | +#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] |
| 468 | +impl<T> RangeArgument<T> for RangeToInclusive<T> { |
| 469 | + fn start(&self) -> Bound<&T> { |
| 470 | + Unbounded |
| 471 | + } |
| 472 | + fn end(&self) -> Bound<&T> { |
| 473 | + Included(&self.end) |
| 474 | + } |
| 475 | +} |
| 476 | + |
| 477 | +#![unstable(feature = "collections_range", |
| 478 | + reason = "waiting for dust to settle on inclusive ranges", |
| 479 | + issue = "30877")] |
| 480 | +impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) { |
| 481 | + fn start(&self) -> Bound<&T> { |
| 482 | + match *self { |
| 483 | + (Included(ref start), _) => Included(start), |
| 484 | + (Excluded(ref start), _) => Excluded(start), |
| 485 | + (Unbounded, _) => Unbounded, |
| 486 | + } |
| 487 | + } |
| 488 | + |
| 489 | + fn end(&self) -> Bound<&T> { |
| 490 | + match *self { |
| 491 | + (_, Included(ref end)) => Included(end), |
| 492 | + (_, Excluded(ref end)) => Excluded(end), |
| 493 | + (_, Unbounded) => Unbounded, |
| 494 | + } |
| 495 | + } |
| 496 | +} |
| 497 | + |
| 498 | +#![unstable(feature = "collections_range", |
| 499 | + reason = "waiting for dust to settle on inclusive ranges", |
| 500 | + issue = "30877")] |
| 501 | +impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) { |
| 502 | + fn start(&self) -> Bound<&T> { |
| 503 | + self.0 |
| 504 | + } |
| 505 | + |
| 506 | + fn end(&self) -> Bound<&T> { |
| 507 | + self.1 |
| 508 | + } |
| 509 | +} |
0 commit comments