generational-cache 0.2.2

Generational Arena based cache impls. in 100% safe, [no_std] compatible Rust.
Documentation
//! Module providing abstractions for reperesenting vectors.

use core::ops::DerefMut;

/// Represents an abstract vector over a type accessible as mutable slice.
pub trait Vector<T>: DerefMut<Target = [T]> {
    type Error: core::fmt::Debug;

    /// Reserves memory for the given number of additional items in this vector.
    fn reserve(&mut self, additional: usize) -> Result<(), Self::Error>;

    /// Returns the number of items this vector is capable of storing.
    fn capacity(&self) -> usize;

    /// Pushes a new element to the end of this vector.
    fn push(&mut self, item: T) -> Result<(), Self::Error>;

    /// Removes all elements from this vector.
    fn clear(&mut self);
}

pub mod impls;

#[doc(hidden)]
pub mod tests {
    use super::Vector;

    pub fn _test_vector_consistency<V: Vector<usize>>(mut vector: V) {
        vector.clear();

        let cap_0 = vector.capacity();

        vector.reserve(vector.capacity()).unwrap();

        let cap_1 = vector.capacity();

        assert_eq!(cap_0, cap_1);

        assert!(vector.is_empty());

        for i in 0..vector.capacity() {
            vector.push(i).unwrap();
        }

        assert_eq!(vector.len(), vector.capacity());

        for (j, i) in vector.iter().enumerate() {
            assert_eq!(i, &j);
        }

        let cap_0 = vector.capacity();

        let res = vector.push(42);

        let cap_1 = vector.capacity();

        if cap_0 == cap_1 {
            assert!(res.is_err());
        } else {
            assert!(cap_1 > cap_0, "Capacity decreased on push().");
            assert!(res.is_ok());
        }

        let cap_0 = vector.capacity();

        vector.clear();

        let cap_1 = vector.capacity();

        assert!(cap_0 == cap_1, "Capacity changed on clear().");

        assert!(vector.is_empty());

        const ADDITIONAL: usize = 5;

        let result = vector.reserve(ADDITIONAL);

        if result.is_err() {
            return;
        }

        for i in 0..ADDITIONAL {
            vector.push(i).unwrap();
        }
    }
}