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:
as_par_index_no_refandas_par_chunk_index_no_refallow access throughUnsafeNoRefIndexandUnsafeNoRefChunkIndexrespectively (see module documentation for more information).as_pointer_par_indexandas_pointer_par_chunk_indexallow access throughPointerIndexandPointerChunkIndexrespectively (see module documentation for more information).as_par_indexandas_par_chunk_indexallow access throughUnsafeIndexandUnsafeChunkIndexrespectively (see module documentation for more information).
Unsafe code can rely on this trait behavior thanks to the invariants specified below.
§Safety
Implementors of this trait must guarantee the following invariants:
as_par_index_no_ref,as_pointer_par_indexandas_par_indexreturn views on the collection such that theirlenis the size of the collection and that if indexirefers to elementxin the collection, it refers to elementxin the returned views as well.as_par_chunk_index_no_ref,as_pointer_par_chunk_indexandas_par_chunk_indexpanic if the collection’s size is not divisible bychunk_size.as_par_chunk_index_no_ref,as_pointer_par_chunk_indexandas_par_chunk_indexreturn views on the collection such that theirnum_elementsis equal to the size of the collection,chunk_sizeis equal to thechunk_sizeparameter passed to the method,lenis equal tonum_elements / chunk_sizeand chunk indices follow the collection’s original indices (i.e. chunk 0 of a collection ofchunk_size4 includes indices from 0 to 3, chunk 1 includes indices from 4 to 7, etc.).
§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§
Sourcefn as_pointer_par_index(&mut self) -> impl PointerIndex<T> + ParView
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]);Sourcefn as_par_index_no_ref(&mut self) -> impl UnsafeNoRefIndex<T> + ParView
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]);Sourcefn as_par_index(&mut self) -> impl UnsafeIndex<T> + ParView
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]);Sourcefn as_pointer_par_chunk_index(
&mut self,
chunk_size: usize,
) -> impl PointerChunkIndex<T> + ParView
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]);Sourcefn as_par_chunk_index_no_ref(
&mut self,
chunk_size: usize,
) -> impl UnsafeNoRefChunkIndex<T> + ParView
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]);Sourcefn as_par_chunk_index(
&mut self,
chunk_size: usize,
) -> impl UnsafeChunkIndex<T> + ParView
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.