Skip to content

Commit bbfcb47

Browse files
authored
Auto merge of #34357 - tbu-:pr_exact_size_is_empty, r=brson
Add `is_empty` function to `ExactSizeIterator` All other types implementing a `len` functions have `is_empty` already.
2 parents 9c88898 + 7b2a03f commit bbfcb47

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/libcore/iter/traits.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,6 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
491491
/// ```
492492
#[stable(feature = "rust1", since = "1.0.0")]
493493
pub trait ExactSizeIterator: Iterator {
494-
#[inline]
495-
#[stable(feature = "rust1", since = "1.0.0")]
496494
/// Returns the exact number of times the iterator will iterate.
497495
///
498496
/// This method has a default implementation, so you usually should not
@@ -516,6 +514,8 @@ pub trait ExactSizeIterator: Iterator {
516514
///
517515
/// assert_eq!(5, five.len());
518516
/// ```
517+
#[inline]
518+
#[stable(feature = "rust1", since = "1.0.0")]
519519
fn len(&self) -> usize {
520520
let (lower, upper) = self.size_hint();
521521
// Note: This assertion is overly defensive, but it checks the invariant
@@ -525,6 +525,32 @@ pub trait ExactSizeIterator: Iterator {
525525
assert_eq!(upper, Some(lower));
526526
lower
527527
}
528+
529+
/// Returns whether the iterator is empty.
530+
///
531+
/// This method has a default implementation using `self.len()`, so you
532+
/// don't need to implement it yourself.
533+
///
534+
/// # Examples
535+
///
536+
/// Basic usage:
537+
///
538+
/// ```
539+
/// #![feature(exact_size_is_empty)]
540+
///
541+
/// let mut one_element = 0..1;
542+
/// assert!(!one_element.is_empty());
543+
///
544+
/// assert_eq!(one_element.next(), Some(0));
545+
/// assert!(one_element.is_empty());
546+
///
547+
/// assert_eq!(one_element.next(), None);
548+
/// ```
549+
#[inline]
550+
#[unstable(feature = "exact_size_is_empty", issue = "0")]
551+
fn is_empty(&self) -> bool {
552+
self.len() == 0
553+
}
528554
}
529555

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

src/test/compile-fail/method-suggestion-no-duplication.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ fn foo<F>(f: F) where F: FnMut(Foo) {}
1818
fn main() {
1919
foo(|s| s.is_empty());
2020
//~^ ERROR no method named `is_empty` found
21-
//~^^ HELP #1: `core::slice::SliceExt`
22-
//~^^^ HELP #2: `core::str::StrExt`
23-
//~^^^^ 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:
21+
//~^^ HELP #1: `std::iter::ExactSizeIterator`
22+
//~^^^ HELP #2: `core::slice::SliceExt`
23+
//~^^^^ HELP #3: `core::str::StrExt`
24+
//~^^^^^ 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:
2425
}

0 commit comments

Comments
 (0)