gen_vec/lib.rs
1//! # gen-vec
2//!
3//! Vector of reusable, generational indices that owns its values
4//!
5//! [Inspired by Catherine West's closing keynote at RustConf 2018](https://kyren.github.io/2018/09/14/rustconf-talk.html)
6//!
7//! ## Closed vs. Exposed index allocation implementations
8//!
9//! `ClosedGenVec` uses a non user-accessible index allocator to manage indices
10//!
11//! `ExposedGenVec` relies on an external `IndexAllocator`
12//!
13//! As such, an `IndexAllocator` must be created and used to allocate/deallocate indices manually.
14//! This is useful for using the same `Index` across multiple `ExposedGenerationalVec` instances
15//!
16//! **Note:** `IndexAllocator` cannot be used with `ClosedGenerationalVec` since it has its own
17//! internal `IndexAllocator`
18//!
19//! ## Explanation of Generational Indices
20//!
21//! `Index` structs are used to access the vector's contents. An `Index` contains an index for the vector
22//! and a generation (which is 0 initially).
23//!
24//! Deallocated/removed `Index`s go into a list of free `Index`s that can be reused
25//!
26//! Every time an `Index` is reused, the internal generation is incremented. This ensures that a deallocated
27//! `Index` handle can't access data that it no longer validly points to
28
29#[cfg(feature = "serde")]
30use serde::{Serialize, Deserialize};
31
32/// An index of a generational vec
33#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy, Debug)]
34#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
35pub struct Index
36{
37 index: usize,
38 generation: usize
39}
40
41/// An item within a generational vec
42#[derive(Debug)]
43#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
44struct Item<T>
45{
46 value: T,
47 generation: usize
48}
49
50pub mod closed;
51pub mod exposed;