]>
Commit | Line | Data |
---|---|---|
d9579d0f AL |
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. | |
62682a34 | 10 | |
9cc50fc6 SL |
11 | #![unstable(feature = "collections_range", |
12 | reason = "waiting for dust to settle on inclusive ranges", | |
13 | issue = "30877")] | |
d9579d0f AL |
14 | |
15 | //! Range syntax. | |
16 | ||
d9579d0f AL |
17 | use core::ops::{RangeFull, Range, RangeTo, RangeFrom}; |
18 | ||
19 | /// **RangeArgument** is implemented by Rust's built-in range types, produced | |
20 | /// by range syntax like `..`, `a..`, `..b` or `c..d`. | |
21 | pub trait RangeArgument<T> { | |
22 | /// Start index (inclusive) | |
23 | /// | |
24 | /// Return start value if present, else `None`. | |
5bcae85e SL |
25 | /// |
26 | /// # Examples | |
27 | /// | |
28 | /// ``` | |
29 | /// #![feature(collections)] | |
30 | /// #![feature(collections_range)] | |
31 | /// | |
32 | /// extern crate collections; | |
33 | /// | |
34 | /// # fn main() { | |
35 | /// use collections::range::RangeArgument; | |
36 | /// | |
37 | /// assert_eq!((..10).start(), None); | |
38 | /// assert_eq!((3..10).start(), Some(&3)); | |
39 | /// # } | |
40 | /// ``` | |
92a42be0 SL |
41 | fn start(&self) -> Option<&T> { |
42 | None | |
43 | } | |
d9579d0f AL |
44 | |
45 | /// End index (exclusive) | |
46 | /// | |
47 | /// Return end value if present, else `None`. | |
5bcae85e SL |
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 | /// | |
60 | /// assert_eq!((3..).end(), None); | |
61 | /// assert_eq!((3..10).end(), Some(&10)); | |
62 | /// # } | |
63 | /// ``` | |
92a42be0 SL |
64 | fn end(&self) -> Option<&T> { |
65 | None | |
66 | } | |
d9579d0f AL |
67 | } |
68 | ||
54a0048b | 69 | // FIXME add inclusive ranges to RangeArgument |
d9579d0f AL |
70 | |
71 | impl<T> RangeArgument<T> for RangeFull {} | |
72 | ||
73 | impl<T> RangeArgument<T> for RangeFrom<T> { | |
92a42be0 SL |
74 | fn start(&self) -> Option<&T> { |
75 | Some(&self.start) | |
76 | } | |
d9579d0f AL |
77 | } |
78 | ||
79 | impl<T> RangeArgument<T> for RangeTo<T> { | |
92a42be0 SL |
80 | fn end(&self) -> Option<&T> { |
81 | Some(&self.end) | |
82 | } | |
d9579d0f AL |
83 | } |
84 | ||
85 | impl<T> RangeArgument<T> for Range<T> { | |
92a42be0 SL |
86 | fn start(&self) -> Option<&T> { |
87 | Some(&self.start) | |
88 | } | |
89 | fn end(&self) -> Option<&T> { | |
90 | Some(&self.end) | |
91 | } | |
d9579d0f | 92 | } |