Skip to content

Commit 4f8049a

Browse files
committed
Add Range[Inclusive]::is_empty
During the RFC, it was discussed that figuring out whether a range is empty was subtle, and thus there should be a clear and obvious way to do it. It can't just be ExactSizeIterator::is_empty (also unstable) because not all ranges are ExactSize -- not even Range<i32> or RangeInclusive<usize>.
1 parent afa8acc commit 4f8049a

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

src/libcore/iter/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ pub trait ExactSizeIterator: Iterator {
706706
/// ```
707707
/// #![feature(exact_size_is_empty)]
708708
///
709-
/// let mut one_element = 0..1;
709+
/// let mut one_element = std::iter::once(0);
710710
/// assert!(!one_element.is_empty());
711711
///
712712
/// assert_eq!(one_element.next(), Some(0));

src/libcore/ops/range.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
9292
}
9393
}
9494

95-
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
9695
impl<Idx: PartialOrd<Idx>> Range<Idx> {
9796
/// Returns `true` if `item` is contained in the range.
9897
///
@@ -109,9 +108,26 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> {
109108
/// assert!(!(3..3).contains(3));
110109
/// assert!(!(3..2).contains(3));
111110
/// ```
111+
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
112112
pub fn contains(&self, item: Idx) -> bool {
113113
(self.start <= item) && (item < self.end)
114114
}
115+
116+
/// Returns `true` if the range contains no items.
117+
///
118+
/// # Examples
119+
///
120+
/// ```
121+
/// #![feature(range_is_empty)]
122+
///
123+
/// assert!(!(3..5).is_empty());
124+
/// assert!( (3..3).is_empty());
125+
/// assert!( (3..2).is_empty());
126+
/// ```
127+
#[unstable(feature = "range_is_empty", reason = "recently added", issue = "123456789")]
128+
pub fn is_empty(&self) -> bool {
129+
!(self.start < self.end)
130+
}
115131
}
116132

117133
/// A range only bounded inclusively below (`start..`).
@@ -280,7 +296,6 @@ impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
280296
}
281297
}
282298

283-
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
284299
impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
285300
/// Returns `true` if `item` is contained in the range.
286301
///
@@ -298,9 +313,26 @@ impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
298313
/// assert!( (3..=3).contains(3));
299314
/// assert!(!(3..=2).contains(3));
300315
/// ```
316+
#[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
301317
pub fn contains(&self, item: Idx) -> bool {
302318
self.start <= item && item <= self.end
303319
}
320+
321+
/// Returns `true` if the range contains no items.
322+
///
323+
/// # Examples
324+
///
325+
/// ```
326+
/// #![feature(range_is_empty,inclusive_range_syntax)]
327+
///
328+
/// assert!(!(3..=5).is_empty());
329+
/// assert!(!(3..=3).is_empty());
330+
/// assert!( (3..=2).is_empty());
331+
/// ```
332+
#[unstable(feature = "range_is_empty", reason = "recently added", issue = "123456789")]
333+
pub fn is_empty(&self) -> bool {
334+
!(self.start <= self.end)
335+
}
304336
}
305337

306338
/// A range only bounded inclusively above (`..=end`).

src/libcore/tests/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,9 +1427,9 @@ fn test_range_inclusive_nth() {
14271427
assert_eq!(r, 13..=20);
14281428
assert_eq!(r.nth(2), Some(15));
14291429
assert_eq!(r, 16..=20);
1430-
assert_eq!(r.is_empty(), false);
1430+
assert_eq!(ExactSizeIterator::is_empty(&r), false);
14311431
assert_eq!(r.nth(10), None);
1432-
assert_eq!(r.is_empty(), true);
1432+
assert_eq!(ExactSizeIterator::is_empty(&r), true);
14331433
assert_eq!(r, 1..=0); // We may not want to document/promise this detail
14341434
}
14351435

src/libcore/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(iter_rfold)]
3030
#![feature(nonzero)]
3131
#![feature(pattern)]
32+
#![feature(range_is_empty)]
3233
#![feature(raw)]
3334
#![feature(refcell_replace_swap)]
3435
#![feature(sip_hash_13)]

src/libcore/tests/ops.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,27 @@ fn test_range_inclusive() {
6868
assert_eq!(r.size_hint(), (0, Some(0)));
6969
assert_eq!(r.next(), None);
7070
}
71+
72+
73+
#[test]
74+
fn test_range_is_empty() {
75+
use core::f32::*;
76+
77+
assert!(!(0.0 .. 10.0).is_empty());
78+
assert!( (-0.0 .. 0.0).is_empty());
79+
assert!( (10.0 .. 0.0).is_empty());
80+
81+
assert!(!(NEG_INFINITY .. INFINITY).is_empty());
82+
assert!( (EPSILON .. NAN).is_empty());
83+
assert!( (NAN .. EPSILON).is_empty());
84+
assert!( (NAN .. NAN).is_empty());
85+
86+
assert!(!(0.0 ..= 10.0).is_empty());
87+
assert!(!(-0.0 ..= 0.0).is_empty());
88+
assert!( (10.0 ..= 0.0).is_empty());
89+
90+
assert!(!(NEG_INFINITY ..= INFINITY).is_empty());
91+
assert!( (EPSILON ..= NAN).is_empty());
92+
assert!( (NAN ..= EPSILON).is_empty());
93+
assert!( (NAN ..= NAN).is_empty());
94+
}

0 commit comments

Comments
 (0)