From 328818934b3cc3a19c29fd4f87f165808bf9c037 Mon Sep 17 00:00:00 2001 From: Djzin Date: Sat, 18 Feb 2017 18:57:18 +0000 Subject: impl RangeArgument for RangeInclusive and add appropriate tests --- src/libcollections/range.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/libcollections') diff --git a/src/libcollections/range.rs b/src/libcollections/range.rs index 1df4ace3777..e62df03f4fb 100644 --- a/src/libcollections/range.rs +++ b/src/libcollections/range.rs @@ -14,7 +14,7 @@ //! Range syntax. -use core::ops::{RangeFull, Range, RangeTo, RangeFrom}; +use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive}; use Bound::{self, Excluded, Included, Unbounded}; /// **RangeArgument** is implemented by Rust's built-in range types, produced @@ -105,6 +105,22 @@ impl RangeArgument for Range { } } +#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +impl RangeArgument for RangeInclusive { + fn start(&self) -> Bound<&T> { + match *self { + RangeInclusive::Empty{ ref at } => Included(at), + RangeInclusive::NonEmpty { ref start, .. } => Included(start), + } + } + fn end(&self) -> Bound<&T> { + match *self { + RangeInclusive::Empty{ ref at } => Excluded(at), + RangeInclusive::NonEmpty { ref end, .. } => Included(end), + } + } +} + impl RangeArgument for (Bound, Bound) { fn start(&self) -> Bound<&T> { match *self { -- cgit 1.4.1-3-g733a5 From 43382903a19ce209dee136e5f983003062b7ce78 Mon Sep 17 00:00:00 2001 From: Djzin Date: Sat, 18 Feb 2017 20:58:12 +0000 Subject: add impl for RangeToInclusive --- src/libcollections/range.rs | 12 +++++++++++- src/libcollectionstest/btree/map.rs | 2 ++ src/libcollectionstest/vec.rs | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src/libcollections') diff --git a/src/libcollections/range.rs b/src/libcollections/range.rs index e62df03f4fb..e4b94a1d70e 100644 --- a/src/libcollections/range.rs +++ b/src/libcollections/range.rs @@ -14,7 +14,7 @@ //! Range syntax. -use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive}; +use core::ops::{RangeFull, Range, RangeTo, RangeFrom, RangeInclusive, RangeToInclusive}; use Bound::{self, Excluded, Included, Unbounded}; /// **RangeArgument** is implemented by Rust's built-in range types, produced @@ -121,6 +121,16 @@ impl RangeArgument for RangeInclusive { } } +#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")] +impl RangeArgument for RangeToInclusive { + fn start(&self) -> Bound<&T> { + Unbounded + } + fn end(&self) -> Bound<&T> { + Included(&self.end) + } +} + impl RangeArgument for (Bound, Bound) { fn start(&self) -> Bound<&T> { match *self { diff --git a/src/libcollectionstest/btree/map.rs b/src/libcollectionstest/btree/map.rs index 0fa73b19343..2c899d96940 100644 --- a/src/libcollectionstest/btree/map.rs +++ b/src/libcollectionstest/btree/map.rs @@ -200,6 +200,8 @@ fn test_range_inclusive() { check(map.range(0...size - 1), map.range(..size)); check(map.range(-1...-1), vec![]); check(map.range(-1...size), map.range(..)); + check(map.range(...size), map.range(..)); + check(map.range(...200), map.range(..201)); check(map.range(5...8), vec![(&5, &5), (&6, &6), (&7, &7), (&8, &8)]); check(map.range(-1...0), vec![(&0, &0)]); check(map.range(-1...2), vec![(&0, &0), (&1, &1), (&2, &2)]); diff --git a/src/libcollectionstest/vec.rs b/src/libcollectionstest/vec.rs index ef3ab964f77..06d70800d39 100644 --- a/src/libcollectionstest/vec.rs +++ b/src/libcollectionstest/vec.rs @@ -528,6 +528,11 @@ fn test_drain_inclusive_range() { for _ in v.drain(0...3) { } assert_eq!(v, &["4".to_string(), "5".to_string()]); + + let mut v: Vec<_> = (0...1).map(|x| x.to_string()).collect(); + for _ in v.drain(...0) { + } + assert_eq!(v, &["1".to_string()]); } #[test] -- cgit 1.4.1-3-g733a5