From 8737061cb59f2563153bdca3d121f40584597426 Mon Sep 17 00:00:00 2001 From: Andreas Jonson Date: Tue, 1 Oct 2019 07:56:18 +0200 Subject: [PATCH] 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 --- src/libcore/iter/traits/iterator.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index 0a9e076ec5869..a272035150a15 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -1859,14 +1859,13 @@ pub trait Iterator { Self: Sized, F: FnMut(Self::Item) -> bool { #[inline] - fn check(mut f: impl FnMut(T) -> bool) -> impl FnMut(T) -> LoopState<(), ()> { - move |x| { + fn check(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> LoopState<(), ()> { + move |(), x| { if f(x) { LoopState::Continue(()) } else { LoopState::Break(()) } } } - - self.try_for_each(check(f)) == LoopState::Continue(()) + self.try_fold((), check(f)) == LoopState::Continue(()) } /// Tests if any element of the iterator matches a predicate. @@ -1913,14 +1912,14 @@ pub trait Iterator { F: FnMut(Self::Item) -> bool { #[inline] - fn check(mut f: impl FnMut(T) -> bool) -> impl FnMut(T) -> LoopState<(), ()> { - move |x| { + fn check(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> LoopState<(), ()> { + move |(), x| { if f(x) { LoopState::Break(()) } else { LoopState::Continue(()) } } } - self.try_for_each(check(f)) == LoopState::Break(()) + self.try_fold((), check(f)) == LoopState::Break(()) } /// Searches for an element of an iterator that satisfies a predicate. @@ -1972,14 +1971,16 @@ pub trait Iterator { P: FnMut(&Self::Item) -> bool, { #[inline] - fn check(mut predicate: impl FnMut(&T) -> bool) -> impl FnMut(T) -> LoopState<(), T> { - move |x| { + fn check( + mut predicate: impl FnMut(&T) -> bool + ) -> impl FnMut((), T) -> LoopState<(), T> { + move |(), x| { if predicate(&x) { LoopState::Break(x) } else { LoopState::Continue(()) } } } - self.try_for_each(check(predicate)).break_value() + self.try_fold((), check(predicate)).break_value() } /// Applies function to the elements of iterator and returns @@ -2004,14 +2005,14 @@ pub trait Iterator { F: FnMut(Self::Item) -> Option, { #[inline] - fn check(mut f: impl FnMut(T) -> Option) -> impl FnMut(T) -> LoopState<(), B> { - move |x| match f(x) { + fn check(mut f: impl FnMut(T) -> Option) -> impl FnMut((), T) -> LoopState<(), B> { + move |(), x| match f(x) { Some(x) => LoopState::Break(x), None => LoopState::Continue(()), } } - self.try_for_each(check(f)).break_value() + self.try_fold((), check(f)).break_value() } /// Searches for an element in an iterator, returning its index.