Skip to main content

Use

Trait Use 

Source
pub trait Use: Sync {
    type Item: Send;

    // Required methods
    unsafe fn init_get(&self, thread_idx: usize) -> &mut Self::Item;
    fn get(&mut self, thread_idx: usize) -> &mut Self::Item;
    fn max_threads(&self) -> Option<usize>;
}
Expand description

Worker-local mutable state used by parallel iterators.

A Use value provides one mutable slot per participating worker thread. Parallel combinators initialize the slot for a thread on first access and then reuse it for subsequent accesses from the same thread.

UseVec is the owned implementation of this trait in this crate.

§Examples

use orx_parallel::{Use, UseVec};

fn bump_first_thread<U: Use<Item = usize>>(use_var: &mut U) -> usize {
    *unsafe { use_var.init_get(0) } += 1;
    *use_var.get(0)
}

let mut use_vec = UseVec::new(|_| 0usize);
assert_eq!(bump_first_thread(&mut use_vec), 1);
assert_eq!(use_vec.into_vec(), vec![1]);

Required Associated Types§

Source

type Item: Send

Type of the worker-local mutable value stored for each thread.

Required Methods§

Source

unsafe fn init_get(&self, thread_idx: usize) -> &mut Self::Item

Returns the mutable worker-local value for thread_idx, creating it if needed.

§SAFETY

Must be called only once per thread.

Source

fn get(&mut self, thread_idx: usize) -> &mut Self::Item

Returns the already-initialized mutable worker-local value for thread_idx.

§Panics

Panics if the corresponding slot has not been initialized by a previous call to init_get.

Source

fn max_threads(&self) -> Option<usize>

Returns an upper bound on the number of worker threads that may use this value.

None means the implementation does not impose a fixed upper bound.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl<T: Send, F: Fn(usize) -> T + Sync> Use for &mut UseVec<T, F>

Source§

type Item = T

Source§

impl<T: Send, F: Fn(usize) -> T + Sync> Use for UseVec<T, F>

Source§

type Item = T