]> git.lizzy.rs Git - rust.git/blob - src/libcollections/range.rs
Rollup merge of #40521 - TimNN:panic-free-shift, r=alexcrichton
[rust.git] / src / libcollections / range.rs
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.
4 //
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.
10
11 #![unstable(feature = "collections_range",
12             reason = "waiting for dust to settle on inclusive ranges",
13             issue = "30877")]
14
15 //! Range syntax.
16
17 use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive, RangeToInclusive};
18 use Bound::{self, Excluded, Included, Unbounded};
19
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
24     ///
25     /// Return start value as a `Bound`
26     ///
27     /// # Examples
28     ///
29     /// ```
30     /// #![feature(collections)]
31     /// #![feature(collections_range)]
32     ///
33     /// extern crate collections;
34     ///
35     /// # fn main() {
36     /// use collections::range::RangeArgument;
37     /// use collections::Bound::*;
38     ///
39     /// assert_eq!((..10).start(), Unbounded);
40     /// assert_eq!((3..10).start(), Included(&3));
41     /// # }
42     /// ```
43     fn start(&self) -> Bound<&T>;
44
45     /// End index bound
46     ///
47     /// Return end value as a `Bound`
48     ///
49     /// # Examples
50     ///
51     /// ```
52     /// #![feature(collections)]
53     /// #![feature(collections_range)]
54     ///
55     /// extern crate collections;
56     ///
57     /// # fn main() {
58     /// use collections::range::RangeArgument;
59     /// use collections::Bound::*;
60     ///
61     /// assert_eq!((3..).end(), Unbounded);
62     /// assert_eq!((3..10).end(), Excluded(&10));
63     /// # }
64     /// ```
65     fn end(&self) -> Bound<&T>;
66 }
67
68 // FIXME add inclusive ranges to RangeArgument
69
70 impl<T: ?Sized> RangeArgument<T> for RangeFull {
71     fn start(&self) -> Bound<&T> {
72         Unbounded
73     }
74     fn end(&self) -> Bound<&T> {
75         Unbounded
76     }
77 }
78
79 impl<T> RangeArgument<T> for RangeFrom<T> {
80     fn start(&self) -> Bound<&T> {
81         Included(&self.start)
82     }
83     fn end(&self) -> Bound<&T> {
84         Unbounded
85     }
86 }
87
88 impl<T> RangeArgument<T> for RangeTo<T> {
89     fn start(&self) -> Bound<&T> {
90         Unbounded
91     }
92     fn end(&self) -> Bound<&T> {
93         Excluded(&self.end)
94     }
95 }
96
97 impl<T> RangeArgument<T> for Range<T> {
98     fn start(&self) -> Bound<&T> {
99         Included(&self.start)
100     }
101     fn end(&self) -> Bound<&T> {
102         Excluded(&self.end)
103     }
104 }
105
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         match *self {
110             RangeInclusive::Empty{ ref at }            => Included(at),
111             RangeInclusive::NonEmpty { ref start, .. } => Included(start),
112         }
113     }
114     fn end(&self) -> Bound<&T> {
115         match *self {
116             RangeInclusive::Empty{ ref at }            => Excluded(at),
117             RangeInclusive::NonEmpty { ref end, .. }   => Included(end),
118         }
119     }
120 }
121
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> {
125         Unbounded
126     }
127     fn end(&self) -> Bound<&T> {
128         Included(&self.end)
129     }
130 }
131
132 impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
133     fn start(&self) -> Bound<&T> {
134         match *self {
135             (Included(ref start), _) => Included(start),
136             (Excluded(ref start), _) => Excluded(start),
137             (Unbounded, _)           => Unbounded,
138         }
139     }
140
141     fn end(&self) -> Bound<&T> {
142         match *self {
143             (_, Included(ref end)) => Included(end),
144             (_, Excluded(ref end)) => Excluded(end),
145             (_, Unbounded)         => Unbounded,
146         }
147     }
148 }
149
150 impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
151     fn start(&self) -> Bound<&T> {
152         self.0
153     }
154
155     fn end(&self) -> Bound<&T> {
156         self.1
157     }
158 }