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

Forward more ExactSizeIterator methods and `is_empty` edits

- Forward ExactSizeIterator methods in more places, like `&mut I` and `Box<I>` iterator impls.
- Improve `VecDeque::is_empty` itself (see commit 4)
- All the collections iterators now have `len` or `is_empty` forwarded if doing so is a benefit. In the remaining cases, they already use a simple size hint (using something like a stored `usize` value), which is sufficient for the default implementation of len and is_empty.
This commit is contained in:
bors 2016-12-07 07:15:31 +00:00
commit 5938eba4e3
9 changed files with 77 additions and 9 deletions

View File

@ -535,7 +535,14 @@ impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {
fn len(&self) -> usize {
(**self).len()
}
fn is_empty(&self) -> bool {
(**self).is_empty()
}
}
#[unstable(feature = "fused", issue = "35602")]
impl<I: FusedIterator + ?Sized> FusedIterator for Box<I> {}

View File

@ -79,6 +79,7 @@
#![feature(core_intrinsics)]
#![feature(custom_attribute)]
#![feature(dropck_parametricity)]
#![cfg_attr(not(test), feature(exact_size_is_empty))]
#![feature(fundamental)]
#![feature(lang_items)]
#![feature(needs_allocator)]

View File

@ -986,7 +986,11 @@ impl<'a, T> DoubleEndedIterator for Iter<'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.iter.is_empty()
}
}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for Iter<'a, T> {}
@ -1022,7 +1026,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.iter.is_empty()
}
}
#[unstable(feature = "fused", issue = "35602")]
impl<T> FusedIterator for IntoIter<T> {}
@ -1057,7 +1065,11 @@ impl<'a, T: 'a> DoubleEndedIterator for Drain<'a, T> {
}
#[stable(feature = "drain", since = "1.6.0")]
impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {}
impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {
fn is_empty(&self) -> bool {
self.iter.is_empty()
}
}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T: 'a> FusedIterator for Drain<'a, T> {}

View File

@ -810,7 +810,7 @@ impl<T> VecDeque<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_empty(&self) -> bool {
self.len() == 0
self.tail == self.head
}
/// Create a draining iterator that removes the specified range in the
@ -1916,7 +1916,11 @@ impl<'a, T> DoubleEndedIterator for Iter<'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.head == self.tail
}
}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for Iter<'a, T> {}
@ -1980,7 +1984,11 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, 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.head == self.tail
}
}
#[unstable(feature = "fused", issue = "35602")]
impl<'a, T> FusedIterator for IterMut<'a, T> {}
@ -2017,7 +2025,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.inner.is_empty()
}
}
#[unstable(feature = "fused", issue = "35602")]
impl<T> FusedIterator for IntoIter<T> {}

View File

@ -1007,3 +1007,24 @@ fn assert_covariance() {
d
}
}
#[test]
fn test_is_empty() {
let mut v = VecDeque::<i32>::new();
assert!(v.is_empty());
assert!(v.iter().is_empty());
assert!(v.iter_mut().is_empty());
v.extend(&[2, 3, 4]);
assert!(!v.is_empty());
assert!(!v.iter().is_empty());
assert!(!v.iter_mut().is_empty());
while let Some(_) = v.pop_front() {
assert_eq!(v.is_empty(), v.len() == 0);
assert_eq!(v.iter().is_empty(), v.iter().len() == 0);
assert_eq!(v.iter_mut().is_empty(), v.iter_mut().len() == 0);
}
assert!(v.is_empty());
assert!(v.iter().is_empty());
assert!(v.iter_mut().is_empty());
assert!(v.into_iter().is_empty());
}

View File

@ -552,7 +552,14 @@ pub trait ExactSizeIterator: Iterator {
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I {}
impl<'a, I: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I {
fn len(&self) -> usize {
(**self).len()
}
fn is_empty(&self) -> bool {
(**self).is_empty()
}
}
/// Trait to represent types that can be created by summing up an iterator.
///

View File

@ -618,6 +618,11 @@ impl<'a> ExactSizeIterator for Bytes<'a> {
fn len(&self) -> usize {
self.0.len()
}
#[inline]
fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
#[unstable(feature = "fused", issue = "35602")]

View File

@ -630,6 +630,7 @@ impl Iterator for Args {
#[stable(feature = "env", since = "1.0.0")]
impl ExactSizeIterator for Args {
fn len(&self) -> usize { self.inner.len() }
fn is_empty(&self) -> bool { self.inner.is_empty() }
}
#[stable(feature = "env_iterators", since = "1.11.0")]
@ -649,6 +650,7 @@ impl Iterator for ArgsOs {
#[stable(feature = "env", since = "1.0.0")]
impl ExactSizeIterator for ArgsOs {
fn len(&self) -> usize { self.inner.len() }
fn is_empty(&self) -> bool { self.inner.is_empty() }
}
#[stable(feature = "env_iterators", since = "1.11.0")]

View File

@ -250,6 +250,7 @@
#![feature(core_float)]
#![feature(core_intrinsics)]
#![feature(dropck_parametricity)]
#![feature(exact_size_is_empty)]
#![feature(float_extras)]
#![feature(float_from_str_radix)]
#![feature(fn_traits)]