From 3984914affa48bb6a65a68844a0ddff498c30512 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 3 Nov 2023 09:17:16 -0700 Subject: [PATCH] Use `filter_map` in `try_par_for_each_in` This simplifies the expression, especially for the rayon part, and also lets us drop the `E: Copy` constraint. --- compiler/rustc_data_structures/src/sync/parallel.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_data_structures/src/sync/parallel.rs b/compiler/rustc_data_structures/src/sync/parallel.rs index 39dddb5956911..5695d839d3eb1 100644 --- a/compiler/rustc_data_structures/src/sync/parallel.rs +++ b/compiler/rustc_data_structures/src/sync/parallel.rs @@ -77,12 +77,12 @@ mod disabled { }) } - pub fn try_par_for_each_in( + pub fn try_par_for_each_in( t: T, mut for_each: impl FnMut(T::Item) -> Result<(), E>, ) -> Result<(), E> { parallel_guard(|guard| { - t.into_iter().fold(Ok(()), |ret, i| guard.run(|| for_each(i)).unwrap_or(ret).and(ret)) + t.into_iter().filter_map(|i| guard.run(|| for_each(i))).fold(Ok(()), Result::and) }) } @@ -178,7 +178,7 @@ mod enabled { pub fn try_par_for_each_in< T: IntoIterator + IntoParallelIterator::Item>, - E: Copy + Send, + E: Send, >( t: T, for_each: impl Fn(::Item) -> Result<(), E> + DynSync + DynSend, @@ -187,11 +187,10 @@ mod enabled { if mode::is_dyn_thread_safe() { let for_each = FromDyn::from(for_each); t.into_par_iter() - .fold_with(Ok(()), |ret, i| guard.run(|| for_each(i)).unwrap_or(ret).and(ret)) - .reduce(|| Ok(()), |a, b| a.and(b)) + .filter_map(|i| guard.run(|| for_each(i))) + .reduce(|| Ok(()), Result::and) } else { - t.into_iter() - .fold(Ok(()), |ret, i| guard.run(|| for_each(i)).unwrap_or(ret).and(ret)) + t.into_iter().filter_map(|i| guard.run(|| for_each(i))).fold(Ok(()), Result::and) } }) }