gvec 0.5.0

Very simple implementation of generational indexing for vectors written in Rust
Documentation
use crate::{error::Error, index::GenerationalIndex};

pub trait SoftAlloc {
    fn match_generation(&self, index: &GenerationalIndex) -> bool;
    fn allocate(&mut self, vec_len: usize) -> GenerationalIndex;
    fn deallocate(&mut self, index: &GenerationalIndex) -> Result<(), Error>;
}

/// Soft version of a Vec with Generational scheme
///
/// Similar to the LightVec, the SoftVec is a data structure with a so-called
/// Generational scheme, that functions as a versioning scheme for indices. In
/// contrast to LightVec, there isn't an implemented allocator for the SoftVec.
/// Instead, there is a trait that *must* be implemented in order to provide
/// the Generational indices.
pub struct SoftVec<T>(Vec<T>);

impl<T> SoftVec<T> {
    /// Creates a new SoftVec
    ///
    /// Creates a new SoftVec, with the given capacity, in order to reduce
    /// initial allocations
    pub fn with_capacity(capacity: usize) -> SoftVec<T> {
        SoftVec(Vec::with_capacity(capacity))
    }

    /// Returns a value with the given index
    pub fn get(&self, index: &GenerationalIndex, alloc: &mut impl SoftAlloc) -> Option<&T> {
        if alloc.match_generation(index) {
            self.0.get(index.index())
        } else {
            None
        }
    }
    
    /// Inserts a value on the vector
    pub fn insert(&mut self, value: T, alloc: &mut impl SoftAlloc) -> GenerationalIndex {
        let temp_idx: GenerationalIndex = alloc.allocate(self.0.len());
        self.0.insert(temp_idx.index(), value);
        temp_idx
    }

    /// Removes a value with the given index
    pub fn remove(
        &mut self,
        index: GenerationalIndex,
        alloc: &mut impl SoftAlloc,
    ) -> Result<(), Error> {
        if let Some(_val) = self.0.get(index.index()) {
            alloc.deallocate(&index)?;
            self.0.remove(index.index());
            Ok(())
        } else {
            Err(Error::ElementNotFound)
        }
    }
}