Skip to content

Commit fc4d037

Browse files
committed
Reduce genericity in Inspect
1 parent f100354 commit fc4d037

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

+22-12
Original file line numberDiff line numberDiff line change
@@ -2220,6 +2220,20 @@ impl<I: Iterator, F> Inspect<I, F> where F: FnMut(&I::Item) {
22202220
}
22212221
}
22222222

2223+
fn inspect_fold<T, Acc>(
2224+
mut f: impl FnMut(&T),
2225+
mut fold: impl FnMut(Acc, T) -> Acc,
2226+
) -> impl FnMut(Acc, T) -> Acc {
2227+
move |acc, item| { f(&item); fold(acc, item) }
2228+
}
2229+
2230+
fn inspect_try_fold<'a, T, Acc, R>(
2231+
f: &'a mut impl FnMut(&T),
2232+
mut fold: impl FnMut(Acc, T) -> R + 'a,
2233+
) -> impl FnMut(Acc, T) -> R + 'a {
2234+
move |acc, item| { f(&item); fold(acc, item) }
2235+
}
2236+
22232237
#[stable(feature = "rust1", since = "1.0.0")]
22242238
impl<I: Iterator, F> Iterator for Inspect<I, F> where F: FnMut(&I::Item) {
22252239
type Item = I::Item;
@@ -2236,19 +2250,17 @@ impl<I: Iterator, F> Iterator for Inspect<I, F> where F: FnMut(&I::Item) {
22362250
}
22372251

22382252
#[inline]
2239-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
2253+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
22402254
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
22412255
{
2242-
let f = &mut self.f;
2243-
self.iter.try_fold(init, move |acc, item| { f(&item); fold(acc, item) })
2256+
self.iter.try_fold(init, inspect_try_fold(&mut self.f, fold))
22442257
}
22452258

22462259
#[inline]
2247-
fn fold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
2260+
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
22482261
where Fold: FnMut(Acc, Self::Item) -> Acc,
22492262
{
2250-
let mut f = self.f;
2251-
self.iter.fold(init, move |acc, item| { f(&item); fold(acc, item) })
2263+
self.iter.fold(init, inspect_fold(self.f, fold))
22522264
}
22532265
}
22542266

@@ -2263,19 +2275,17 @@ impl<I: DoubleEndedIterator, F> DoubleEndedIterator for Inspect<I, F>
22632275
}
22642276

22652277
#[inline]
2266-
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
2278+
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
22672279
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
22682280
{
2269-
let f = &mut self.f;
2270-
self.iter.try_rfold(init, move |acc, item| { f(&item); fold(acc, item) })
2281+
self.iter.try_rfold(init, inspect_try_fold(&mut self.f, fold))
22712282
}
22722283

22732284
#[inline]
2274-
fn rfold<Acc, Fold>(self, init: Acc, mut fold: Fold) -> Acc
2285+
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
22752286
where Fold: FnMut(Acc, Self::Item) -> Acc,
22762287
{
2277-
let mut f = self.f;
2278-
self.iter.rfold(init, move |acc, item| { f(&item); fold(acc, item) })
2288+
self.iter.rfold(init, inspect_fold(self.f, fold))
22792289
}
22802290
}
22812291

0 commit comments

Comments
 (0)