Skip to content

Commit 9993a43

Browse files
author
Ulrik Sverdrup
committed
collections: Add trait RangeArgument
RangeArgument is introduced as unstable under the feature(collections_range)
1 parent f191f92 commit 9993a43

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/libcollections/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub mod borrow;
8282
pub mod enum_set;
8383
pub mod fmt;
8484
pub mod linked_list;
85+
pub mod range;
8586
pub mod slice;
8687
pub mod str;
8788
pub mod string;

src/libcollections/range.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#![unstable(feature = "collections_range", reason = "was just added")]
11+
12+
//! Range syntax.
13+
14+
use core::option::Option::{self, None, Some};
15+
use core::ops::{RangeFull, Range, RangeTo, RangeFrom};
16+
17+
/// **RangeArgument** is implemented by Rust's built-in range types, produced
18+
/// by range syntax like `..`, `a..`, `..b` or `c..d`.
19+
pub trait RangeArgument<T> {
20+
/// Start index (inclusive)
21+
///
22+
/// Return start value if present, else `None`.
23+
fn start(&self) -> Option<&T> { None }
24+
25+
/// End index (exclusive)
26+
///
27+
/// Return end value if present, else `None`.
28+
fn end(&self) -> Option<&T> { None }
29+
}
30+
31+
32+
impl<T> RangeArgument<T> for RangeFull {}
33+
34+
impl<T> RangeArgument<T> for RangeFrom<T> {
35+
fn start(&self) -> Option<&T> { Some(&self.start) }
36+
}
37+
38+
impl<T> RangeArgument<T> for RangeTo<T> {
39+
fn end(&self) -> Option<&T> { Some(&self.end) }
40+
}
41+
42+
impl<T> RangeArgument<T> for Range<T> {
43+
fn start(&self) -> Option<&T> { Some(&self.start) }
44+
fn end(&self) -> Option<&T> { Some(&self.end) }
45+
}

0 commit comments

Comments
 (0)