use std::{marker, ptr};
use crate::depot::map::direct;
#[must_use = "a vacant entry does nothing unless inserted"]
pub struct Vacant<'a, K: crate::depot::raw::Key, V, P = ()> {
pub(in crate::depot::map::direct) table: &'a mut direct::Table<K, V>,
pub(in crate::depot::map::direct) key: K,
pub(in crate::depot::map::direct) plane: marker::PhantomData<fn(P) -> P>,
}
impl<'a, K: crate::depot::raw::Key, V, P> Vacant<'a, K, V, P> {
pub fn key(&self) -> K {
self.key
}
pub fn table(&self) -> &direct::Table<K, V> {
self.table
}
pub fn as_mut_ptr(&mut self) -> ptr::NonNull<V> {
unsafe {
let entry = self.table.block.as_mut_ptr().add(self.key.index());
ptr::NonNull::new_unchecked(ptr::addr_of_mut!((*entry).value).cast())
}
}
pub unsafe fn assume_init(self) -> direct::Occupied<'a, K, V> {
let row =
ptr::NonNull::from(unsafe { self.table.block.get_unchecked_mut(self.key.index()) });
unsafe { self.table.publish(row, self.key) };
direct::Occupied {
table: self.table,
key: self.key,
plane: marker::PhantomData,
}
}
pub fn insert(mut self, value: V) -> direct::Occupied<'a, K, V> {
unsafe { self.as_mut_ptr().as_ptr().write(value) };
unsafe { self.assume_init() }
}
}