Trait orx_fixed_vec::prelude::PinnedVec
source · pub trait PinnedVec<T> {
Show 16 methods
// Required methods
fn clear(&mut self);
fn capacity(&self) -> usize;
fn extend_from_slice(&mut self, other: &[T])
where T: Clone;
fn get(&self, index: usize) -> Option<&T>;
fn get_mut(&mut self, index: usize) -> Option<&mut T>;
unsafe fn get_unchecked(&self, index: usize) -> &T;
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T;
fn is_empty(&self) -> bool;
fn len(&self) -> usize;
fn push(&mut self, value: T);
unsafe fn unsafe_insert(&mut self, index: usize, element: T);
unsafe fn unsafe_remove(&mut self, index: usize) -> T;
unsafe fn unsafe_pop(&mut self) -> Option<T>;
unsafe fn unsafe_clone(&self) -> Self
where T: Clone;
fn partial_eq<S>(&self, other: S) -> bool
where S: AsRef<[T]>,
T: PartialEq<T>;
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
where T: Debug;
}
Expand description
A vector of elements of T which differs from the std::vec::Vec
by the following feature:
- memory location of an element already pushed to the collection never changes and remains valid unless:
- the vector is
drop
ped, - the vector is
clear
ed,resize
d ortruncate
d, - any element is
insert
ed into the vector, - any element is
remove
d orpop
ped from the vector.
- the vector is
Or, more briefly, push
ing or extend
ing the vector does not change memory locations
of already added elements, and hence, the corresponding references remain valid.
Safety
This trait can be considered as a marker trait: its methods are relevant for the useful ‘vec’-related side of the trait, rather than the pinned side. The implementing struct must guarantee that pushing or extending the vector does not cause the memory locations of already added elements to change.
Required Methods§
sourcefn clear(&mut self)
fn clear(&mut self)
Clears the vector, removing all values.
Note that this method has no effect on the allocated capacity of the vector.
Safety
clear
operation is safe both when T: NotSelfReferencingVecItem
or T: SelfReferencingVecItem
.
The prior is obvious; the reason why T: SelfReferencingVecItem
is safe is as follows:
- elements holding references to each other will be cleaned all together; hence, none of them can have an invalid reference;
- we cannot have a reference to a vector element defined before the
clear
, sinceclear
requires amut
reference.
sourcefn capacity(&self) -> usize
fn capacity(&self) -> usize
Returns the total number of elements the vector can hold without reallocating.
sourcefn extend_from_slice(&mut self, other: &[T])where
T: Clone,
fn extend_from_slice(&mut self, other: &[T])where T: Clone,
Clones and appends all elements in a slice to the Vec.
Iterates over the slice other, clones each element, and then appends it to this Vec. The other slice is traversed in-order.
Note that this function is same as extend except that it is specialized to work with slices instead. If and when Rust gets specialization this function will likely be deprecated (but still available).
sourcefn get(&self, index: usize) -> Option<&T>
fn get(&self, index: usize) -> Option<&T>
Returns a reference to an element with the given index
,
returns None if the index is out of bounds.
sourcefn get_mut(&mut self, index: usize) -> Option<&mut T>
fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to an element with the given index
,
returns None if the index is out of bounds.
sourceunsafe fn get_unchecked(&self, index: usize) -> &T
unsafe fn get_unchecked(&self, index: usize) -> &T
Returns a reference to an element or subslice, without doing bounds checking.
For a safe alternative see [get
].
Safety
Calling this method with an out-of-bounds index is [undefined behavior] even if the resulting reference is not used.
sourceunsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Returns a mutable reference to an element or subslice, without doing bounds checking.
For a safe alternative see [get_mut
].
Safety
Calling this method with an out-of-bounds index is [undefined behavior] even if the resulting reference is not used.
sourcefn len(&self) -> usize
fn len(&self) -> usize
Returns the number of elements in the vector, also referred to as its ‘length’.
sourceunsafe fn unsafe_insert(&mut self, index: usize, element: T)
unsafe fn unsafe_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
This operation is unsafe when T
is not NotSelfRefVecItem
.
To pick the conservative approach, every T which does not implement NotSelfRefVecItem
is assumed to be a vector item referencing other vector items.
insert
is unsafe since insertion of a new element at an arbitrary position of the vector
typically changes the positions of already existing elements.
When the elements are holding references to other elements of the vector, this change in positions makes the references invalid.
On the other hand, any vector implementing PinnedVec<T>
where T: NotSelfRefVecItem
implements PinnedVecSimple<T>
which implements the safe version of this method.
sourceunsafe fn unsafe_remove(&mut self, index: usize) -> T
unsafe fn unsafe_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
This operation is unsafe when T
is not NotSelfRefVecItem
.
To pick the conservative approach, every T which does not implement NotSelfRefVecItem
is assumed to be a vector item referencing other vector items.
remove
is unsafe since removal of an element at an arbitrary position of the vector
typically changes the positions of already existing elements.
Further, it is possible that at least one of the remaining elements is pointing to the element which is being removed.
On the other hand, any vector implementing PinnedVec<T>
where T: NotSelfRefVecItem
implements PinnedVecSimple<T>
which implements the safe version of this method.
sourceunsafe fn unsafe_pop(&mut self) -> Option<T>
unsafe fn unsafe_pop(&mut self) -> Option<T>
Removes the last element from a vector and returns it, or None if it is empty.
Safety
This operation is unsafe when T
is not NotSelfRefVecItem
.
To pick the conservative approach, every T which does not implement NotSelfRefVecItem
is assumed to be a vector item referencing other vector items.
pop
is unsafe since it is possible that at least one of the remaining elements is
pointing to the last element which is being popped.
On the other hand, any vector implementing PinnedVec<T>
where T: NotSelfRefVecItem
implements PinnedVecSimple<T>
which implements the safe version of this method.
sourceunsafe fn unsafe_clone(&self) -> Selfwhere
T: Clone,
unsafe fn unsafe_clone(&self) -> Selfwhere T: Clone,
Creates and returns a clone of the vector.
Safety
This operation is unsafe when T
is not NotSelfRefVecItem
.
To pick the conservative approach, every T which does not implement NotSelfRefVecItem
is assumed to be a vector item referencing other vector items.
To understand why clone
is unsafe when T
is not NotSelfRefVecItem
,
consider the following example.
- let
vec
be the initial vector with self referencing vector elements. - let
cl
be the clone ofA
; i.e., ’let cl = vec.clone()`. - In this case, elements of
cl
are pointing to elements ofvec
.- This is not correct, as these references are to be kept internal to the vector.
- Furthermore, if
vec
is dropped,cl
elements will contain invalid references leading to UB.