mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 20:37:15 +02:00
Merge branch 'main' into indexer-edition-2024
This commit is contained in:
commit
10feeb88f2
1122 changed files with 6265 additions and 5265 deletions
33
crates/milli/src/update/new/parallel_iterator_ext.rs
Normal file
33
crates/milli/src/update/new/parallel_iterator_ext.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use rayon::iter::ParallelIterator;
|
||||
|
||||
pub trait ParallelIteratorExt: ParallelIterator {
|
||||
/// A method to run a closure of all the items and return an owned error.
|
||||
///
|
||||
/// The init function is ran only as necessary which is basically once by thread.
|
||||
fn try_arc_for_each_try_init<F, INIT, T, E>(self, init: INIT, op: F) -> Result<(), E>
|
||||
where
|
||||
E: Send + Sync,
|
||||
F: Fn(&mut T, Self::Item) -> Result<(), Arc<E>> + Sync + Send + Clone,
|
||||
INIT: Fn() -> Result<T, E> + Sync + Send + Clone,
|
||||
{
|
||||
let result = self.try_for_each_init(
|
||||
move || match init() {
|
||||
Ok(t) => Ok(t),
|
||||
Err(err) => Err(Arc::new(err)),
|
||||
},
|
||||
move |result, item| match result {
|
||||
Ok(t) => op(t, item),
|
||||
Err(err) => Err(err.clone()),
|
||||
},
|
||||
);
|
||||
|
||||
match result {
|
||||
Ok(()) => Ok(()),
|
||||
Err(err) => Err(Arc::into_inner(err).expect("the error must be only owned by us")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ParallelIterator> ParallelIteratorExt for T {}
|
Loading…
Add table
Add a link
Reference in a new issue