about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-07-18 14:26:22 -0700
committerGitHub <noreply@github.com>2016-07-18 14:26:22 -0700
commitbbfcb471db0799a7d92d62e66cf44bbd68051675 (patch)
tree2c50da9289268c660ceb5bbf4b2c98faedf769c2 /src/libcore
parent9c888980763e8ba16caa67f5f08e13109bbc6fa5 (diff)
parent7b2a03f08e930f6eaac419e203630b06e3d491e6 (diff)
downloadrust-bbfcb471db0799a7d92d62e66cf44bbd68051675.tar.gz
rust-bbfcb471db0799a7d92d62e66cf44bbd68051675.zip
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.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/iter/traits.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/libcore/iter/traits.rs b/src/libcore/iter/traits.rs
index 9b5c2128f1e..896d1c6f30b 100644
--- a/src/libcore/iter/traits.rs
+++ b/src/libcore/iter/traits.rs
@@ -491,8 +491,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
@@ -516,6 +514,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
@@ -525,6 +525,32 @@ 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:
+    ///
+    /// ```
+    /// #![feature(exact_size_is_empty)]
+    ///
+    /// let mut one_element = 0..1;
+    /// 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")]