Skip to content

Commit

Permalink
Rollup merge of rust-lang#41028 - bluss:rev-rfind, r=alexcrichton
Browse files Browse the repository at this point in the history
Let .rev()'s find use the underlying rfind and vice versa

- Connect the plumbing in an obvious way from Rev's find → underlying rfind and vice versa
- A style change in the provided implementation for Iterator::rfind, using simple next_back when it is enough
  • Loading branch information
Ariel Ben-Yehuda authored Apr 5, 2017
2 parents b712950 + 74f8ea2 commit 5e410ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,24 @@ impl<I> Iterator for Rev<I> where I: DoubleEndedIterator {
fn next(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next_back() }
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }

fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where P: FnMut(&Self::Item) -> bool
{
self.iter.rfind(predicate)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> DoubleEndedIterator for Rev<I> where I: DoubleEndedIterator {
#[inline]
fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next() }

fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where P: FnMut(&Self::Item) -> bool
{
self.iter.find(predicate)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/iter/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub trait DoubleEndedIterator: Iterator {
Self: Sized,
P: FnMut(&Self::Item) -> bool
{
for x in self.by_ref().rev() {
while let Some(x) = self.next_back() {
if predicate(&x) { return Some(x) }
}
None
Expand Down

0 comments on commit 5e410ba

Please sign in to comment.