Struct orx_fixed_vec::FixedVec
source · 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
ImpVec
to enable immutable-push operations which allows for convenient, efficient and safe implementations of self-referencing data structures.
Implementations§
source§impl<T> FixedVec<T>
impl<T> FixedVec<T>
sourcepub fn new(fixed_capacity: usize) -> Self
pub fn new(fixed_capacity: usize) -> Self
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());
sourcepub fn room(&self) -> usize
pub fn room(&self) -> usize
Returns the available room for new items; i.e.,
capacity() - len()
.
Examples
use orx_fixed_vec::prelude::*;
let mut vec = FixedVec::new(7);
vec.push(42);
assert_eq!(7, vec.capacity());
assert_eq!(1, vec.len());
assert_eq!(6, vec.room());
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Return whether the fixed vector is full or not;
equivalent to capacity() == len()
or room() == 0
.
Examples
use orx_fixed_vec::prelude::*;
let mut vec = FixedVec::new(2);
assert!(!vec.is_full());
vec.push(42);
assert!(!vec.is_full());
vec.push(7);
assert!(vec.is_full());
Trait Implementations§
source§impl<T> IntoIterator for FixedVec<T>
impl<T> IntoIterator for FixedVec<T>
source§impl<T> PinnedVec<T> for FixedVec<T>
impl<T> PinnedVec<T> for FixedVec<T>
source§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]));
}
source§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()
.
source§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()
.
source§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.
§type Iter<'a> = Iter<'a, T>
where
T: 'a,
Self: 'a
type Iter<'a> = Iter<'a, T> where T: 'a, Self: 'a
source§fn capacity(&self) -> usize
fn capacity(&self) -> usize
source§fn get(&self, index: usize) -> Option<&T>
fn get(&self, index: usize) -> Option<&T>
index
returns None if the index is out of bounds.source§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.source§unsafe fn get_unchecked(&self, index: usize) -> &T
unsafe fn get_unchecked(&self, index: usize) -> &T
source§unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
source§fn len(&self) -> usize
fn len(&self) -> usize
source§unsafe fn unsafe_remove(&mut self, index: usize) -> T
unsafe fn unsafe_remove(&mut self, index: usize) -> T
source§unsafe fn unsafe_pop(&mut self) -> Option<T>
unsafe fn unsafe_pop(&mut self) -> Option<T>
source§unsafe fn unsafe_swap(&mut self, a: usize, b: usize)
unsafe fn unsafe_swap(&mut self, a: usize, b: usize)
source§unsafe fn unsafe_truncate(&mut self, len: usize)
unsafe fn unsafe_truncate(&mut self, len: usize)
len
elements and dropping
the rest. Read moresource§unsafe fn unsafe_clone(&self) -> Selfwhere
T: Clone,
unsafe fn unsafe_clone(&self) -> Selfwhere
T: Clone,
source§fn partial_eq<S>(&self, other: S) -> bool
fn partial_eq<S>(&self, other: S) -> bool
self
and other
values to be equal, and is used by ==
.