Struct orx_linked_list::FixedVec
pub struct FixedVec<T> { /* private fields */ }Expand description
A fixed vector, FixedVec, is a vector with a strict predetermined capacity
(see SplitVec for dynamic capacity version).
It provides the following features:
- It provides operations with the same complexity and speed as the standard vector.
- It makes sure that the data stays pinned in place.
FixedVec<T>implementsPinnedVec<T>for anyT;FixedVec<T>implementsPinnedVecSimple<T>forT: NotSelfRefVecItem;- Memory location of an item added to the fixed vector will never change unless the vector is dropped or cleared.
- This allows the fixed vec to be converted into an
ImpVecto enable immutable-push operations which allows for convenient, efficient and safe implementations of self-referencing data structures.
Implementations§
§impl<T> FixedVec<T>
impl<T> FixedVec<T>
pub fn new(fixed_capacity: usize) -> FixedVec<T>
pub fn new(fixed_capacity: usize) -> FixedVec<T>
Creates a new vector with the given fixed capacity.
Note that the vector can never grow beyond this capacity.
Examples
use orx_fixed_vec::prelude::*;
let mut vec = FixedVec::new(7);
vec.push(42);
assert_eq!(7, vec.capacity());Trait Implementations§
§impl<T, I> Index<I> for FixedVec<T>where
I: SliceIndex<[T]>,
impl<T, I> Index<I> for FixedVec<T>where I: SliceIndex<[T]>,
§impl<T, I> IndexMut<I> for FixedVec<T>where
I: SliceIndex<[T]>,
impl<T, I> IndexMut<I> for FixedVec<T>where I: SliceIndex<[T]>,
§impl<T> IntoIterator for FixedVec<T>
impl<T> IntoIterator for FixedVec<T>
§impl<T> PinnedVec<T> for FixedVec<T>
impl<T> PinnedVec<T> for FixedVec<T>
§fn index_of(&self, element: &T) -> Option<usize>
fn index_of(&self, element: &T) -> Option<usize>
Returns the index of the element with the given reference.
This method has O(1) time complexity.
Note that T: Eq is not required; reference equality is used.
Safety
Since FixedVec implements PinnedVec, the underlying memory
of the vector stays pinned; i.e., is not carried to different memory
locations.
Therefore, it is possible and safe to compare an element’s reference
to find its position in the vector.
Examples
use orx_fixed_vec::prelude::*;
let mut vec = FixedVec::new(4);
for i in 0..4 {
vec.push(10 * i);
}
assert_eq!(Some(0), vec.index_of(&vec[0]));
assert_eq!(Some(1), vec.index_of(&vec[1]));
assert_eq!(Some(2), vec.index_of(&vec[2]));
assert_eq!(Some(3), vec.index_of(&vec[3]));
// the following does not compile since vec[4] is out of bounds
// assert_eq!(Some(3), vec.index_of(&vec[4]));
// num certainly does not belong to `vec`
let num = 42;
assert_eq!(None, vec.index_of(&num));
// even if its value belongs
let num = 20;
assert_eq!(None, vec.index_of(&num));
// as expected, querying elements of another vector will also fail
let eq_vec = vec![0, 10, 20, 30];
for i in 0..4 {
assert_eq!(None, vec.index_of(&eq_vec[i]));
}§fn 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).
Panics
Panics if there is not enough room in the vector for the elements in other;
i.e., self.room() < other.len().
§fn push(&mut self, value: T)
fn push(&mut self, value: T)
Appends an element to the back of a collection.
Panics
Panics if there is no available room in the vector;
i.e., self.is_full() or equivalently self.len() == self.capacity().
§unsafe 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.
Panics also if there is no available room in the vector;
i.e., self.is_full() or equivalently self.len() == self.capacity().
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.
§fn capacity(&self) -> usize
fn capacity(&self) -> usize
§fn get(&self, index: usize) -> Option<&T>
fn get(&self, index: usize) -> Option<&T>
index,
returns None if the index is out of bounds.§fn get_mut(&mut self, index: usize) -> Option<&mut T>
fn get_mut(&mut self, index: usize) -> Option<&mut T>
index,
returns None if the index is out of bounds.§unsafe fn get_unchecked(&self, index: usize) -> &T
unsafe fn get_unchecked(&self, index: usize) -> &T
§unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
§fn len(&self) -> usize
fn len(&self) -> usize
§unsafe fn unsafe_remove(&mut self, index: usize) -> T
unsafe fn unsafe_remove(&mut self, index: usize) -> T
§unsafe fn unsafe_pop(&mut self) -> Option<T>
unsafe fn unsafe_pop(&mut self) -> Option<T>
§unsafe fn unsafe_swap(&mut self, a: usize, b: usize)
unsafe fn unsafe_swap(&mut self, a: usize, b: usize)
§unsafe fn unsafe_truncate(&mut self, len: usize)
unsafe fn unsafe_truncate(&mut self, len: usize)
len elements and dropping
the rest. Read more