Skip to content

Commit

Permalink
Auto merge of rust-lang#34357 - tbu-:pr_exact_size_is_empty, r=brson
Browse files Browse the repository at this point in the history
Add `is_empty` function to `ExactSizeIterator`

All other types implementing a `len` functions have `is_empty` already.
  • Loading branch information
bors authored Jul 18, 2016
2 parents 9c88898 + 7b2a03f commit bbfcb47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
30 changes: 28 additions & 2 deletions src/libcore/iter/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,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 @@ -516,6 +514,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 @@ -525,6 +525,32 @@ 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:
///
/// ```
/// #![feature(exact_size_is_empty)]
///
/// let mut one_element = 0..1;
/// 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
7 changes: 4 additions & 3 deletions src/test/compile-fail/method-suggestion-no-duplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ fn foo<F>(f: F) where F: FnMut(Foo) {}
fn main() {
foo(|s| s.is_empty());
//~^ ERROR no method named `is_empty` found
//~^^ HELP #1: `core::slice::SliceExt`
//~^^^ HELP #2: `core::str::StrExt`
//~^^^^ HELP items from traits can only be used if the trait is implemented and in scope; the following traits define an item `is_empty`, perhaps you need to implement one of them:
//~^^ HELP #1: `std::iter::ExactSizeIterator`
//~^^^ HELP #2: `core::slice::SliceExt`
//~^^^^ HELP #3: `core::str::StrExt`
//~^^^^^ HELP items from traits can only be used if the trait is implemented and in scope; the following traits define an item `is_empty`, perhaps you need to implement one of them:
}

0 comments on commit bbfcb47

Please sign in to comment.