o3 0.60.0

shared-nothing primitives
Documentation
use std::mem::MaybeUninit;

use crate::buffer::{pool, raw, resident, storage};

/// # Safety
/// Nonempty fixed capacity retains one stable allocation; its initialized prefix fits.
/// Moves and shared access preserve backing; `spare` exclusively covers its suffix.
/// `truncate` keeps `min(len, current_len)`; `complete` extends by exactly `amount`.
pub unsafe trait Target: AsRef<[u8]> {
    fn capacity(&self) -> usize;
    fn truncate(&mut self, len: usize);

    /// # Safety
    /// Writes may initialize bytes, but must never deinitialize the backing.
    unsafe fn spare(&mut self) -> &mut [MaybeUninit<u8>];

    /// # Safety
    /// Native access ended and initialized `amount` bytes of this unchanged suffix.
    unsafe fn complete(&mut self, amount: usize);
}

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

// 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: raw::Backing, S: pool::state::State> Stable for pool::Span<K, pool::Owned, S> where
    Self: raw::Exclusive
{
}

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

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

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

    unsafe fn complete(&mut self, amount: usize) {
        // The caller proves the exact initialized extension fits the u32 capacity.
        *self.len_mut() += amount as u32;
    }
}