Skip to content

Commit ce027cf

Browse files
committed
add Iterator::fold to MapDeref and FilterMapDeref
1 parent 7045918 commit ce027cf

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,19 @@ where
11071107

11081108
None
11091109
}
1110+
1111+
#[inline]
1112+
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1113+
where
1114+
Self: Sized,
1115+
Fold: FnMut(Acc, Self::Item) -> Acc,
1116+
{
1117+
let mut map = self.f;
1118+
self.it.fold(init, move |acc, item| match map(item) {
1119+
Some(mapped) => f(acc, mapped),
1120+
None => acc,
1121+
})
1122+
}
11101123
}
11111124

11121125
impl<I, B, F> DoubleEndedIterator for FilterMapDeref<I, F>
@@ -1375,6 +1388,16 @@ where
13751388
fn size_hint(&self) -> (usize, Option<usize>) {
13761389
self.it.size_hint()
13771390
}
1391+
1392+
#[inline]
1393+
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1394+
where
1395+
Self: Sized,
1396+
Fold: FnMut(Acc, Self::Item) -> Acc,
1397+
{
1398+
let mut map = self.f;
1399+
self.it.fold(init, move |acc, item| f(acc, map(item)))
1400+
}
13781401
}
13791402

13801403
impl<I, B, F> DoubleEndedIterator for MapDeref<I, F>

0 commit comments

Comments
 (0)