o3 0.62.0

shared-nothing primitives
Documentation
use std::{mem::MaybeUninit, num::NonZero, slice::from_raw_parts_mut};

use crate::buffer::{
    self,
    pool::{self, owners, state},
    resident, storage,
};

// SAFETY: Plain pool allocations contain no resident account table.
unsafe impl buffer::raw::Backing for owners::Plain {}

// SAFETY: Fixed admission binds both dimensions to this plain allocation.
unsafe impl<const SLOTS: usize, const CAPACITY: usize> buffer::raw::Backing
    for owners::Plain<pool::Fixed<SLOTS, CAPACITY>>
{
    const SLOTS: Option<u32> = Some(SLOTS as u32);
    const CAPACITY: Option<NonZero<u32>> = NonZero::new(CAPACITY as u32);
}

// SAFETY: Width admission binds this capacity to every plain allocation it issues.
unsafe impl<const CAPACITY: usize> buffer::raw::Backing for owners::Plain<pool::Width<CAPACITY>> {
    const CAPACITY: Option<NonZero<u32>> = NonZero::new(CAPACITY as u32);
}
// SAFETY: Resident preserves the lifetime-bound form of an accounted pool.
unsafe impl<'d> buffer::raw::Backing for owners::Resident<'d> {
    const COUNTED: bool = true;

    unsafe fn release<S>(core: owners::raw::Ptr<Self, S>) {
        // SAFETY: the terminal edge transfers the allocation and its resident debit together.
        unsafe { owners::Accounts::release(core) };
    }
}

/// # Safety
/// The nonnull initialized prefix stays immutable during shared access and satisfies all slice rules.
/// A nonempty prefix stays retained in one live allocation for the complete loan.
pub(in crate::buffer) unsafe trait Prefix {
    fn prefix(&self) -> (*mut MaybeUninit<u8>, u32);

    fn as_slice(&self) -> &[u8] {
        let (pointer, len) = self.prefix();
        // SAFETY: the backend retains this exact initialized prefix for this borrow.
        unsafe { buffer::raw::range(pointer.cast(), 0, len as usize) }
    }
}

/// # Safety
/// The exact Prefix fits capacity, a disjoint valid slice span retained exclusively through each mutable loan.
/// Pure projections preserve provenance, owner, length and capacity; writes and growth preserve these proofs.
pub(in crate::buffer) unsafe trait Exclusive: Prefix {
    fn as_mut_ptr(&mut self) -> *mut MaybeUninit<u8>;

    fn len_mut(&mut self) -> &mut u32;

    fn capacity(&self) -> u32;

    fn remaining(&self) -> usize {
        (self.capacity() - self.prefix().1) as usize
    }

    fn as_mut_slice(&mut self) -> &mut [u8] {
        let pointer = self.as_mut_ptr();
        let len = self.len_mut();
        // SAFETY: this exclusive owner retains its exact initialized prefix.
        unsafe { buffer::raw::range_mut(pointer.cast(), 0, *len as usize) }
    }

    fn spare_parts(&mut self) -> (&mut u32, &mut [MaybeUninit<u8>]) {
        let capacity = self.capacity();
        let pointer = self.as_mut_ptr();
        let len = self.len_mut();
        let used = *len as usize;
        // SAFETY: the backend pairs this length word with its disjoint live
        // allocation, whose remaining suffix is exclusive and unpublished.
        let spare = unsafe { from_raw_parts_mut(pointer.add(used), capacity as usize - used) };
        (len, spare)
    }

    fn fill(&mut self, byte: u8) {
        let (len, spare) = self.spare_parts();
        let end = *len + spare.len() as u32;
        // SAFETY: each byte is exclusively borrowed and writes initialize the whole suffix.
        unsafe { spare.as_mut_ptr().write_bytes(byte, spare.len()) };
        *len = end;
    }
}

/// # Safety
/// Moving the owner preserves the exclusive allocation and its capacity.
unsafe trait Stable: Exclusive + AsRef<[u8]> {}

// SAFETY: moving the owned allocation preserves its backing and capacity.
unsafe impl Stable for storage::Owned {}

// SAFETY: moving the charged owner does not move or resize its heap.
unsafe impl Stable for storage::Owned<resident::Budget<'_>> {}

// SAFETY: Span retains stable slot storage; Exclusive excludes live prefix aliases.
unsafe impl<K: buffer::raw::Backing, S: state::State> Stable for pool::Span<K, pool::Owned, S> where
    Self: Exclusive
{
}

// SAFETY: the stable backing and exclusive prefix remain paired through native completion.
unsafe impl<T: Stable> buffer::raw::Target for T {
    fn capacity(&self) -> usize {
        Exclusive::capacity(self) as usize
    }

    fn truncate(&mut self, len: usize) {
        buffer::raw::Length::truncate(self.len_mut(), len);
    }

    unsafe fn spare(&mut self) -> &mut [MaybeUninit<u8>] {
        self.spare_parts().1
    }

    unsafe fn complete(&mut self, amount: usize) {
        *self.len_mut() += amount as u32;
    }

    unsafe fn set_len(&mut self, len: usize) {
        *self.len_mut() = len as u32;
    }
}

/// Exclusive initialized suffix retained through its output operation.
pub struct Output<'a, L = pool::Layout>
where
    owners::Plain<L>: buffer::raw::Backing,
{
    target: &'a mut pool::Span<owners::Plain<L>, pool::Owned, state::Initialized>,
}

impl<'a, L> From<&'a mut pool::Span<owners::Plain<L>, pool::Owned, state::Initialized>>
    for Output<'a, L>
where
    owners::Plain<L>: buffer::raw::Backing,
{
    fn from(target: &'a mut pool::Span<owners::Plain<L>, pool::Owned, state::Initialized>) -> Self {
        Self { target }
    }
}

impl<'a, L> Output<'a, L>
where
    owners::Plain<L>: buffer::raw::Backing,
{
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        Exclusive::remaining(self.target)
    }

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        // SAFETY: Initialized retains all bytes of this exclusive spare suffix.
        unsafe { self.target.spare_parts().1.assume_init_mut() }
    }

    /// # Safety
    /// The foreign operation must prove `len` fits this unchanged output.
    pub unsafe fn complete(self, len: NonZero<usize>) {
        // SAFETY: the foreign operation admitted this exact completion.
        unsafe { buffer::raw::Target::complete(self.target, len.get()) };
    }
}

const _: () = assert!(size_of::<Output<'_>>() == size_of::<usize>());