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:
into_par_index_no_refandinto_par_chunk_index_no_refallow access throughUnsafeNoRefIndexandUnsafeNoRefChunkIndexrespectively (see module documentation for more information).into_pointer_par_indexandinto_pointer_par_chunk_indexallow access throughPointerIndexandPointerChunkIndexrespectively (see module documentation for more information).into_par_indexandinto_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:
into_par_index_no_ref,into_pointer_par_indexandinto_par_indexreturn collections such that theirlenis the size of the input collection and that if indexirefers to elementxin the input collection, it refers to elementxin the returned collection as well.into_par_chunk_index_no_ref,into_pointer_par_chunk_indexandinto_par_chunk_indexpanic if the collection’s size is not divisible bychunk_size.into_par_chunk_index_no_ref,into_pointer_par_chunk_indexandinto_par_chunk_indexreturn collections such that theirnum_elementsis equal to the size of the input collection,chunk_sizeis equal to thechunk_sizeparameter passed to the method,lenis equal tonum_elements / chunk_sizeand chunk indices follow the input collection’s 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.).- All returned collections must implement
Intoto convert back to the original collection type. The input collection’s original internal state beside size is not guaranteed to be preserved (i.e. aVeccan possess a differentcapacitywhen converted back, but thelenmust be the same, as well as the indexes of its elements).
§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§
Sourcefn into_pointer_par_index(self) -> impl PointerIndex<T> + ParCollection<Self>
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]);Sourcefn into_par_index_no_ref(self) -> impl UnsafeNoRefIndex<T> + ParCollection<Self>
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]);Sourcefn into_par_index(self) -> impl UnsafeIndex<T> + ParCollection<Self>
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]);Sourcefn into_pointer_par_chunk_index(
self,
chunk_size: usize,
) -> impl PointerChunkIndex<T> + ParCollection<Self>
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]);Sourcefn into_par_chunk_index_no_ref(
self,
chunk_size: usize,
) -> impl UnsafeNoRefChunkIndex<T> + ParCollection<Self>
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]);Sourcefn into_par_chunk_index(
self,
chunk_size: usize,
) -> impl UnsafeChunkIndex<T> + ParCollection<Self>
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.