mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-03 18:15:46 +01:00
Add ParallelIteratorExt
This commit is contained in:
parent
bb7a503e5d
commit
5b776556fe
@ -1,4 +1,36 @@
|
||||
use crossbeam_channel::{Receiver, Sender, TryRecvError};
|
||||
use rayon::iter::{MapInit, ParallelIterator};
|
||||
|
||||
pub trait ParallelIteratorExt: ParallelIterator {
|
||||
fn try_map_try_init<F, INIT, T, E, R>(
|
||||
self,
|
||||
init: INIT,
|
||||
map_op: F,
|
||||
) -> MapInit<
|
||||
Self,
|
||||
impl Fn() -> Result<T, Option<E>> + Sync + Send + Clone,
|
||||
impl Fn(&mut Result<T, Option<E>>, Self::Item) -> Result<R, Option<E>> + Sync + Send + Clone,
|
||||
>
|
||||
where
|
||||
E: Send,
|
||||
F: Fn(&mut T, Self::Item) -> Result<R, E> + Sync + Send + Clone,
|
||||
INIT: Fn() -> Result<T, E> + Sync + Send + Clone,
|
||||
R: Send,
|
||||
{
|
||||
self.map_init(
|
||||
move || match init() {
|
||||
Ok(t) => Ok(t),
|
||||
Err(err) => Err(Some(err)),
|
||||
},
|
||||
move |maybe_t, item| match maybe_t {
|
||||
Ok(t) => map_op(t, item).map_err(Some),
|
||||
Err(maybe_err) => Err(maybe_err.take()),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ParallelIterator> ParallelIteratorExt for T {}
|
||||
|
||||
/// A pool of items that can be pull and generated on demand.
|
||||
pub struct ItemsPool<F, T, E>
|
||||
|
Loading…
Reference in New Issue
Block a user