hyperplonk/util/src/lib.rs
util/src/lib.rsRaw
1// Copyright (c) 2023 Espresso Systems (espressosys.com)
2// This file is part of the HyperPlonk library.
3
4// You should have received a copy of the MIT License
5// along with the HyperPlonk library. If not, see <https://mit-license.org/>.
6
7//! Utilities for parallel code.
8
9/// this function helps with slice iterator creation that optionally use
10/// `par_iter()` when feature flag `parallel` is on.
11///
12/// # Usage
13/// let v = [1, 2, 3, 4, 5];
14/// let sum = parallelizable_slice_iter(&v).sum();
15///
16/// // the above code is a shorthand for (thus equivalent to)
17/// #[cfg(feature = "parallel")]
18/// let sum = v.par_iter().sum();
19/// #[cfg(not(feature = "parallel"))]
20/// let sum = v.iter().sum();
21#[cfg(feature = "parallel")]
22pub fn parallelizable_slice_iter<T: Sync>(data: &[T]) -> rayon::slice::Iter<T> {
23 use rayon::iter::IntoParallelIterator;
24 data.into_par_iter()
25}
26
27#[cfg(not(feature = "parallel"))]
28pub fn parallelizable_slice_iter<T>(data: &[T]) -> core::slice::Iter<T> {
29 data.iter()
30}