Skip to content

Commit 715b556

Browse files
Use Enumerate inside Positions
1 parent 0fc4675 commit 715b556

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/adaptors/mod.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use self::multi_product::*;
1616

1717
use crate::size_hint::{self, SizeHint};
1818
use std::fmt;
19-
use std::iter::{FromIterator, Fuse, FusedIterator};
19+
use std::iter::{Enumerate, FromIterator, Fuse, FusedIterator};
2020
use std::marker::PhantomData;
2121

2222
/// An iterator adaptor that alternates elements from two iterators until both
@@ -1039,16 +1039,15 @@ where
10391039
#[derive(Clone)]
10401040
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
10411041
pub struct Positions<I, F> {
1042-
iter: I,
1042+
iter: Enumerate<I>,
10431043
f: F,
1044-
count: usize,
10451044
}
10461045

10471046
impl<I, F> fmt::Debug for Positions<I, F>
10481047
where
10491048
I: fmt::Debug,
10501049
{
1051-
debug_fmt_fields!(Positions, iter, count);
1050+
debug_fmt_fields!(Positions, iter);
10521051
}
10531052

10541053
/// Create a new `Positions` iterator.
@@ -1057,7 +1056,8 @@ where
10571056
I: Iterator,
10581057
F: FnMut(I::Item) -> bool,
10591058
{
1060-
Positions { iter, f, count: 0 }
1059+
let iter = iter.enumerate();
1060+
Positions { iter, f }
10611061
}
10621062

10631063
impl<I, F> Iterator for Positions<I, F>
@@ -1068,14 +1068,10 @@ where
10681068
type Item = usize;
10691069

10701070
fn next(&mut self) -> Option<Self::Item> {
1071-
while let Some(v) = self.iter.next() {
1072-
let i = self.count;
1073-
self.count = i + 1;
1074-
if (self.f)(v) {
1075-
return Some(i);
1076-
}
1077-
}
1078-
None
1071+
let f = &mut self.f;
1072+
// TODO: once MSRV >= 1.62, use `then_some`.
1073+
self.iter
1074+
.find_map(|(count, val)| if f(val) { Some(count) } else { None })
10791075
}
10801076

10811077
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -1086,13 +1082,11 @@ where
10861082
where
10871083
G: FnMut(B, Self::Item) -> B,
10881084
{
1089-
let mut count = self.count;
10901085
let mut f = self.f;
1091-
self.iter.fold(init, |mut acc, val| {
1086+
self.iter.fold(init, |mut acc, (count, val)| {
10921087
if f(val) {
10931088
acc = func(acc, count);
10941089
}
1095-
count += 1;
10961090
acc
10971091
})
10981092
}
@@ -1104,22 +1098,20 @@ where
11041098
F: FnMut(I::Item) -> bool,
11051099
{
11061100
fn next_back(&mut self) -> Option<Self::Item> {
1107-
while let Some(v) = self.iter.next_back() {
1108-
if (self.f)(v) {
1109-
return Some(self.count + self.iter.len());
1110-
}
1111-
}
1112-
None
1101+
let f = &mut self.f;
1102+
// TODO: once MSRV >= 1.62, use `then_some`.
1103+
self.iter
1104+
.by_ref()
1105+
.rev()
1106+
.find_map(|(count, val)| if f(val) { Some(count) } else { None })
11131107
}
11141108

11151109
fn rfold<B, G>(self, init: B, mut func: G) -> B
11161110
where
11171111
G: FnMut(B, Self::Item) -> B,
11181112
{
1119-
let mut count = self.count + self.iter.len();
11201113
let mut f = self.f;
1121-
self.iter.rfold(init, |mut acc, val| {
1122-
count -= 1;
1114+
self.iter.rfold(init, |mut acc, (count, val)| {
11231115
if f(val) {
11241116
acc = func(acc, count);
11251117
}

0 commit comments

Comments
 (0)