Skip to main content

Fanout

Trait Fanout 

Source
pub trait Fanout: Sync {
    // Required methods
    fn workers(&self) -> usize;
    fn for_each<T, F>(&self, items: &mut [T], body: F)
       where T: Send,
             F: Fn(&mut T) + Send + Sync;

    // Provided method
    fn scope<R, F>(&self, work: F) -> R
       where F: FnOnce() -> R + Send,
             R: Send { ... }
}
Expand description

A caller’s way of running independent work at the same time.

The simulation asks for one of these rather than depending on a scheduler, so the same step runs on a thread pool, on one thread, or on a host that has no threads at all.

§Examples

use concinnity_physics::{Fanout, Inline};

let mut work = [1u32, 2, 3];
Inline.for_each(&mut work, |item| *item *= 10);
assert_eq!(work, [10, 20, 30]);
assert_eq!(Inline.workers(), 1);

Required Methods§

Source

fn workers(&self) -> usize

Units of work this fan-out can run at once. One means the work runs on the calling thread, which is what the simulation sizes its per-worker scratch against.

Source

fn for_each<T, F>(&self, items: &mut [T], body: F)
where T: Send, F: Fn(&mut T) + Send + Sync,

Run body over every item and return once all of them are done.

Items are independent, so an implementation may visit them in any order and on any thread. The simulation never lets the result depend on which one did what.

Provided Methods§

Source

fn scope<R, F>(&self, work: F) -> R
where F: FnOnce() -> R + Send, R: Send,

Run work with this fan-out’s workers already gathered, and return what it produced.

Everything a step hands out happens inside one of these. An implementation backed by a thread pool enters it here, so the Fanout::for_each calls inside are already there; one with no pool to enter leaves this as it is and runs work where it stands.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§