]> git.lizzy.rs Git - rust.git/blob - src/libcollections/range.rs
Refactor `proc_macro::TokenStream` to use `syntax::tokenstream::TokenStream`; fix...
[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};
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 impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
109     fn start(&self) -> Bound<&T> {
110         match *self {
111             (Included(ref start), _) => Included(start),
112             (Excluded(ref start), _) => Excluded(start),
113             (Unbounded, _)           => Unbounded,
114         }
115     }
116
117     fn end(&self) -> Bound<&T> {
118         match *self {
119             (_, Included(ref end)) => Included(end),
120             (_, Excluded(ref end)) => Excluded(end),
121             (_, Unbounded)         => Unbounded,
122         }
123     }
124 }
125
126 impl<'a, T: ?Sized + 'a> RangeArgument<T> for (Bound<&'a T>, Bound<&'a T>) {
127     fn start(&self) -> Bound<&T> {
128         self.0
129     }
130
131     fn end(&self) -> Bound<&T> {
132         self.1
133     }
134 }