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> {
25 /// Return start value as a `Bound`
30 /// #![feature(collections)]
31 /// #![feature(collections_range)]
33 /// extern crate collections;
36 /// use collections::range::RangeArgument;
37 /// use collections::Bound::*;
39 /// assert_eq!((..10).start(), Unbounded);
40 /// assert_eq!((3..10).start(), Included(&3));
43 fn start(&self) -> Bound<&T>;
47 /// Return end value as a `Bound`
52 /// #![feature(collections)]
53 /// #![feature(collections_range)]
55 /// extern crate collections;
58 /// use collections::range::RangeArgument;
59 /// use collections::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> {
110 RangeInclusive::Empty{ ref at } => Included(at),
111 RangeInclusive::NonEmpty { ref start, .. } => Included(start),
114 fn end(&self) -> Bound<&T> {
116 RangeInclusive::Empty{ ref at } => Excluded(at),
117 RangeInclusive::NonEmpty { ref end, .. } => Included(end),
122 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
123 impl<T> RangeArgument<T> for RangeToInclusive<T> {
124 fn start(&self) -> Bound<&T> {
127 fn end(&self) -> Bound<&T> {
132 impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
133 fn start(&self) -> Bound<&T> {
135 (Included(ref start), _) => Included(start),
136 (Excluded(ref start), _) => Excluded(start),
137 (Unbounded, _) => Unbounded,
141 fn end(&self) -> Bound<&T> {
143 (_, Included(ref end)) => Included(end),
144 (_, Excluded(ref end)) => Excluded(end),
145 (_, Unbounded) => Unbounded,
150 impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
151 fn start(&self) -> Bound<&T> {
155 fn end(&self) -> Bound<&T> {