Skip to main content

map_in_lanes

Function map_in_lanes 

Source
pub fn map_in_lanes<L, T, U, E, F>(
    lanes: &mut L,
    items: Vec<T>,
    f: F,
) -> Result<Vec<U>, MapInError>
where L: Lanes, T: Send, U: Send, E: Display + Send, F: Fn(T, &mut L::Lane<'_>) -> Result<U, E> + Sync,
Expand description

Maps items through a Lanes executor with LocalMode::Outer semantics.

Every input is evaluated exactly once, results keep input order, and the lowest failing input index is reported after every item has finished. Lane callbacks pull the next input index from one shared counter and write their result into the per-index slot, so no Rayon job and no extra heap allocation is created per item. A callback panic resumes only after every lane has been joined.

§Errors

Returns MapInError::Callback with the lowest failing input index, or MapInError::Executor when the lane executor itself cannot run or join a lane.

§Example

use hataori::{map_in_lanes, Domain, RayonLanes};
use std::sync::Arc;

let pool = Arc::new(rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap());
let domain = Domain::external(Arc::clone(&pool), vec![0, 1], 2).unwrap();
let mut lanes = RayonLanes::new(&domain).unwrap();
let tripled = map_in_lanes(&mut lanes, vec![2, 4], |item, ()| {
    Ok::<_, std::convert::Infallible>(item * 3)
})
.unwrap();
assert_eq!(tripled, vec![6, 12]);