Skip to content

Commit e7a9f1b

Browse files
authored
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.
2 parents 23ecebd + 2007987 commit e7a9f1b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/libcore/iter/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,19 @@ impl<I> Iterator for Rev<I> where I: DoubleEndedIterator {
359359
#[inline]
360360
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
361361

362+
#[inline]
362363
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
363364
where P: FnMut(&Self::Item) -> bool
364365
{
365366
self.iter.rfind(predicate)
366367
}
368+
369+
#[inline]
370+
fn rposition<P>(&mut self, predicate: P) -> Option<usize> where
371+
P: FnMut(Self::Item) -> bool
372+
{
373+
self.iter.position(predicate)
374+
}
367375
}
368376

369377
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/str/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,37 @@ impl<'a> Iterator for Bytes<'a> {
710710
fn nth(&mut self, n: usize) -> Option<Self::Item> {
711711
self.0.nth(n)
712712
}
713+
714+
#[inline]
715+
fn all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool {
716+
self.0.all(f)
717+
}
718+
719+
#[inline]
720+
fn any<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool {
721+
self.0.any(f)
722+
}
723+
724+
#[inline]
725+
fn find<P>(&mut self, predicate: P) -> Option<Self::Item> where
726+
P: FnMut(&Self::Item) -> bool
727+
{
728+
self.0.find(predicate)
729+
}
730+
731+
#[inline]
732+
fn position<P>(&mut self, predicate: P) -> Option<usize> where
733+
P: FnMut(Self::Item) -> bool
734+
{
735+
self.0.position(predicate)
736+
}
737+
738+
#[inline]
739+
fn rposition<P>(&mut self, predicate: P) -> Option<usize> where
740+
P: FnMut(Self::Item) -> bool
741+
{
742+
self.0.rposition(predicate)
743+
}
713744
}
714745

715746
#[stable(feature = "rust1", since = "1.0.0")]
@@ -718,6 +749,13 @@ impl<'a> DoubleEndedIterator for Bytes<'a> {
718749
fn next_back(&mut self) -> Option<u8> {
719750
self.0.next_back()
720751
}
752+
753+
#[inline]
754+
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> where
755+
P: FnMut(&Self::Item) -> bool
756+
{
757+
self.0.rfind(predicate)
758+
}
721759
}
722760

723761
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)