]> git.lizzy.rs Git - rust.git/blob - src/libcollections/range.rs
Auto merge of #27630 - sylvestre:master, r=dotdash
[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", reason = "was just added")]
12
13 //! Range syntax.
14
15 use core::option::Option::{self, None, Some};
16 use core::ops::{RangeFull, Range, RangeTo, RangeFrom};
17
18 /// **RangeArgument** is implemented by Rust's built-in range types, produced
19 /// by range syntax like `..`, `a..`, `..b` or `c..d`.
20 pub trait RangeArgument<T> {
21     /// Start index (inclusive)
22     ///
23     /// Return start value if present, else `None`.
24     fn start(&self) -> Option<&T> { None }
25
26     /// End index (exclusive)
27     ///
28     /// Return end value if present, else `None`.
29     fn end(&self) -> Option<&T> { None }
30 }
31
32
33 impl<T> RangeArgument<T> for RangeFull {}
34
35 impl<T> RangeArgument<T> for RangeFrom<T> {
36     fn start(&self) -> Option<&T> { Some(&self.start) }
37 }
38
39 impl<T> RangeArgument<T> for RangeTo<T> {
40     fn end(&self) -> Option<&T> { Some(&self.end) }
41 }
42
43 impl<T> RangeArgument<T> for Range<T> {
44     fn start(&self) -> Option<&T> { Some(&self.start) }
45     fn end(&self) -> Option<&T> { Some(&self.end) }
46 }