pub mod prelude {
#[cfg(not(target_arch = "wasm32"))]
pub use rayon::prelude::*;
#[cfg(target_arch = "wasm32")]
pub use super::serial::*;
}
#[cfg(target_arch = "wasm32")]
pub mod serial {
pub trait IntoParallelIterator {
type Iter;
fn into_par_iter(self) -> Self::Iter;
}
impl IntoParallelIterator for core::ops::Range<usize> {
type Iter = Self;
#[inline]
fn into_par_iter(self) -> Self {
self
}
}
pub trait ParallelSlice<T> {
fn par_chunks(&self, n: usize) -> core::slice::Chunks<'_, T>;
}
impl<T> ParallelSlice<T> for [T] {
#[inline]
fn par_chunks(&self, n: usize) -> core::slice::Chunks<'_, T> {
self.chunks(n)
}
}
pub trait ForEachInit: Iterator + Sized {
#[inline]
fn for_each_init<T, INIT, OP>(self, init: INIT, mut op: OP)
where
INIT: Fn() -> T,
OP: FnMut(&mut T, Self::Item),
{
let mut state = init();
for item in self {
op(&mut state, item);
}
}
}
impl<I: Iterator> ForEachInit for I {}
pub trait ParallelSliceMut<T> {
fn par_chunks_mut(&mut self, n: usize) -> core::slice::ChunksMut<'_, T>;
}
impl<T> ParallelSliceMut<T> for [T] {
#[inline]
fn par_chunks_mut(&mut self, n: usize) -> core::slice::ChunksMut<'_, T> {
self.chunks_mut(n)
}
}
}
#[inline]
#[must_use]
pub fn current_thread_index() -> Option<usize> {
#[cfg(not(target_arch = "wasm32"))]
{
rayon::current_thread_index()
}
#[cfg(target_arch = "wasm32")]
{
None
}
}
#[inline]
#[must_use]
pub fn current_num_threads() -> usize {
#[cfg(not(target_arch = "wasm32"))]
{
rayon::current_num_threads()
}
#[cfg(target_arch = "wasm32")]
{
1
}
}