use std::{marker, ptr};
use crate::depot::map::direct::{self, sealed::Live};
pub struct Occupied<'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) row: ptr::NonNull<direct::raw::Entry<K, V>>,
pub(in crate::depot::map::direct) plane: marker::PhantomData<fn(P) -> P>,
}
impl<'a, K: crate::depot::raw::Key, V> Occupied<'a, K, V> {
pub fn from_live<C, P>(
live: Live<(&'a mut direct::Table<K, V>, C), P>,
) -> (Occupied<'a, K, V, P>, C) {
let ((table, context), index) = live.into_inner();
let row = unsafe { ptr::NonNull::new_unchecked(table.block.as_mut_ptr().add(index.get())) };
(
Occupied {
table,
row,
plane: marker::PhantomData,
},
context,
)
}
}
impl<'a, K: crate::depot::raw::Key, V, P> Occupied<'a, K, V, P> {
pub fn key(&self) -> K {
unsafe { direct::raw::Entry::identity(self.row) }
}
pub fn get(&self) -> &V {
unsafe { direct::raw::Entry::payload(self.row).as_ref() }
}
pub fn get_mut(&mut self) -> &mut V {
unsafe { direct::raw::Entry::payload(self.row).as_mut() }
}
pub fn into_mut(self) -> &'a mut V {
unsafe { direct::raw::Entry::payload(self.row).as_mut() }
}
pub fn into_uninit(self) -> direct::raw::Vacant<'a, K, V, P> {
let key = self.key();
unsafe { self.table.vacate(self.row) };
direct::raw::Vacant {
table: self.table,
key,
plane: self.plane,
}
}
pub fn take(self) -> (direct::raw::Vacant<'a, K, V, P>, V) {
let row = self.row;
let vacant = self.into_uninit();
(vacant, unsafe { direct::raw::Entry::payload(row).read() })
}
pub fn remove(self) -> V {
unsafe { self.table.vacate(self.row).read() }
}
pub fn discard(self) {
unsafe { self.table.vacate(self.row).drop_in_place() };
}
}