]> git.lizzy.rs Git - rust.git/commitdiff
Add `is_empty` function to `ExactSizeIterator`
authorTobias Bucher <tobiasbucher5991@gmail.com>
Sun, 19 Jun 2016 09:45:26 +0000 (11:45 +0200)
committerTobias Bucher <tobiasbucher5991@gmail.com>
Sun, 19 Jun 2016 09:45:26 +0000 (11:45 +0200)
All other types implementing a `len` functions have `is_empty` already.

src/libcore/iter/traits.rs

index 67503984450a45806a3af0bd1f8667397269a058..41c34d932621cfaafbec3dd3b316c572070dca96 100644 (file)
@@ -485,8 +485,6 @@ fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
 /// ```
 #[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 @@ fn len(&self) -> usize {
         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")]