Trait orx_split_vec::prelude::PinnedVecSimple
source · pub trait PinnedVecSimple<T>: PinnedVec<T>where
T: NotSelfRefVecItem,{
// Required methods
fn insert(&mut self, index: usize, element: T);
fn remove(&mut self, index: usize) -> T;
fn pop(&mut self) -> Option<T>;
fn swap(&mut self, a: usize, b: usize);
fn truncate(&mut self, len: usize);
fn clone(&self) -> Self
where T: Clone;
}Expand description
PinnedVecSimple is a PinnedVec where the elements satisfy the trait bound
`T: NotSelfRefVecItem.
In other words, elements of the vector does not hold references to other
elements of the same vector.
Note that this is satisfied for all std::vec::Vecs.
On the other hand, PinnedVec is designed to conveniently build more complex
data structures while holding children of these data structures in a vec-like
layout for better cache locality and to reduce heap allocations.
These structures often contain child structures referencing each other;
such as the parent or children relations of a tree.
PinnedVec aims at guaranteeing that these internal references are kept valid.
This makes methods insert, remove and pop unsafe.
Since the aforementioned safety concern is absent when elements do not hold such internal references;
i.e., when T: NotSelfRefVecItem,
such vectors automatically implement PinnedVecSimple which enables safe calls
to these methods.
Safety
Picking the more conservative and safer approach;
the default versions of methods insert, remove and pop are unsafe.
In order to be able to make safe calls to these methods,
once must explicitly implement NotSelfRefVecItem for the element type.
This is a marker trait, and hence, easy to implement.
For convenience, this crate already contains implementations for the primitive structs such as numbers, string or bool.
Required Methods§
sourcefn insert(&mut self, index: usize, element: T)
fn insert(&mut self, index: usize, element: T)
Inserts an element at position index within the vector, shifting all elements after it to the right.
Panics
Panics if index >= len.
Safety
insert operation for a PinnedVecSimple where the elements are T: NotSelfRefVecItem is safe;
in this case, pinned vector shares the same safety requirements as std::vec::Vec which is readily
provided by the borrow checker.
sourcefn remove(&mut self, index: usize) -> T
fn remove(&mut self, index: usize) -> T
Removes and returns the element at position index within the vector, shifting all elements after it to the left.
Panics
Panics if index is out of bounds.
Safety
remove operation for a PinnedVecSimple where the elements are T: NotSelfRefVecItem is safe;
in this case, pinned vector shares the same safety requirements as std::vec::Vec which is readily
provided by the borrow checker.
sourcefn pop(&mut self) -> Option<T>
fn pop(&mut self) -> Option<T>
Removes the last element from a vector and returns it, or None if it is empty.
Safety
pop operation for a PinnedVecSimple where the elements are T: NotSelfRefVecItem is safe;
in this case, pinned vector shares the same safety requirements as std::vec::Vec which is readily
provided by the borrow checker.
sourcefn swap(&mut self, a: usize, b: usize)
fn swap(&mut self, a: usize, b: usize)
Swaps two elements in the slice.
If a equals to b, it’s guaranteed that elements won’t change value.
Arguments
- a - The index of the first element
- b - The index of the second element
Safety
swap operation for a PinnedVecSimple where the elements are T: NotSelfRefVecItem is safe;
in this case, pinned vector shares the same safety requirements as std::vec::Vec which is readily
provided by the borrow checker.
sourcefn truncate(&mut self, len: usize)
fn truncate(&mut self, len: usize)
Shortens the vector, keeping the first len elements and dropping
the rest.
If len is greater than the vector’s current length, this has no
effect.
sourcefn clone(&self) -> Selfwhere
T: Clone,
fn clone(&self) -> Selfwhere T: Clone,
Creates and returns a clone of the vector.
Safety
clone operation for a PinnedVecSimple where the elements are T: NotSelfRefVecItem is safe;
in this case, pinned vector shares the same safety requirements as std::vec::Vec which is readily
provided by the borrow checker.