[−][src]Struct gvec::GenerationalVec
A Generational vector
A vector with a so-called Generational scheme, akin to a versioning scheme, for its indices. This is done in order to prevent use-after-free scenarios, where an attempt to access a specific position with a given index is denied after the original item is removed and its position (or index) has been freed. It also promotes memory reuse, since the memory allocated is not released until the vector goes out of scope or the program ends, and it might lead to lower overall memory use.
Note
Due to the functionality of avoiding invalid access (use-after-free), this type of vector consumes much more memory than a simple vector itself.
Methods
impl<T> GenerationalVec<T>
[src]
pub fn with_capacity(capacity: usize) -> GenerationalVec<T>
[src]
Creates a GenVec with a specific capacity
Creates a GenerationalVec with a pre-allocated memory in order to reduce the overhead of future reallocations.
pub fn get(&self, index: GenerationalIndex) -> Option<&T>
[src]
Get a value by an index
Returns the value if both the index and the generation do match. Otherwise returns None. A safe alternative to the index [] operator, which can panic if index out-of-bounds or generation does not match.
pub fn insert(&mut self, value: T) -> GenerationalIndex
[src]
Insert a value on the vector
Returns a GenerationalIndex with the index and the generation in which it was added.
Example
use gvec::GenerationalVec; let mut example = GenerationalVec::with_capacity(2); let first = example.insert(2); assert_eq!(example[first], 2);
pub fn remove(&mut self, index: GenerationalIndex) -> Result<(), Error>
[src]
Remove a value of the vector by a given index
Upon the removal of the value, the index is liberated for use, in order to reuse the space already allocated, and the generation is upped by one, in order to prevent use-after-free scenarios or "address violation" (access another value allocated in the same space (i.e. same index), but with a different generation)
Example
use gvec::GenerationalVec; let mut example = GenerationalVec::with_capacity(2); let first = example.insert(1); assert_eq!(example[first], 1); example.remove(first); assert_eq!(example[first], 1); // produces a panic!
pub fn iter(&self) -> impl Iterator<Item = &T>
[src]
Trait Implementations
impl<T> IntoIterator for GenerationalVec<T>
[src]
type Item = T
The type of the elements being iterated over.
type IntoIter = IntoIter<T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<T> Index<GenerationalIndex> for GenerationalVec<T>
[src]
type Output = T
The returned type after indexing.
fn index(&self, index: GenerationalIndex) -> &Self::Output
[src]
Panics
Indexing will panic! when the generation of the given index does not
match the generation in the vector. Use GenVec::get()
instead
as a non-panic version that will return an Option.
Auto Trait Implementations
impl<T> Send for GenerationalVec<T> where
T: Send,
T: Send,
impl<T> Sync for GenerationalVec<T> where
T: Sync,
T: Sync,
Blanket Implementations
impl<I> IntoIterator for I where
I: Iterator,
[src]
I: Iterator,
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
fn into_iter(self) -> I
[src]
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,