Skip to content

Commit

Permalink
Add is_empty function to ExactSizeIterator
Browse files Browse the repository at this point in the history
All other types implementing a `len` functions have `is_empty` already.
  • Loading branch information
tbu- committed Jun 19, 2016
1 parent 3313e50 commit ad335c2
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/libcore/iter/traits.rs
Expand Up @@ -485,8 +485,6 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait ExactSizeIterator: Iterator {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
/// Returns the exact number of times the iterator will iterate.
///
/// This method has a default implementation, so you usually should not
Expand All @@ -510,6 +508,8 @@ pub trait ExactSizeIterator: Iterator {
///
/// assert_eq!(5, five.len());
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn len(&self) -> usize {
let (lower, upper) = self.size_hint();
// Note: This assertion is overly defensive, but it checks the invariant
Expand All @@ -519,6 +519,31 @@ pub trait ExactSizeIterator: Iterator {
assert_eq!(upper, Some(lower));
lower
}

///
/// Returns whether the iterator is empty.
///
/// This method has a default implementation using `self.len()`, so you
/// don't need to implement it yourself.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let mut one_element = [0].iter();
/// assert!(!one_element.is_empty());
///
/// assert_eq!(one_element.next(), Some(0));
/// assert!(one_element.is_empty());
///
/// assert_eq!(one_element.next(), None);
/// ```
#[inline]
#[unstable(feature = "exact_size_is_empty", issue = "0")]
fn is_empty(&self) -> bool {
self.len() == 0
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit ad335c2

Please sign in to comment.