about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-07-14 20:57:13 -0700
committerGitHub <noreply@github.com>2017-07-14 20:57:13 -0700
commite7a9f1b626455d51c2804692f2e7c1ea4f22d78e (patch)
tree078ee9de64a96fe568238da8a11ed7f6ea39bf14
parent23ecebd6bd4362142ac586014aec44070a177a3d (diff)
parent2007987099309e05c08d65e6a0e722c5ec1d0653 (diff)
Rollup merge of #43074 - SimonSapin:iter, r=aturon
Forward more Iterator methods

This allows in more cases to take advantage of specific (possibly more optimized) impls of these methods, rather than the default one defined for all `Iterator`s.

I also wanted to do this for `&mut I` and `Box<I>`, but that didn’t compile for two reasons:

* To make the trait object-safe, generic methods (e.g. that take a closure parameter) have a `where Self: Sized` bound. But e.g. `Box<I>: Sized` does not imply `I: Sized`, and adding an additional bound in the impl is not allowed. Some for of specialization would be needed here.
* With e.g. a `F: FnMut(Self::Item) -> bool` bound and a `type Item = I::Item` associated types, I got errors like `F does not implement FnMut(I::Item) -> bool`. This looks like a limitation in the trait resolution system not recognizing that `Self::Item == I::Item` or "propagating" that fact to `FnMut` bounds.
-rw-r--r--src/libcore/iter/mod.rs8
-rw-r--r--src/libcore/str/mod.rs38
2 files changed, 46 insertions, 0 deletions
diff --git a/src/libcore/iter/mod.rs b/src/libcore/iter/mod.rs
index decd718d65e..79e6b11beac 100644
--- a/src/libcore/iter/mod.rs
+++ b/src/libcore/iter/mod.rs
@@ -359,11 +359,19 @@ impl<I> Iterator for Rev<I> where I: DoubleEndedIterator {
     #[inline]
     fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
 
+    #[inline]
     fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
         where P: FnMut(&Self::Item) -> bool
     {
         self.iter.rfind(predicate)
     }
+
+    #[inline]
+    fn rposition<P>(&mut self, predicate: P) -> Option<usize> where
+        P: FnMut(Self::Item) -> bool
+    {
+        self.iter.position(predicate)
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs
index 624c3638df5..3862b4a2eb0 100644
--- a/src/libcore/str/mod.rs
+++ b/src/libcore/str/mod.rs
@@ -710,6 +710,37 @@ impl<'a> Iterator for Bytes<'a> {
     fn nth(&mut self, n: usize) -> Option<Self::Item> {
         self.0.nth(n)
     }
+
+    #[inline]
+    fn all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool {
+        self.0.all(f)
+    }
+
+    #[inline]
+    fn any<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool {
+        self.0.any(f)
+    }
+
+    #[inline]
+    fn find<P>(&mut self, predicate: P) -> Option<Self::Item> where
+        P: FnMut(&Self::Item) -> bool
+    {
+        self.0.find(predicate)
+    }
+
+    #[inline]
+    fn position<P>(&mut self, predicate: P) -> Option<usize> where
+        P: FnMut(Self::Item) -> bool
+    {
+        self.0.position(predicate)
+    }
+
+    #[inline]
+    fn rposition<P>(&mut self, predicate: P) -> Option<usize> where
+        P: FnMut(Self::Item) -> bool
+    {
+        self.0.rposition(predicate)
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -718,6 +749,13 @@ impl<'a> DoubleEndedIterator for Bytes<'a> {
     fn next_back(&mut self) -> Option<u8> {
         self.0.next_back()
     }
+
+    #[inline]
+    fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> where
+        P: FnMut(&Self::Item) -> bool
+    {
+        self.0.rfind(predicate)
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]