Skip to content

Implement better .is_empty() for slice and vec iterators #37943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#![cfg_attr(not(test), feature(char_escape_debug))]
#![feature(core_intrinsics)]
#![feature(dropck_parametricity)]
#![feature(exact_size_is_empty)]
#![feature(fmt_internals)]
#![feature(fused)]
#![feature(heap_api)]
Expand Down
12 changes: 10 additions & 2 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,11 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ExactSizeIterator for IntoIter<T> {}
impl<T> ExactSizeIterator for IntoIter<T> {
fn is_empty(&self) -> bool {
self.ptr == self.end
}
}

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


#[stable(feature = "drain", since = "1.6.0")]
impl<'a, T> ExactSizeIterator for Drain<'a, T> {}
impl<'a, T> ExactSizeIterator for Drain<'a, T> {
fn is_empty(&self) -> bool {
self.iter.is_empty()
}
}

#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for Drain<'a, T> {}
1 change: 1 addition & 0 deletions src/libcollectionstest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#![feature(const_fn)]
#![feature(dedup_by)]
#![feature(enumset)]
#![feature(exact_size_is_empty)]
#![feature(pattern)]
#![feature(rand)]
#![feature(repeat_str)]
Expand Down
10 changes: 10 additions & 0 deletions src/libcollectionstest/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,16 @@ fn test_iter_clone() {
assert_eq!(it.next(), jt.next());
}

#[test]
fn test_iter_is_empty() {
let xs = [1, 2, 5, 10, 11];
for i in 0..xs.len() {
for j in i..xs.len() {
assert_eq!(xs[i..j].iter().is_empty(), xs[i..j].is_empty());
}
}
}

#[test]
fn test_mut_iterator() {
let mut xs = [1, 2, 3, 4, 5];
Expand Down
12 changes: 10 additions & 2 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,11 @@ impl<'a, T> Iter<'a, T> {
iterator!{struct Iter -> *const T, &'a T}

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

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

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {}
impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
fn is_empty(&self) -> bool {
self.ptr == self.end
}
}

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