| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// Copyright by contributors to this project. |
| 3 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 4 |
|
| 5 |
#[cfg(all(not(mls_build_async), feature = "rayon"))] |
| 6 |
mod sync_rayon { |
| 7 |
use rayon::{ |
| 8 |
iter::IterBridge, |
| 9 |
prelude::{FromParallelIterator, IntoParallelIterator, ParallelBridge, ParallelIterator}, |
| 10 |
}; |
| 11 |
|
| 12 |
pub fn wrap_iter<I>(it: I) -> I::Iter |
| 13 |
where |
| 14 |
I: IntoParallelIterator, |
| 15 |
{ |
| 16 |
it.into_par_iter() |
| 17 |
} |
| 18 |
|
| 19 |
pub fn wrap_impl_iter<I>(it: I) -> IterBridge<I::IntoIter> |
| 20 |
where |
| 21 |
I: IntoIterator, |
| 22 |
I::IntoIter: Send, |
| 23 |
I::Item: Send, |
| 24 |
{ |
| 25 |
it.into_iter().par_bridge() |
| 26 |
} |
| 27 |
|
| 28 |
pub trait ParallelIteratorExt { |
| 29 |
type Ok: Send; |
| 30 |
type Error: Send; |
| 31 |
|
| 32 |
fn try_collect<A>(self) -> Result<A, Self::Error> |
| 33 |
where |
| 34 |
A: FromParallelIterator<Self::Ok>; |
| 35 |
} |
| 36 |
|
| 37 |
impl<I, T, E> ParallelIteratorExt for I |
| 38 |
where |
| 39 |
I: ParallelIterator<Item = Result<T, E>>, |
| 40 |
T: Send, |
| 41 |
E: Send, |
| 42 |
{ |
| 43 |
type Ok = T; |
| 44 |
type Error = E; |
| 45 |
|
| 46 |
fn try_collect<A>(self) -> Result<A, Self::Error> |
| 47 |
where |
| 48 |
A: FromParallelIterator<Self::Ok>, |
| 49 |
{ |
| 50 |
self.collect() |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#[cfg(all(not(mls_build_async), feature = "rayon"))] |
| 56 |
pub use sync_rayon::{wrap_impl_iter, wrap_iter, ParallelIteratorExt}; |
| 57 |
|
| 58 |
#[cfg(not(any(mls_build_async, feature = "rayon")))] |
| 59 |
mod sync { |
| 60 |
pub fn wrap_iter<I>(it: I) -> I::IntoIter |
| 61 |
where |
| 62 |
I: IntoIterator, |
| 63 |
{ |
| 64 |
it.into_iter() |
| 65 |
} |
| 66 |
|
| 67 |
pub fn wrap_impl_iter<I>(it: I) -> I::IntoIter |
| 68 |
where |
| 69 |
I: IntoIterator, |
| 70 |
{ |
| 71 |
it.into_iter() |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
#[cfg(not(any(mls_build_async, feature = "rayon")))] |
| 76 |
pub use sync::{wrap_impl_iter, wrap_iter}; |
| 77 |
|
| 78 |
#[cfg(mls_build_async)] |
| 79 |
mod async_ { |
| 80 |
pub fn wrap_iter<I>(it: I) -> futures::stream::Iter<I::IntoIter> |
| 81 |
where |
| 82 |
I: IntoIterator, |
| 83 |
{ |
| 84 |
futures::stream::iter(it) |
| 85 |
} |
| 86 |
|
| 87 |
pub fn wrap_impl_iter<I>(it: I) -> futures::stream::Iter<I::IntoIter> |
| 88 |
where |
| 89 |
I: IntoIterator, |
| 90 |
{ |
| 91 |
futures::stream::iter(it) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(mls_build_async)] |
| 96 |
pub use async_::{wrap_impl_iter, wrap_iter}; |