Skip to content

Commit 8737061

Browse files
committed
replace try_for_each with try_fold to generate less code
removes two functions to inline by combining the check functions and extra call to try_for_each
1 parent 22bc9e1 commit 8737061

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/libcore/iter/traits/iterator.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -1859,14 +1859,13 @@ pub trait Iterator {
18591859
Self: Sized, F: FnMut(Self::Item) -> bool
18601860
{
18611861
#[inline]
1862-
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut(T) -> LoopState<(), ()> {
1863-
move |x| {
1862+
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> LoopState<(), ()> {
1863+
move |(), x| {
18641864
if f(x) { LoopState::Continue(()) }
18651865
else { LoopState::Break(()) }
18661866
}
18671867
}
1868-
1869-
self.try_for_each(check(f)) == LoopState::Continue(())
1868+
self.try_fold((), check(f)) == LoopState::Continue(())
18701869
}
18711870

18721871
/// Tests if any element of the iterator matches a predicate.
@@ -1913,14 +1912,14 @@ pub trait Iterator {
19131912
F: FnMut(Self::Item) -> bool
19141913
{
19151914
#[inline]
1916-
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut(T) -> LoopState<(), ()> {
1917-
move |x| {
1915+
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> LoopState<(), ()> {
1916+
move |(), x| {
19181917
if f(x) { LoopState::Break(()) }
19191918
else { LoopState::Continue(()) }
19201919
}
19211920
}
19221921

1923-
self.try_for_each(check(f)) == LoopState::Break(())
1922+
self.try_fold((), check(f)) == LoopState::Break(())
19241923
}
19251924

19261925
/// Searches for an element of an iterator that satisfies a predicate.
@@ -1972,14 +1971,16 @@ pub trait Iterator {
19721971
P: FnMut(&Self::Item) -> bool,
19731972
{
19741973
#[inline]
1975-
fn check<T>(mut predicate: impl FnMut(&T) -> bool) -> impl FnMut(T) -> LoopState<(), T> {
1976-
move |x| {
1974+
fn check<T>(
1975+
mut predicate: impl FnMut(&T) -> bool
1976+
) -> impl FnMut((), T) -> LoopState<(), T> {
1977+
move |(), x| {
19771978
if predicate(&x) { LoopState::Break(x) }
19781979
else { LoopState::Continue(()) }
19791980
}
19801981
}
19811982

1982-
self.try_for_each(check(predicate)).break_value()
1983+
self.try_fold((), check(predicate)).break_value()
19831984
}
19841985

19851986
/// Applies function to the elements of iterator and returns
@@ -2004,14 +2005,14 @@ pub trait Iterator {
20042005
F: FnMut(Self::Item) -> Option<B>,
20052006
{
20062007
#[inline]
2007-
fn check<T, B>(mut f: impl FnMut(T) -> Option<B>) -> impl FnMut(T) -> LoopState<(), B> {
2008-
move |x| match f(x) {
2008+
fn check<T, B>(mut f: impl FnMut(T) -> Option<B>) -> impl FnMut((), T) -> LoopState<(), B> {
2009+
move |(), x| match f(x) {
20092010
Some(x) => LoopState::Break(x),
20102011
None => LoopState::Continue(()),
20112012
}
20122013
}
20132014

2014-
self.try_for_each(check(f)).break_value()
2015+
self.try_fold((), check(f)).break_value()
20152016
}
20162017

20172018
/// Searches for an element in an iterator, returning its index.

0 commit comments

Comments
 (0)