use std::ops::Deref;
use sharded_slab::{Entry, OwnedEntry};
pub type Ref<'a, K, V> = RefWrapper<K, Entry<'a, V>>;
pub type OwnedRef<K, V> = RefWrapper<K, OwnedEntry<V>>;
pub trait SlabEntry {
type Value;
fn offset(&self) -> usize;
fn as_inner(&self) -> &Self::Value;
}
#[derive(Debug)]
pub struct RefWrapper<K, E> {
inner: E,
_marker: std::marker::PhantomData<K>,
}
impl<K, E: SlabEntry> RefWrapper<K, E> {
#[inline]
pub(super) const fn new(inner: E) -> Self {
Self {
inner,
_marker: std::marker::PhantomData,
}
}
#[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()
}
}