]> git.lizzy.rs Git - rust.git/blob - src/libcollections/range.rs
rustfmt libcollections
[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             issue = "27711")]
13
14 //! Range syntax.
15
16 use core::option::Option::{self, None, Some};
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`.
25     fn start(&self) -> Option<&T> {
26         None
27     }
28
29     /// End index (exclusive)
30     ///
31     /// Return end value if present, else `None`.
32     fn end(&self) -> Option<&T> {
33         None
34     }
35 }
36
37
38 impl<T> RangeArgument<T> for RangeFull {}
39
40 impl<T> RangeArgument<T> for RangeFrom<T> {
41     fn start(&self) -> Option<&T> {
42         Some(&self.start)
43     }
44 }
45
46 impl<T> RangeArgument<T> for RangeTo<T> {
47     fn end(&self) -> Option<&T> {
48         Some(&self.end)
49     }
50 }
51
52 impl<T> RangeArgument<T> for Range<T> {
53     fn start(&self) -> Option<&T> {
54         Some(&self.start)
55     }
56     fn end(&self) -> Option<&T> {
57         Some(&self.end)
58     }
59 }