Skip to content

Commit 3288189

Browse files
committed
impl RangeArgument for RangeInclusive and add appropriate tests
1 parent 3c97cbe commit 3288189

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

src/libcollections/range.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
//! Range syntax.
1616
17-
use core::ops::{RangeFull, Range, RangeTo, RangeFrom};
17+
use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive};
1818
use Bound::{self, Excluded, Included, Unbounded};
1919

2020
/// **RangeArgument** is implemented by Rust's built-in range types, produced
@@ -105,6 +105,22 @@ impl<T> RangeArgument<T> for Range<T> {
105105
}
106106
}
107107

108+
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
109+
impl<T> RangeArgument<T> for RangeInclusive<T> {
110+
fn start(&self) -> Bound<&T> {
111+
match *self {
112+
RangeInclusive::Empty{ ref at } => Included(at),
113+
RangeInclusive::NonEmpty { ref start, .. } => Included(start),
114+
}
115+
}
116+
fn end(&self) -> Bound<&T> {
117+
match *self {
118+
RangeInclusive::Empty{ ref at } => Excluded(at),
119+
RangeInclusive::NonEmpty { ref end, .. } => Included(end),
120+
}
121+
}
122+
}
123+
108124
impl<T> RangeArgument<T> for (Bound<T>, Bound<T>) {
109125
fn start(&self) -> Bound<&T> {
110126
match *self {

src/libcollectionstest/btree/map.rs

+27
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ fn test_range_small() {
178178
assert_eq!(j, size - 2);
179179
}
180180

181+
#[test]
182+
fn test_range_inclusive() {
183+
let size = 500;
184+
185+
let map: BTreeMap<_, _> = (0...size).map(|i| (i, i)).collect();
186+
187+
fn check<'a, L, R>(lhs: L, rhs: R)
188+
where L: IntoIterator<Item=(&'a i32, &'a i32)>,
189+
R: IntoIterator<Item=(&'a i32, &'a i32)>,
190+
{
191+
let lhs: Vec<_> = lhs.into_iter().collect();
192+
let rhs: Vec<_> = rhs.into_iter().collect();
193+
assert_eq!(lhs, rhs);
194+
}
195+
196+
check(map.range(size + 1...size + 1), vec![]);
197+
check(map.range(size...size), vec![(&size, &size)]);
198+
check(map.range(size...size + 1), vec![(&size, &size)]);
199+
check(map.range(0...0), vec![(&0, &0)]);
200+
check(map.range(0...size - 1), map.range(..size));
201+
check(map.range(-1...-1), vec![]);
202+
check(map.range(-1...size), map.range(..));
203+
check(map.range(5...8), vec![(&5, &5), (&6, &6), (&7, &7), (&8, &8)]);
204+
check(map.range(-1...0), vec![(&0, &0)]);
205+
check(map.range(-1...2), vec![(&0, &0), (&1, &1), (&2, &2)]);
206+
}
207+
181208
#[test]
182209
fn test_range_equal_empty_cases() {
183210
let map: BTreeMap<_, _> = (0..5).map(|i| (i, i)).collect();

src/libcollectionstest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(binary_heap_peek_mut_pop)]
1515
#![feature(box_syntax)]
1616
#![feature(btree_range)]
17+
#![feature(inclusive_range_syntax)]
1718
#![feature(collection_placement)]
1819
#![feature(collections)]
1920
#![feature(collections_bound)]

src/libcollectionstest/vec.rs

+45
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,51 @@ fn test_drain_range() {
507507
assert_eq!(v, &[(), ()]);
508508
}
509509

510+
#[test]
511+
fn test_drain_inclusive_range() {
512+
let mut v = vec!['a', 'b', 'c', 'd', 'e'];
513+
for _ in v.drain(1...3) {
514+
}
515+
assert_eq!(v, &['a', 'e']);
516+
517+
let mut v: Vec<_> = (0...5).map(|x| x.to_string()).collect();
518+
for _ in v.drain(1...5) {
519+
}
520+
assert_eq!(v, &["0".to_string()]);
521+
522+
let mut v: Vec<String> = (0...5).map(|x| x.to_string()).collect();
523+
for _ in v.drain(0...5) {
524+
}
525+
assert_eq!(v, Vec::<String>::new());
526+
527+
let mut v: Vec<_> = (0...5).map(|x| x.to_string()).collect();
528+
for _ in v.drain(0...3) {
529+
}
530+
assert_eq!(v, &["4".to_string(), "5".to_string()]);
531+
}
532+
533+
#[test]
534+
fn test_drain_max_vec_size() {
535+
let mut v = Vec::<()>::with_capacity(usize::max_value());
536+
unsafe { v.set_len(usize::max_value()); }
537+
for _ in v.drain(usize::max_value() - 1..) {
538+
}
539+
assert_eq!(v.len(), usize::max_value() - 1);
540+
541+
let mut v = Vec::<()>::with_capacity(usize::max_value());
542+
unsafe { v.set_len(usize::max_value()); }
543+
for _ in v.drain(usize::max_value() - 1...usize::max_value() - 1) {
544+
}
545+
assert_eq!(v.len(), usize::max_value() - 1);
546+
}
547+
548+
#[test]
549+
#[should_panic]
550+
fn test_drain_inclusive_out_of_bounds() {
551+
let mut v = vec![1, 2, 3, 4, 5];
552+
v.drain(5...5);
553+
}
554+
510555
#[test]
511556
fn test_into_boxed_slice() {
512557
let xs = vec![1, 2, 3];

0 commit comments

Comments
 (0)