From ad335c2d50ba140392807c167839c3869c95a3d1 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sun, 19 Jun 2016 11:45:26 +0200 Subject: [PATCH] Add `is_empty` function to `ExactSizeIterator` All other types implementing a `len` functions have `is_empty` already. --- src/libcore/iter/traits.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/libcore/iter/traits.rs b/src/libcore/iter/traits.rs index 67503984450a4..41c34d932621c 100644 --- a/src/libcore/iter/traits.rs +++ b/src/libcore/iter/traits.rs @@ -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 @@ -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 @@ -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")]