]> git.lizzy.rs Git - rust.git/blob - src/libcollections/range.rs
Clarify docs in `VecDeque::resize`
[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     /// #![feature(collections_bound)]
33     ///
34     /// extern crate collections;
35     ///
36     /// # fn main() {
37     /// use collections::range::RangeArgument;
38     /// use collections::Bound::*;
39     ///
40     /// assert_eq!((..10).start(), Unbounded);
41     /// assert_eq!((3..10).start(), Included(&3));
42     /// # }
43     /// ```
44     fn start(&self) -> Bound<&T>;
45
46     /// End index bound
47     ///
48     /// Return end value as a `Bound`
49     ///
50     /// # Examples
51     ///
52     /// ```
53     /// #![feature(collections)]
54     /// #![feature(collections_range)]
55     /// #![feature(collections_bound)]
56     ///
57     /// extern crate collections;
58     ///
59     /// # fn main() {
60     /// use collections::range::RangeArgument;
61     /// use collections::Bound::*;
62     ///
63     /// assert_eq!((3..).end(), Unbounded);
64     /// assert_eq!((3..10).end(), Excluded(&10));
65     /// # }
66     /// ```
67     fn end(&self) -> Bound<&T>;
68 }
69
70 // FIXME add inclusive ranges to RangeArgument
71
72 impl<T: ?Sized> RangeArgument<T> for RangeFull {
73     fn start(&self) -> Bound<&T> {
74         Unbounded
75     }
76     fn end(&self) -> Bound<&T> {
77         Unbounded
78     }
79 }
80
81 impl<T> RangeArgument<T> for RangeFrom<T> {
82     fn start(&self) -> Bound<&T> {
83         Included(&self.start)
84     }
85     fn end(&self) -> Bound<&T> {
86         Unbounded
87     }
88 }
89
90 impl<T> RangeArgument<T> for RangeTo<T> {
91     fn start(&self) -> Bound<&T> {
92         Unbounded
93     }
94     fn end(&self) -> Bound<&T> {
95         Excluded(&self.end)
96     }
97 }
98
99 impl<T> RangeArgument<T> for Range<T> {
100     fn start(&self) -> Bound<&T> {
101         Included(&self.start)
102     }
103     fn end(&self) -> Bound<&T> {
104         Excluded(&self.end)
105     }
106 }
107
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> {
111         match *self {
112             RangeInclusive::Empty{ ref at }            => Included(at),
113             RangeInclusive::NonEmpty { ref start, .. } => Included(start),
114         }
115     }
116     fn end(&self) -> Bound<&T> {
117         match *self {
118             RangeInclusive::Empty{ ref at }            => Excluded(at),
119             RangeInclusive::NonEmpty { ref end, .. }   => Included(end),
120         }
121     }
122 }
123
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> {
127         Unbounded
128     }
129     fn end(&self) -> Bound<&T> {
130         Included(&self.end)
131     }
132 }
133
134 impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
135     fn start(&self) -> Bound<&T> {
136         match *self {
137             (Included(ref start), _) => Included(start),
138             (Excluded(ref start), _) => Excluded(start),
139             (Unbounded, _)           => Unbounded,
140         }
141     }
142
143     fn end(&self) -> Bound<&T> {
144         match *self {
145             (_, Included(ref end)) => Included(end),
146             (_, Excluded(ref end)) => Excluded(end),
147             (_, Unbounded)         => Unbounded,
148         }
149     }
150 }
151
152 impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
153     fn start(&self) -> Bound<&T> {
154         self.0
155     }
156
157     fn end(&self) -> Bound<&T> {
158         self.1
159     }
160 }