Skip to content

Commit 6e60205

Browse files
committed
Reduce genericity in Inspect
1 parent 7ea5e72 commit 6e60205

File tree

1 file changed

+22
-12
lines changed
  • src/libcore/iter/adapters

1 file changed

+22
-12
lines changed

src/libcore/iter/adapters/mod.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,20 @@ impl<I: Iterator, F> Inspect<I, F> where F: FnMut(&I::Item) {
21032103
}
21042104
}
21052105

2106+
fn inspect_fold<T, Acc>(
2107+
mut f: impl FnMut(&T),
2108+
mut fold: impl FnMut(Acc, T) -> Acc,
2109+
) -> impl FnMut(Acc, T) -> Acc {
2110+
move |acc, item| { f(&item); fold(acc, item) }
2111+
}
2112+
2113+
fn inspect_try_fold<'a, T, Acc, R>(
2114+
f: &'a mut impl FnMut(&T),
2115+
mut fold: impl FnMut(Acc, T) -> R + 'a,
2116+
) -> impl FnMut(Acc, T) -> R + 'a {
2117+
move |acc, item| { f(&item); fold(acc, item) }
2118+
}
2119+
21062120
#[stable(feature = "rust1", since = "1.0.0")]
21072121
impl<I: Iterator, F> Iterator for Inspect<I, F> where F: FnMut(&I::Item) {
21082122
type Item = I::Item;
@@ -2119,19 +2133,17 @@ impl<I: Iterator, F> Iterator for Inspect<I, F> where F: FnMut(&I::Item) {
21192133
}
21202134

21212135
#[inline]
2122-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
2136+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
21232137
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
21242138
{
2125-
let f = &mut self.f;
2126-
self.iter.try_fold(init, move |acc, item| { f(&item); fold(acc, item) })
2139+
self.iter.try_fold(init, inspect_try_fold(&mut self.f, fold))
21272140
}
21282141

21292142
#[inline]
2130-
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
2143+
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
21312144
where Fold: FnMut(Acc, Self::Item) -> Acc,
21322145
{
2133-
let mut f = self.f;
2134-
self.iter.fold(init, move |acc, item| { f(&item); fold(acc, item) })
2146+
self.iter.fold(init, inspect_fold(self.f, fold))
21352147
}
21362148
}
21372149

@@ -2146,19 +2158,17 @@ impl<I: DoubleEndedIterator, F> DoubleEndedIterator for Inspect<I, F>
21462158
}
21472159

21482160
#[inline]
2149-
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
2161+
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
21502162
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
21512163
{
2152-
let f = &mut self.f;
2153-
self.iter.try_rfold(init, move |acc, item| { f(&item); fold(acc, item) })
2164+
self.iter.try_rfold(init, inspect_try_fold(&mut self.f, fold))
21542165
}
21552166

21562167
#[inline]
2157-
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
2168+
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
21582169
where Fold: FnMut(Acc, Self::Item) -> Acc,
21592170
{
2160-
let mut f = self.f;
2161-
self.iter.rfold(init, move |acc, item| { f(&item); fold(acc, item) })
2171+
self.iter.rfold(init, inspect_fold(self.f, fold))
21622172
}
21632173
}
21642174

0 commit comments

Comments
 (0)