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)]
32 /// #![feature(collections_bound)]
34 /// extern crate collections;
37 /// use collections::range::RangeArgument;
38 /// use collections::Bound::*;
40 /// assert_eq!((..10).start(), Unbounded);
41 /// assert_eq!((3..10).start(), Included(&3));
44 fn start(&self) -> Bound<&T>;
48 /// Return end value as a `Bound`
53 /// #![feature(collections)]
54 /// #![feature(collections_range)]
55 /// #![feature(collections_bound)]
57 /// extern crate collections;
60 /// use collections::range::RangeArgument;
61 /// use collections::Bound::*;
63 /// assert_eq!((3..).end(), Unbounded);
64 /// assert_eq!((3..10).end(), Excluded(&10));
67 fn end(&self) -> Bound<&T>;
70 // FIXME add inclusive ranges to RangeArgument
72 impl<T: ?Sized> RangeArgument<T> for RangeFull {
73 fn start(&self) -> Bound<&T> {
76 fn end(&self) -> Bound<&T> {
81 impl<T> RangeArgument<T> for RangeFrom<T> {
82 fn start(&self) -> Bound<&T> {
85 fn end(&self) -> Bound<&T> {
90 impl<T> RangeArgument<T> for RangeTo<T> {
91 fn start(&self) -> Bound<&T> {
94 fn end(&self) -> Bound<&T> {
99 impl<T> RangeArgument<T> for Range<T> {
100 fn start(&self) -> Bound<&T> {
101 Included(&self.start)
103 fn end(&self) -> Bound<&T> {
108 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
109 impl<T> RangeArgument<T> for RangeInclusive<T> {
110 fn start(&self) -> Bound<&T> {
112 RangeInclusive::Empty{ ref at } => Included(at),
113 RangeInclusive::NonEmpty { ref start, .. } => Included(start),
116 fn end(&self) -> Bound<&T> {
118 RangeInclusive::Empty{ ref at } => Excluded(at),
119 RangeInclusive::NonEmpty { ref end, .. } => Included(end),
124 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
125 impl<T> RangeArgument<T> for RangeToInclusive<T> {
126 fn start(&self) -> Bound<&T> {
129 fn end(&self) -> Bound<&T> {
134 impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
135 fn start(&self) -> Bound<&T> {
137 (Included(ref start), _) => Included(start),
138 (Excluded(ref start), _) => Excluded(start),
139 (Unbounded, _) => Unbounded,
143 fn end(&self) -> Bound<&T> {
145 (_, Included(ref end)) => Included(end),
146 (_, Excluded(ref end)) => Excluded(end),
147 (_, Unbounded) => Unbounded,
152 impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
153 fn start(&self) -> Bound<&T> {
157 fn end(&self) -> Bound<&T> {