Skip to content

Commit 9e58fcc

Browse files
committed
Reduce genericity in Take
1 parent f365574 commit 9e58fcc

File tree

1 file changed

+14
-7
lines changed
  • src/libcore/iter/adapters

1 file changed

+14
-7
lines changed

src/libcore/iter/adapters/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,19 +1710,26 @@ impl<I> Iterator for Take<I> where I: Iterator{
17101710
}
17111711

17121712
#[inline]
1713-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
1713+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
17141714
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
17151715
{
1716-
if self.n == 0 {
1717-
Try::from_ok(init)
1718-
} else {
1719-
let n = &mut self.n;
1720-
self.iter.try_fold(init, move |acc, x| {
1716+
fn check<'a, T, Acc, R: Try<Ok = Acc>>(
1717+
n: &'a mut usize,
1718+
mut fold: impl FnMut(Acc, T) -> R + 'a,
1719+
) -> impl FnMut(Acc, T) -> LoopState<Acc, R> + 'a {
1720+
move |acc, x| {
17211721
*n -= 1;
17221722
let r = fold(acc, x);
17231723
if *n == 0 { LoopState::Break(r) }
17241724
else { LoopState::from_try(r) }
1725-
}).into_try()
1725+
}
1726+
}
1727+
1728+
if self.n == 0 {
1729+
Try::from_ok(init)
1730+
} else {
1731+
let n = &mut self.n;
1732+
self.iter.try_fold(init, check(n, fold)).into_try()
17261733
}
17271734
}
17281735
}

0 commit comments

Comments
 (0)