Skip to content

Commit 0f82c0c

Browse files
committed
Reduce genericity in Take
1 parent 46a62ca commit 0f82c0c

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -1782,19 +1782,26 @@ impl<I> Iterator for Take<I> where I: Iterator{
17821782
}
17831783

17841784
#[inline]
1785-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
1785+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
17861786
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
17871787
{
1788-
if self.n == 0 {
1789-
Try::from_ok(init)
1790-
} else {
1791-
let n = &mut self.n;
1792-
self.iter.try_fold(init, move |acc, x| {
1788+
fn check<'a, T, Acc, R: Try<Ok = Acc>>(
1789+
n: &'a mut usize,
1790+
mut fold: impl FnMut(Acc, T) -> R + 'a,
1791+
) -> impl FnMut(Acc, T) -> LoopState<Acc, R> + 'a {
1792+
move |acc, x| {
17931793
*n -= 1;
17941794
let r = fold(acc, x);
17951795
if *n == 0 { LoopState::Break(r) }
17961796
else { LoopState::from_try(r) }
1797-
}).into_try()
1797+
}
1798+
}
1799+
1800+
if self.n == 0 {
1801+
Try::from_ok(init)
1802+
} else {
1803+
let n = &mut self.n;
1804+
self.iter.try_fold(init, check(n, fold)).into_try()
17981805
}
17991806
}
18001807
}

0 commit comments

Comments
 (0)