Skip to content

Commit 4dbb8e7

Browse files
author
Ulrik Sverdrup
committed
collections: Add trait RangeArgument
1 parent f191f92 commit 4dbb8e7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/libcollections/lib.rs

Lines changed: 1 addition & 0 deletions
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

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

0 commit comments

Comments
 (0)