Skip to content

Commit f100354

Browse files
committed
Reduce genericity in Scan
1 parent 0f82c0c commit f100354

File tree

1 file changed

+17
-8
lines changed
  • src/libcore/iter/adapters

1 file changed

+17
-8
lines changed

src/libcore/iter/adapters/mod.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,8 @@ impl<B, I, St, F> Iterator for Scan<I, St, F> where
19001900

19011901
#[inline]
19021902
fn next(&mut self) -> Option<B> {
1903-
self.iter.next().and_then(|a| (self.f)(&mut self.state, a))
1903+
let a = self.iter.next()?;
1904+
(self.f)(&mut self.state, a)
19041905
}
19051906

19061907
#[inline]
@@ -1910,17 +1911,25 @@ impl<B, I, St, F> Iterator for Scan<I, St, F> where
19101911
}
19111912

19121913
#[inline]
1913-
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
1914+
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R where
19141915
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
19151916
{
1917+
fn scan<'a, T, St, B, Acc, R: Try<Ok = Acc>>(
1918+
state: &'a mut St,
1919+
f: &'a mut impl FnMut(&mut St, T) -> Option<B>,
1920+
mut fold: impl FnMut(Acc, B) -> R + 'a,
1921+
) -> impl FnMut(Acc, T) -> LoopState<Acc, R> + 'a {
1922+
move |acc, x| {
1923+
match f(state, x) {
1924+
None => LoopState::Break(Try::from_ok(acc)),
1925+
Some(x) => LoopState::from_try(fold(acc, x)),
1926+
}
1927+
}
1928+
}
1929+
19161930
let state = &mut self.state;
19171931
let f = &mut self.f;
1918-
self.iter.try_fold(init, move |acc, x| {
1919-
match f(state, x) {
1920-
None => LoopState::Break(Try::from_ok(acc)),
1921-
Some(x) => LoopState::from_try(fold(acc, x)),
1922-
}
1923-
}).into_try()
1932+
self.iter.try_fold(init, scan(state, f, fold)).into_try()
19241933
}
19251934
}
19261935

0 commit comments

Comments
 (0)