Skip to content

Commit 3984914

Browse files
committed
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.
1 parent a026bd4 commit 3984914

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

compiler/rustc_data_structures/src/sync/parallel.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ mod disabled {
7777
})
7878
}
7979

80-
pub fn try_par_for_each_in<T: IntoIterator, E: Copy>(
80+
pub fn try_par_for_each_in<T: IntoIterator, E>(
8181
t: T,
8282
mut for_each: impl FnMut(T::Item) -> Result<(), E>,
8383
) -> Result<(), E> {
8484
parallel_guard(|guard| {
85-
t.into_iter().fold(Ok(()), |ret, i| guard.run(|| for_each(i)).unwrap_or(ret).and(ret))
85+
t.into_iter().filter_map(|i| guard.run(|| for_each(i))).fold(Ok(()), Result::and)
8686
})
8787
}
8888

@@ -178,7 +178,7 @@ mod enabled {
178178

179179
pub fn try_par_for_each_in<
180180
T: IntoIterator + IntoParallelIterator<Item = <T as IntoIterator>::Item>,
181-
E: Copy + Send,
181+
E: Send,
182182
>(
183183
t: T,
184184
for_each: impl Fn(<T as IntoIterator>::Item) -> Result<(), E> + DynSync + DynSend,
@@ -187,11 +187,10 @@ mod enabled {
187187
if mode::is_dyn_thread_safe() {
188188
let for_each = FromDyn::from(for_each);
189189
t.into_par_iter()
190-
.fold_with(Ok(()), |ret, i| guard.run(|| for_each(i)).unwrap_or(ret).and(ret))
191-
.reduce(|| Ok(()), |a, b| a.and(b))
190+
.filter_map(|i| guard.run(|| for_each(i)))
191+
.reduce(|| Ok(()), Result::and)
192192
} else {
193-
t.into_iter()
194-
.fold(Ok(()), |ret, i| guard.run(|| for_each(i)).unwrap_or(ret).and(ret))
193+
t.into_iter().filter_map(|i| guard.run(|| for_each(i))).fold(Ok(()), Result::and)
195194
}
196195
})
197196
}

0 commit comments

Comments
 (0)