use o3::{
depot,
depot::map::{compact, compact::Insert as _},
};
use crate::payload::compress::dynamic::{
index::Key,
mode::Direct,
table::{Storage, storage::PartsMut},
};
pub(super) unsafe fn insert(storage: &mut Storage<Direct>, index: u32) {
let mut parts = storage.parts_mut();
let name = Directory::<0, false>(&mut parts, index).insert();
name.exact().insert();
}
pub(super) unsafe fn remove(storage: &mut Storage<Direct>, index: u32) {
let mut parts = storage.parts_mut();
let name = compact::Found::new(Directory::<0, true>(&mut parts, index)).remove();
compact::Found::new(name.exact()).remove();
}
struct Directory<'a, 'table, const N: usize, const PRESENT: bool>(
&'a mut PartsMut<'table, Direct>,
u32,
);
struct Done<T>(T);
impl<'a, 'table, const PRESENT: bool> Done<Directory<'a, 'table, 0, PRESENT>> {
fn exact(self) -> Directory<'a, 'table, 1, PRESENT> {
let Directory(storage, index) = self.0;
Directory(storage, index)
}
}
impl<const N: usize, const PRESENT: bool> Directory<'_, '_, N, PRESENT> {
fn member(&self) -> compact::Value<'_, Self> {
compact::Value::new(unsafe { depot::raw::Proof::new((self, self.1)) })
}
}
unsafe impl<const N: usize, const PRESENT: bool> compact::raw::Read
for Directory<'_, '_, N, PRESENT>
{
fn layout(&self) -> compact::Layout {
self.0.state.layout.index.layout(Key::staged::<N>())
}
fn bytes(&self) -> &[u8] {
self.0.index
}
}
unsafe impl<const N: usize, const PRESENT: bool> compact::raw::Source
for Directory<'_, '_, N, PRESENT>
{
fn hash(value: compact::Value<'_, Self>) -> u64 {
let parts = &*value.owner().0;
let record = unsafe { parts.ledger.get_unchecked(value.index()) };
Key::staged::<N>().of(record.digest())
}
}
unsafe impl<const N: usize, const PRESENT: bool> compact::raw::Write
for Directory<'_, '_, N, PRESENT>
{
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
self.0.index
}
}
unsafe impl<const N: usize> compact::raw::Insert for Directory<'_, '_, N, false> {
type Output = Done<Self>;
fn input(&self) -> compact::Value<'_, Self> {
self.member()
}
unsafe fn inserted(self) -> Self::Output {
Done(self)
}
}
unsafe impl<const N: usize> compact::raw::Member for Directory<'_, '_, N, true> {
fn input(&self) -> compact::Value<'_, Self> {
self.member()
}
}
unsafe impl<const N: usize> compact::raw::Remove for Directory<'_, '_, N, true> {
type Output = Done<Self>;
fn removed(removed: compact::Removed<Self>) -> Self::Output {
Done(unsafe { compact::raw::Remove::into_owner(removed) })
}
}