]> git.proxmox.com Git - rustc.git/blob - src/libcollections/range.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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::option::Option::{self, None, Some};
18 use core::ops::{RangeFull, Range, RangeTo, RangeFrom};
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> {
23 /// Start index (inclusive)
24 ///
25 /// Return start value if present, else `None`.
26 fn start(&self) -> Option<&T> {
27 None
28 }
29
30 /// End index (exclusive)
31 ///
32 /// Return end value if present, else `None`.
33 fn end(&self) -> Option<&T> {
34 None
35 }
36 }
37
38 // FIXME add inclusive ranges to RangeArgument
39
40 impl<T> RangeArgument<T> for RangeFull {}
41
42 impl<T> RangeArgument<T> for RangeFrom<T> {
43 fn start(&self) -> Option<&T> {
44 Some(&self.start)
45 }
46 }
47
48 impl<T> RangeArgument<T> for RangeTo<T> {
49 fn end(&self) -> Option<&T> {
50 Some(&self.end)
51 }
52 }
53
54 impl<T> RangeArgument<T> for Range<T> {
55 fn start(&self) -> Option<&T> {
56 Some(&self.start)
57 }
58 fn end(&self) -> Option<&T> {
59 Some(&self.end)
60 }
61 }