Trait ParIndexView

Source
pub unsafe trait ParIndexView<T> {
    // Required methods
    fn as_pointer_par_index(&mut self) -> impl PointerIndex<T> + ParView;
    fn as_par_index_no_ref(&mut self) -> impl UnsafeNoRefIndex<T> + ParView;
    fn as_par_index(&mut self) -> impl UnsafeIndex<T> + ParView;
    fn as_pointer_par_chunk_index(
        &mut self,
        chunk_size: usize,
    ) -> impl PointerChunkIndex<T> + ParView;
    fn as_par_chunk_index_no_ref(
        &mut self,
        chunk_size: usize,
    ) -> impl UnsafeNoRefChunkIndex<T> + ParView;
    fn as_par_chunk_index(
        &mut self,
        chunk_size: usize,
    ) -> impl UnsafeChunkIndex<T> + ParView;
}
Expand description

View of a collection that allows unsynchronized access to its elements.

This trait allows to temporarily opt-in to unsynchronized access to the elements of the underlying collection, 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 may opt-in to different access paradigms in different scopes, but never more than 1 at any given time:

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

{
    // Let's use pointers to single elements
    let view = collection.as_pointer_par_index();
    let ptr_1 = view.get_mut_ptr(1);
    unsafe {
        *ptr_1 = 42;
    }
}

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 view = collection.as_par_chunk_index_no_ref(2);
    unsafe {
        view.set(1, &[69, 69]);
    }
}

assert_eq!(collection, vec![0, 42, 69, 69, 0, 0, 0, 0, 0, 0]);

{
    // Let's use references to chunks of size 5
    let view = collection.as_par_chunk_index(5);
    let last_five = unsafe { view.get_mut(1) };
    let mut i = 1;
    for elem in last_five.iter_mut() {
        *elem = i;
        i += 1;
    }
    last_five[2] = 42;
}

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

Required Methods§

Source

fn as_pointer_par_index(&mut self) -> impl PointerIndex<T> + ParView

Returns a view of the collection that allows unsynchronized access to its elements through pointers.

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

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

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

fn as_par_index_no_ref(&mut self) -> impl UnsafeNoRefIndex<T> + ParView

Returns a view of the collection that allows unsynchronized access to its elements through setters and getters.

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

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

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

fn as_par_index(&mut self) -> impl UnsafeIndex<T> + ParView

Returns a view of the collection that allows unsynchronized access to its elements through references.

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

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

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

fn as_pointer_par_chunk_index( &mut self, chunk_size: usize, ) -> impl PointerChunkIndex<T> + ParView

Returns a view of the collection 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 mut collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

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

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

fn as_par_chunk_index_no_ref( &mut self, chunk_size: usize, ) -> impl UnsafeNoRefChunkIndex<T> + ParView

Returns a view of the collection 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 mut collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

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

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

fn as_par_chunk_index( &mut self, chunk_size: usize, ) -> impl UnsafeChunkIndex<T> + ParView

Returns a view of the collection 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 mut collection = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

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

assert_eq!(collection, 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> ParIndexView<T> for [T]

Source§

fn as_pointer_par_index(&mut self) -> impl PointerIndex<T> + ParView

Source§

fn as_par_index_no_ref(&mut self) -> impl UnsafeNoRefIndex<T> + ParView

Source§

fn as_par_index(&mut self) -> impl UnsafeIndex<T> + ParView

Source§

fn as_pointer_par_chunk_index( &mut self, chunk_size: usize, ) -> impl PointerChunkIndex<T> + ParView

Source§

fn as_par_chunk_index_no_ref( &mut self, chunk_size: usize, ) -> impl UnsafeNoRefChunkIndex<T> + ParView

Source§

fn as_par_chunk_index( &mut self, chunk_size: usize, ) -> impl UnsafeChunkIndex<T> + ParView

Implementors§