use std::{marker, mem, ptr};
use crate::depot::{
index::{Identity, Stored},
map::direct,
};
#[repr(C)]
pub(in crate::depot) struct Entry<K: crate::depot::raw::Key, V> {
pub(in crate::depot) key: K::Raw,
pub(in crate::depot) value: mem::MaybeUninit<V>,
domain: Identity<K>,
}
impl<K: crate::depot::raw::Key, V> Entry<K, V> {
pub(in crate::depot) fn vacant() -> Self {
Self {
key: K::VACANT,
value: mem::MaybeUninit::uninit(),
domain: marker::PhantomData,
}
}
pub(in crate::depot) fn read(&self) -> Option<(K, &V)> {
(self.key != K::VACANT).then(|| unsafe { self.live() })
}
pub(in crate::depot) unsafe fn live(&self) -> (K, &V) {
let key = K::from_raw(unsafe { Stored::new(self.key) });
(key, unsafe { self.value.assume_init_ref() })
}
pub(in crate::depot) unsafe fn occupied(
table: &mut direct::Table<K, V>,
index: usize,
) -> (K, ptr::NonNull<Self>) {
let row = unsafe { table.block.get_unchecked_mut(index) };
let key = unsafe { row.live() }.0;
(key, ptr::NonNull::from(row))
}
}
impl<K: crate::depot::raw::Key, V> Drop for Entry<K, V> {
fn drop(&mut self) {
if self.key == K::VACANT {
return;
}
unsafe { self.value.assume_init_drop() };
}
}