Auto merge of #37943 - bluss:exact-is-empty, r=alexcrichton

Implement better .is_empty() for slice and vec iterators

These iterators can use a pointer comparison instead of computing the length.
This commit is contained in:
bors 2016-11-24 03:37:44 -06:00 committed by GitHub
commit 217f57c0b5
5 changed files with 32 additions and 4 deletions

View File

@ -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)]

View File

@ -1988,7 +1988,11 @@ fn next_back(&mut self) -> Option<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> {}
@ -2082,7 +2086,11 @@ fn drop(&mut self) {
#[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> {}

View File

@ -18,6 +18,7 @@
#![feature(const_fn)]
#![feature(dedup_by)]
#![feature(enumset)]
#![feature(exact_size_is_empty)]
#![feature(pattern)]
#![feature(rand)]
#![feature(repeat_str)]

View File

@ -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];

View File

@ -983,7 +983,11 @@ fn iter_nth(&mut self, n: usize) -> Option<&'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> {}
@ -1107,7 +1111,11 @@ fn iter_nth(&mut self, n: usize) -> Option<&'a mut 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> {}