1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![unstable(feature = "collections_range",
12 reason = "waiting for dust to settle on inclusive ranges",
17 use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive, RangeToInclusive};
18 use Bound::{self, Excluded, Included, Unbounded};
20 /// `RangeArgument` is implemented by Rust's built-in range types, produced
21 /// by range syntax like `..`, `a..`, `..b` or `c..d`.
22 pub trait RangeArgument<T: ?Sized> {
23 /// Start index bound.
25 /// Returns the start value as a `Bound`.
30 /// #![feature(alloc)]
31 /// #![feature(collections_range)]
33 /// extern crate alloc;
36 /// use alloc::range::RangeArgument;
37 /// use alloc::Bound::*;
39 /// assert_eq!((..10).start(), Unbounded);
40 /// assert_eq!((3..10).start(), Included(&3));
43 fn start(&self) -> Bound<&T>;
47 /// Returns the end value as a `Bound`.
52 /// #![feature(alloc)]
53 /// #![feature(collections_range)]
55 /// extern crate alloc;
58 /// use alloc::range::RangeArgument;
59 /// use alloc::Bound::*;
61 /// assert_eq!((3..).end(), Unbounded);
62 /// assert_eq!((3..10).end(), Excluded(&10));
65 fn end(&self) -> Bound<&T>;
68 // FIXME add inclusive ranges to RangeArgument
70 impl<T: ?Sized> RangeArgument<T> for RangeFull {
71 fn start(&self) -> Bound<&T> {
74 fn end(&self) -> Bound<&T> {
79 impl<T> RangeArgument<T> for RangeFrom<T> {
80 fn start(&self) -> Bound<&T> {
83 fn end(&self) -> Bound<&T> {
88 impl<T> RangeArgument<T> for RangeTo<T> {
89 fn start(&self) -> Bound<&T> {
92 fn end(&self) -> Bound<&T> {
97 impl<T> RangeArgument<T> for Range<T> {
98 fn start(&self) -> Bound<&T> {
101 fn end(&self) -> Bound<&T> {
106 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
107 impl<T> RangeArgument<T> for RangeInclusive<T> {
108 fn start(&self) -> Bound<&T> {
109 Included(&self.start)
111 fn end(&self) -> Bound<&T> {
116 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
117 impl<T> RangeArgument<T> for RangeToInclusive<T> {
118 fn start(&self) -> Bound<&T> {
121 fn end(&self) -> Bound<&T> {
126 impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
127 fn start(&self) -> Bound<&T> {
129 (Included(ref start), _) => Included(start),
130 (Excluded(ref start), _) => Excluded(start),
131 (Unbounded, _) => Unbounded,
135 fn end(&self) -> Bound<&T> {
137 (_, Included(ref end)) => Included(end),
138 (_, Excluded(ref end)) => Excluded(end),
139 (_, Unbounded) => Unbounded,
144 impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
145 fn start(&self) -> Bound<&T> {
149 fn end(&self) -> Bound<&T> {