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 fans out through RayonLanes and map_in_lanes: every input is evaluated exactly once, result order is preserved, and the lowest callback-error index is reported only after all callbacks have finished.

§Errors

Returns MapInError::MissingPool, MapInError::ForeignPool, or MapInError::DomainBusy for the preconditions above, MapInError::Callback with the lowest failing input index when a callback fails, and MapInError::Executor when the lane executor cannot run.

§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]);