Skip to main content

qsm_core/
par.rs

1//! Conditional parallelism macros.
2//!
3//! When the `parallel` feature is enabled, these macros expand to rayon's
4//! parallel iterators. Without the feature, they expand to standard iterators.
5//! This allows algorithm code to use `maybe_par_iter!(data)` and get parallelism
6//! on native targets while remaining single-threaded for WASM.
7
8#[cfg(feature = "parallel")]
9pub use rayon::prelude::*;
10
11/// Parallel or sequential immutable iterator over a slice.
12#[cfg(feature = "parallel")]
13#[macro_export]
14macro_rules! maybe_par_iter {
15    ($slice:expr) => {
16        $slice.par_iter()
17    };
18}
19
20#[cfg(not(feature = "parallel"))]
21#[macro_export]
22macro_rules! maybe_par_iter {
23    ($slice:expr) => {
24        $slice.iter()
25    };
26}
27
28/// Parallel or sequential mutable iterator over a slice.
29#[cfg(feature = "parallel")]
30#[macro_export]
31macro_rules! maybe_par_iter_mut {
32    ($slice:expr) => {
33        $slice.par_iter_mut()
34    };
35}
36
37#[cfg(not(feature = "parallel"))]
38#[macro_export]
39macro_rules! maybe_par_iter_mut {
40    ($slice:expr) => {
41        $slice.iter_mut()
42    };
43}
44
45/// Parallel or sequential chunks iterator.
46#[cfg(feature = "parallel")]
47#[macro_export]
48macro_rules! maybe_par_chunks_mut {
49    ($slice:expr, $chunk_size:expr) => {
50        $slice.par_chunks_mut($chunk_size)
51    };
52}
53
54#[cfg(not(feature = "parallel"))]
55#[macro_export]
56macro_rules! maybe_par_chunks_mut {
57    ($slice:expr, $chunk_size:expr) => {
58        $slice.chunks_mut($chunk_size)
59    };
60}