Skip to content

Commit ad335c2

Browse files
committed
Add is_empty function to ExactSizeIterator
All other types implementing a `len` functions have `is_empty` already.
1 parent 3313e50 commit ad335c2

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/libcore/iter/traits.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
485485
/// ```
486486
#[stable(feature = "rust1", since = "1.0.0")]
487487
pub trait ExactSizeIterator: Iterator {
488-
#[inline]
489-
#[stable(feature = "rust1", since = "1.0.0")]
490488
/// Returns the exact number of times the iterator will iterate.
491489
///
492490
/// This method has a default implementation, so you usually should not
@@ -510,6 +508,8 @@ pub trait ExactSizeIterator: Iterator {
510508
///
511509
/// assert_eq!(5, five.len());
512510
/// ```
511+
#[inline]
512+
#[stable(feature = "rust1", since = "1.0.0")]
513513
fn len(&self) -> usize {
514514
let (lower, upper) = self.size_hint();
515515
// Note: This assertion is overly defensive, but it checks the invariant
@@ -519,6 +519,31 @@ pub trait ExactSizeIterator: Iterator {
519519
assert_eq!(upper, Some(lower));
520520
lower
521521
}
522+
523+
///
524+
/// Returns whether the iterator is empty.
525+
///
526+
/// This method has a default implementation using `self.len()`, so you
527+
/// don't need to implement it yourself.
528+
///
529+
/// # Examples
530+
///
531+
/// Basic usage:
532+
///
533+
/// ```
534+
/// let mut one_element = [0].iter();
535+
/// assert!(!one_element.is_empty());
536+
///
537+
/// assert_eq!(one_element.next(), Some(0));
538+
/// assert!(one_element.is_empty());
539+
///
540+
/// assert_eq!(one_element.next(), None);
541+
/// ```
542+
#[inline]
543+
#[unstable(feature = "exact_size_is_empty", issue = "0")]
544+
fn is_empty(&self) -> bool {
545+
self.len() == 0
546+
}
522547
}
523548

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

0 commit comments

Comments
 (0)