Skip to main content

map_in

Function map_in 

Source
pub fn map_in<T, U, E, F>(
    domain: &Domain,
    mode: LocalMode,
    items: Vec<T>,
    f: F,
) -> Result<Vec<U>, MapInError>
where T: Send, U: Send, E: Display + Send, F: Fn(T) -> Result<U, E> + Send + Sync,
Expand description

Maps items through an explicit Rayon domain.

The target domain must have a pool, the call must not originate in another Rayon pool, and the domain must not already be admitted. These preconditions are checked in that order, before f is called. The callback and input, output, and error types must satisfy Send/Sync as required by Rayon, but none needs to be 'static.

LocalMode::Sequential and LocalMode::Inner run callbacks one at a time in the target pool and stop at the first callback error. Inner leaves nested same-pool Rayon work available. LocalMode::Outer evaluates every input exactly once in parallel, preserves result order, and reports the lowest callback-error index only after all callbacks have finished.

ยงExample

use hataori::{map_in, Domain, LocalMode};
use std::sync::Arc;
use rayon::ThreadPoolBuilder;

let pool = Arc::new(ThreadPoolBuilder::new().num_threads(2).build().unwrap());
let domain = Domain::external(Arc::clone(&pool), vec![0, 1], 2).unwrap();
let result = map_in(&domain, LocalMode::Outer, vec![1, 2, 3], |item| {
    Ok::<_, std::convert::Infallible>(item * 2)
}).unwrap();
assert_eq!(result, vec![2, 4, 6]);