From ad335c2d50ba140392807c167839c3869c95a3d1 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sun, 19 Jun 2016 11:45:26 +0200 Subject: [PATCH 1/4] 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")] From 208de46f8b7c7338f7d19057e748a45a72fc7d73 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sun, 19 Jun 2016 18:52:33 +0200 Subject: [PATCH 2/4] Remove first empty line of doc comment --- src/libcore/iter/traits.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libcore/iter/traits.rs b/src/libcore/iter/traits.rs index 41c34d932621c..b7ee9ba55f591 100644 --- a/src/libcore/iter/traits.rs +++ b/src/libcore/iter/traits.rs @@ -520,7 +520,6 @@ pub trait ExactSizeIterator: Iterator { lower } - /// /// Returns whether the iterator is empty. /// /// This method has a default implementation using `self.len()`, so you From f2e73d9d48dec8ae3103ba46a35586a2a5435a4f Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Tue, 12 Jul 2016 10:43:37 +0200 Subject: [PATCH 3/4] Fix compile-fail test for `ExactSizeIterator::is_empty` --- src/test/compile-fail/method-suggestion-no-duplication.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/compile-fail/method-suggestion-no-duplication.rs b/src/test/compile-fail/method-suggestion-no-duplication.rs index c8c1447fea386..390b8f07b2fbd 100644 --- a/src/test/compile-fail/method-suggestion-no-duplication.rs +++ b/src/test/compile-fail/method-suggestion-no-duplication.rs @@ -18,7 +18,8 @@ fn foo(f: F) where F: FnMut(Foo) {} fn main() { foo(|s| s.is_empty()); //~^ ERROR no method named `is_empty` found - //~^^ HELP #1: `core::slice::SliceExt` - //~^^^ HELP #2: `core::str::StrExt` - //~^^^^ 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: + //~^^ HELP #1: `std::iter::ExactSizeIterator` + //~^^^ HELP #2: `core::slice::SliceExt` + //~^^^^ HELP #3: `core::str::StrExt` + //~^^^^^ 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: } From 7b2a03f08e930f6eaac419e203630b06e3d491e6 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Mon, 18 Jul 2016 12:08:37 +0200 Subject: [PATCH 4/4] Fix doctest of `ExactSizeIterator::is_empty` --- src/libcore/iter/traits.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libcore/iter/traits.rs b/src/libcore/iter/traits.rs index b7ee9ba55f591..b1af30559328c 100644 --- a/src/libcore/iter/traits.rs +++ b/src/libcore/iter/traits.rs @@ -530,7 +530,9 @@ pub trait ExactSizeIterator: Iterator { /// Basic usage: /// /// ``` - /// let mut one_element = [0].iter(); + /// #![feature(exact_size_is_empty)] + /// + /// let mut one_element = 0..1; /// assert!(!one_element.is_empty()); /// /// assert_eq!(one_element.next(), Some(0));