Skip to content

Commit 74cde12

Browse files
committed
core, collections: Implement better .is_empty() for slice and vec iterators
These iterators can use a pointer comparison instead of computing the length.
1 parent 5a3aa2f commit 74cde12

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

src/libcollections/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#![cfg_attr(not(test), feature(char_escape_debug))]
3737
#![feature(core_intrinsics)]
3838
#![feature(dropck_parametricity)]
39+
#![feature(exact_size_is_empty)]
3940
#![feature(fmt_internals)]
4041
#![feature(fused)]
4142
#![feature(heap_api)]

src/libcollections/vec.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,11 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
19881988
}
19891989

19901990
#[stable(feature = "rust1", since = "1.0.0")]
1991-
impl<T> ExactSizeIterator for IntoIter<T> {}
1991+
impl<T> ExactSizeIterator for IntoIter<T> {
1992+
fn is_empty(&self) -> bool {
1993+
self.ptr == self.end
1994+
}
1995+
}
19921996

19931997
#[unstable(feature = "fused", issue = "35602")]
19941998
impl<T> FusedIterator for IntoIter<T> {}
@@ -2082,7 +2086,11 @@ impl<'a, T> Drop for Drain<'a, T> {
20822086

20832087

20842088
#[stable(feature = "drain", since = "1.6.0")]
2085-
impl<'a, T> ExactSizeIterator for Drain<'a, T> {}
2089+
impl<'a, T> ExactSizeIterator for Drain<'a, T> {
2090+
fn is_empty(&self) -> bool {
2091+
self.iter.is_empty()
2092+
}
2093+
}
20862094

20872095
#[unstable(feature = "fused", issue = "35602")]
20882096
impl<'a, T> FusedIterator for Drain<'a, T> {}

src/libcollectionstest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#![feature(const_fn)]
1919
#![feature(dedup_by)]
2020
#![feature(enumset)]
21+
#![feature(exact_size_is_empty)]
2122
#![feature(pattern)]
2223
#![feature(rand)]
2324
#![feature(repeat_str)]

src/libcollectionstest/slice.rs

+10
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,16 @@ fn test_iter_clone() {
633633
assert_eq!(it.next(), jt.next());
634634
}
635635

636+
#[test]
637+
fn test_iter_is_empty() {
638+
let xs = [1, 2, 5, 10, 11];
639+
for i in 0..xs.len() {
640+
for j in i..xs.len() {
641+
assert_eq!(xs[i..j].iter().is_empty(), xs[i..j].is_empty());
642+
}
643+
}
644+
}
645+
636646
#[test]
637647
fn test_mut_iterator() {
638648
let mut xs = [1, 2, 3, 4, 5];

src/libcore/slice.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,11 @@ impl<'a, T> Iter<'a, T> {
983983
iterator!{struct Iter -> *const T, &'a T}
984984

985985
#[stable(feature = "rust1", since = "1.0.0")]
986-
impl<'a, T> ExactSizeIterator for Iter<'a, T> {}
986+
impl<'a, T> ExactSizeIterator for Iter<'a, T> {
987+
fn is_empty(&self) -> bool {
988+
self.ptr == self.end
989+
}
990+
}
987991

988992
#[unstable(feature = "fused", issue = "35602")]
989993
impl<'a, T> FusedIterator for Iter<'a, T> {}
@@ -1107,7 +1111,11 @@ impl<'a, T> IterMut<'a, T> {
11071111
iterator!{struct IterMut -> *mut T, &'a mut T}
11081112

11091113
#[stable(feature = "rust1", since = "1.0.0")]
1110-
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
1114+
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
1115+
fn is_empty(&self) -> bool {
1116+
self.ptr == self.end
1117+
}
1118+
}
11111119

11121120
#[unstable(feature = "fused", issue = "35602")]
11131121
impl<'a, T> FusedIterator for IterMut<'a, T> {}

0 commit comments

Comments
 (0)