Struct rayon_tlsctx::ThreadLocalCtx[][src]

pub struct ThreadLocalCtx<T, F> { /* fields omitted */ }
Expand description

A thread local storage container for Rayon jobs

This context can be used to efficiently clone inner, only when it’s necessary.


impl<T, F: Fn() -> T> ThreadLocalCtx<T, F>[src]

pub fn new(inner: F) -> Self[src]

Create a new TlsCtx


Creating a thread-local byte buffer:

use rayon_tlsctx::ThreadLocalCtx;
let ctx = ThreadLocalCtx::new(Vec::<u8>::new);

Cloning an initialised buffer for each thread:

use rayon_tlsctx::ThreadLocalCtx;

let mut buf: Vec<u16> = (0..!0).collect();
buf.shuffle(&mut rng);

let ctx = ThreadLocalCtx::new(move || buf.clone());

pub unsafe fn get<'a>(&'a self) -> ThreadLocalMut<'a, T, F>[src]

Get a thread local context reference with dynamically checked borrow rules


Only one thread pool at a given time should use this scope. The thread pool size can not grow midway through.


If called from outside of a Rayon thread pool, or when the reference is already being borrowed.


use rayon_tlsctx::ThreadLocalCtx;
use rayon::iter::*;

const NUM_COPIES: usize = 16;

let mut buf: Vec<u16> = (0..!0).collect();

// Create a thread local context with value 0.
let ctx = ThreadLocalCtx::new(|| 0);

// Sum the buffer `NUM_COPIES` times and accumulate the results
// into the threaded pool of counts. Note that the counts may be
// Unevenly distributed.
    .flat_map(|_| buf.par_iter())
    .for_each(|i| {
        let mut cnt = unsafe { ctx.get() };
        *cnt += *i as usize;

let buf_sum = buf.into_iter().fold(0, |acc, i| acc + i as usize);

// What matters is that the final sum matches the expected value.
assert_eq!(ctx.into_iter().sum::<usize>(), buf_sum * NUM_COPIES);

Trait Implementations

impl<T, F> IntoIterator for ThreadLocalCtx<T, F>[src]

Consume the context and retrieve all created items.

type Item = T

The type of the elements being iterated over.

type IntoIter = FilterMap<IntoIter<(Option<T>, bool)>, fn(_: (Option<T>, bool)) -> Option<T>>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<T: Send, F: Send> Send for ThreadLocalCtx<T, F>[src]

impl<T: Send, F: Send> Sync for ThreadLocalCtx<T, F>[src]

Auto Trait Implementations

impl<T, F> !RefUnwindSafe for ThreadLocalCtx<T, F>

impl<T, F> Unpin for ThreadLocalCtx<T, F> where
    F: Unpin,
    T: Unpin

impl<T, F> UnwindSafe for ThreadLocalCtx<T, F> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T

pub const ALIGN: usize

The alignment of pointer.

type Init = T

The type for initializers.

pub unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more

pub unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more

pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.