thunderation 0.2.0

Fast arena-based map with compact generational indices
Documentation
use crate::{free_pointer::FreePointer, generation::Generation};

#[derive(Debug, Clone)]
pub(crate) enum Entry<V> {
    Occupied(OccupiedEntry<V>),
    Empty(EmptyEntry),
}

impl<V> Entry<V> {
    /// Consume the entry, and if it's occupied, return the value.
    pub(crate) fn into_value(self) -> Option<V> {
        match self {
            Entry::Occupied(occupied) => Some(occupied.value),
            Entry::Empty(_) => None,
        }
    }

    pub(crate) fn get_value_mut(&mut self, generation: Generation) -> Option<&mut V> {
        match self {
            Entry::Occupied(occupied) if occupied.generation == generation => {
                Some(&mut occupied.value)
            }
            _ => None,
        }
    }

    /// If the entry is empty, a reference to it.
    pub(crate) fn as_empty(&self) -> Option<&EmptyEntry> {
        match self {
            Entry::Empty(empty) => Some(empty),
            Entry::Occupied(_) => None,
        }
    }

    /// If the entry is empty, return a mutable reference to it.
    pub(crate) fn as_empty_mut(&mut self) -> Option<&mut EmptyEntry> {
        match self {
            Entry::Empty(empty) => Some(empty),
            Entry::Occupied(_) => None,
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct OccupiedEntry<V> {
    pub(crate) generation: Generation,
    pub(crate) value: V,
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct EmptyEntry {
    pub(crate) generation: Generation,
    pub(crate) next_free: Option<FreePointer>,
}