reslab 0.0.0

`HashMap` alternative, backed with slab storage.
Documentation
use std::ops::Deref;

use sharded_slab::{Entry, OwnedEntry};

/// A type that represents a borrowed reference to a value in a
/// [`super::ReSlab`].
pub type Ref<'a, K, V> = RefWrapper<K, Entry<'a, V>>;

/// A type that represents a owned reference to a value in a [`super::ReSlab`].
pub type OwnedRef<K, V> = RefWrapper<K, OwnedEntry<V>>;

/// A trait to abstract over getting the offset of a slab entry.
pub trait SlabEntry {
    type Value;

    /// Gets the offset to this entry in the slab.
    fn offset(&self) -> usize;

    /// Gets a reference to the value of this entry.
    fn as_inner(&self) -> &Self::Value;
}

/// A wrapper to wrap reference to a value in a [`ReSlab`].
#[derive(Debug)]
pub struct RefWrapper<K, E> {
    inner: E,
    _marker: std::marker::PhantomData<K>,
}

impl<K, E: SlabEntry> RefWrapper<K, E> {
    /// Creates a new instance of [`Ref`] with the provided slab entry.
    #[inline]
    pub(super) const fn new(inner: E) -> Self {
        Self {
            inner,
            _marker: std::marker::PhantomData,
        }
    }

    /// Gets the offset to this entry in the slab.
    #[inline]
    pub fn offset(&self) -> usize {
        self.inner.offset()
    }
}

impl<K, E: SlabEntry> Deref for RefWrapper<K, E> {
    type Target = E::Value;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.inner.as_inner()
    }
}

impl<V> SlabEntry for Entry<'_, V> {
    type Value = V;

    #[inline]
    fn offset(&self) -> usize {
        self.key()
    }

    #[inline]
    fn as_inner(&self) -> &V {
        self.deref()
    }
}

impl<V> SlabEntry for OwnedEntry<V> {
    type Value = V;

    #[inline]
    fn offset(&self) -> usize {
        self.key()
    }

    #[inline]
    fn as_inner(&self) -> &V {
        self.deref()
    }
}