diff options
| author | bors <bors@rust-lang.org> | 2016-07-18 14:26:22 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-07-18 14:26:22 -0700 |
| commit | bbfcb471db0799a7d92d62e66cf44bbd68051675 (patch) | |
| tree | 2c50da9289268c660ceb5bbf4b2c98faedf769c2 /src | |
| parent | 9c888980763e8ba16caa67f5f08e13109bbc6fa5 (diff) | |
| parent | 7b2a03f08e930f6eaac419e203630b06e3d491e6 (diff) | |
| download | rust-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')
| -rw-r--r-- | src/libcore/iter/traits.rs | 30 | ||||
| -rw-r--r-- | src/test/compile-fail/method-suggestion-no-duplication.rs | 7 |
2 files changed, 32 insertions, 5 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")] diff --git a/src/test/compile-fail/method-suggestion-no-duplication.rs b/src/test/compile-fail/method-suggestion-no-duplication.rs index c8c1447fea3..390b8f07b2f 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: 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: } |
