Trait IntoParIndex

Source
pub unsafe trait IntoParIndex<T>: Sized {
    // Required methods
    fn into_pointer_par_index(
        self,
    ) -> impl PointerIndex<T> + ParCollection<Self>;
    fn into_par_index_no_ref(
        self,
    ) -> impl UnsafeNoRefIndex<T> + ParCollection<Self>;
    fn into_par_index(self) -> impl UnsafeIndex<T> + ParCollection<Self>;
    fn into_pointer_par_chunk_index(
        self,
        chunk_size: usize,
    ) -> impl PointerChunkIndex<T> + ParCollection<Self>;
    fn into_par_chunk_index_no_ref(
        self,
        chunk_size: usize,
    ) -> impl UnsafeNoRefChunkIndex<T> + ParCollection<Self>;
    fn into_par_chunk_index(
        self,
        chunk_size: usize,
    ) -> impl UnsafeChunkIndex<T> + ParCollection<Self>;
}
Expand description

A value-to-value conversion that consumes the input collection and produces one that allows unsynchronized access to its elements.

This trait allows to convert a collection to allow unsynchronized access to its elements, either one-by-one or in chunks of arbitrary size.

The different methods allow the user to choose the level of access to use:

Unsafe code can rely on this trait behavior thanks to the invariants specified below.

§Safety

Implementors of this trait must guarantee the following invariants:

§Examples

We can convert back and forth between different paradigms:

let mut collection = vec![0; 10];

// Let's use pointers to single elements
let par_collection = collection.into_pointer_par_index();

let ptr_1 = par_collection.get_mut_ptr(1);
unsafe {
    *ptr_1 = 42;
}

collection = par_collection.into();
assert_eq!(collection, vec![0, 42, 0, 0, 0, 0, 0, 0, 0, 0]);

// Let's use setters and getters to chunks of size 2
let par_collection = collection.into_par_chunk_index_no_ref(2);

unsafe {
    par_collection.set(1, &[69, 69]);
}

collection = par_collection.into();
assert_eq!(collection, vec![0, 42, 69, 69, 0, 0, 0, 0, 0, 0]);

// Let's use references to chunks of size 5
let par_collection = collection.into_par_chunk_index(5);

let last_five = unsafe { par_collection.get_mut(1) };
let mut i = 1;
for elem in last_five.iter_mut() {
    *elem = i;
    i += 1;
}
last_five[2] = 42;

collection = par_collection.into();
assert_eq!(collection, vec![0, 42, 69, 69, 0, 1, 2, 42, 4, 5]);

Required Methods§

Source

fn into_pointer_par_index(self) -> impl PointerIndex<T> + ParCollection<Self>

Converts the collection into one that allows unsynchronized access to its elements through pointers.

§Examples
let collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_pointer_par_index();

let mut_ptr_1 = collection.get_mut_ptr(1);
let mut_ptr_5 = collection.get_mut_ptr(5);
let ptr_2 = collection.get_ptr(2);
unsafe {
    *mut_ptr_1 = 42;
    *mut_ptr_5 = 69;
    assert_eq!(*ptr_2, 2);
}

assert_eq!(collection.into(), vec![0, 42, 2, 3, 4, 69, 6, 7, 8, 9]);
Source

fn into_par_index_no_ref(self) -> impl UnsafeNoRefIndex<T> + ParCollection<Self>

Converts the collection into one that allows unsynchronized access to its elements through setters and getters.

§Examples
let collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_par_index_no_ref();

unsafe {
    collection.set(1, 42);
    collection.set(5, 69);
    assert_eq!(collection.get(2), 2);
}

assert_eq!(collection.into(), vec![0, 42, 2, 3, 4, 69, 6, 7, 8, 9]);
Source

fn into_par_index(self) -> impl UnsafeIndex<T> + ParCollection<Self>

Converts the collection into one that allows unsynchronized access to its elements through references.

§Examples
let collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_par_index();

let mut_ref_1 = unsafe { collection.get_mut(1) };
let mut_ref_5 = unsafe { collection.get_mut(5) };
let ref_2 = unsafe { collection.get(2) };
*mut_ref_1 = 42;
*mut_ref_5 = 69;
assert_eq!(*ref_2, 2);

assert_eq!(collection.into(), vec![0, 42, 2, 3, 4, 69, 6, 7, 8, 9]);
Source

fn into_pointer_par_chunk_index( self, chunk_size: usize, ) -> impl PointerChunkIndex<T> + ParCollection<Self>

Converts the collection into one that allows unsynchronized access to chunks of chunk_size of its elements through pointers.

§Panics

Panics if the size of the collection is not divisible by chunk_size.

§Examples
let collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_pointer_par_chunk_index(5);

let first_five = collection.get_mut_ptr(0);
let last_five = collection.get_mut_ptr(1);
unsafe {
    (*first_five)[1] = 42;
    (*last_five)[0] = 69;
    assert_eq!((*first_five)[2], 2);
}

assert_eq!(collection.into(), vec![0, 42, 2, 3, 4, 69, 6, 7, 8, 9]);
Source

fn into_par_chunk_index_no_ref( self, chunk_size: usize, ) -> impl UnsafeNoRefChunkIndex<T> + ParCollection<Self>

Converts the collection into one that allows unsynchronized access to chunks of chunk_size of its elements through setters and getters.

§Panics

Panics if the size of the collection is not divisible by chunk_size.

§Examples
let collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_par_chunk_index_no_ref(5);

unsafe {
    collection.set(0, &[0, 42, 2, 3, 4]);
    collection.set(1, &[69, 6, 7, 8, 9]);
    assert_eq!(collection.get(1, vec![0; 5]).as_ref(), vec![69, 6, 7, 8, 9]);
}

assert_eq!(collection.into(), vec![0, 42, 2, 3, 4, 69, 6, 7, 8, 9]);
Source

fn into_par_chunk_index( self, chunk_size: usize, ) -> impl UnsafeChunkIndex<T> + ParCollection<Self>

Converts the collection into one that allows unsynchronized access to chunks of chunk_size of its elements through references.

§Panics

Panics if the size of the collection is not divisible by chunk_size.

§Examples
let collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9].into_par_chunk_index(5);

let first_five = unsafe { collection.get_mut(0) };
let last_five = unsafe { collection.get_mut(1) };
first_five[1] = 42;
last_five[0] = 69;
assert_eq!(first_five[2], 2);

assert_eq!(collection.into(), vec![0, 42, 2, 3, 4, 69, 6, 7, 8, 9]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Send + Sync> IntoParIndex<T> for Box<[T]>

Source§

impl<T: Send + Sync> IntoParIndex<T> for Vec<T>

Implementors§