Skip to content
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

Add is_empty function to ExactSizeIterator #34357

Merged
merged 4 commits into from
Jul 19, 2016
Merged
Changes from 1 commit
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
29 changes: 27 additions & 2 deletions src/libcore/iter/traits.rs
Original file line number Diff line number Diff line change
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
}

///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustdoc might not like this leading empty doc comment line :)

/// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling .len() you could also only check let (lower, upper) = self.size_hint(); upper == 0 and skip the assert. (I'm sure sure this is better.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert! will be optimized out in most cases anyway; the Iterator::size_hint function of such iterators usually does something along the lines of let len = ...; (len, Some(len)) which the compiler can see through and optimize out the assert! in ExactSizeIterator::len.

}
}

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