sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use o3::{
    depot,
    depot::map::{compact, compact::Insert as _},
};

use crate::payload::compress::dynamic::{
    index::Key,
    mode::Direct,
    table::{Storage, storage::PartsMut},
};

/// # Safety
/// The row must be initialized in this storage and absent from both directories.
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();
}

/// # Safety
/// The row must remain initialized and present in both directories until removal.
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> {
        // SAFETY: insertion wrote this row, while eviction retains the live oldest row.
        compact::Value::new(unsafe { depot::raw::Proof::new((self, self.1)) })
    }
}

// SAFETY: the exclusive storage retains the layout and both directory planes.
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
    }
}

// SAFETY: admitted and indexed rows retain their initialized digest throughout each stage.
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())
    }
}

// SAFETY: both byte projections belong to the exclusively retained directory.
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
    }
}

// SAFETY: each stage receives the new initialized row before its directory indexes it.
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)
    }
}

// SAFETY: the oldest row remains a member of this directory until removal.
unsafe impl<const N: usize> compact::raw::Member for Directory<'_, '_, N, true> {
    fn input(&self) -> compact::Value<'_, Self> {
        self.member()
    }
}

// SAFETY: only the eviction owner receives the storage after removing this membership.
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) })
    }
}