]> git.lizzy.rs Git - rust.git/blob - src/liballoc/range.rs
Rollup merge of #45099 - mikeyhew:fix-astconv-self-type-comments, r=nikomatsakis
[rust.git] / src / liballoc / 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     /// Returns the start value as a `Bound`.
26     ///
27     /// # Examples
28     ///
29     /// ```
30     /// #![feature(alloc)]
31     /// #![feature(collections_range)]
32     ///
33     /// extern crate alloc;
34     ///
35     /// # fn main() {
36     /// use alloc::range::RangeArgument;
37     /// use alloc::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     /// Returns the end value as a `Bound`.
48     ///
49     /// # Examples
50     ///
51     /// ```
52     /// #![feature(alloc)]
53     /// #![feature(collections_range)]
54     ///
55     /// extern crate alloc;
56     ///
57     /// # fn main() {
58     /// use alloc::range::RangeArgument;
59     /// use alloc::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         Included(&self.start)
110     }
111     fn end(&self) -> Bound<&T> {
112         Included(&self.end)
113     }
114 }
115
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> {
119         Unbounded
120     }
121     fn end(&self) -> Bound<&T> {
122         Included(&self.end)
123     }
124 }
125
126 impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
127     fn start(&self) -> Bound<&T> {
128         match *self {
129             (Included(ref start), _) => Included(start),
130             (Excluded(ref start), _) => Excluded(start),
131             (Unbounded, _)           => Unbounded,
132         }
133     }
134
135     fn end(&self) -> Bound<&T> {
136         match *self {
137             (_, Included(ref end)) => Included(end),
138             (_, Excluded(ref end)) => Excluded(end),
139             (_, Unbounded)         => Unbounded,
140         }
141     }
142 }
143
144 impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
145     fn start(&self) -> Bound<&T> {
146         self.0
147     }
148
149     fn end(&self) -> Bound<&T> {
150         self.1
151     }
152 }