o3 0.60.0

shared-nothing primitives
Documentation
use resident::Admission as _;

use crate::buffer::{
    self,
    hold::{Error, Retained},
    resident, storage,
    storage::{provided, regions::ResidentBacking},
};

/// # Safety
/// Source and view bytes remain unchanged through shared access and these methods; Cursor/Complete selection and Read splitting preserve exact visible ranges and prefix/suffix.
/// Successful copies/retains preserve selected bytes, order, length, and ownership for 'd; backing identity may change.
pub unsafe trait Retain<'d>: buffer::Source {
    /// Bounds retained Residency by max(plan.len(), resident(plan)), including retries.
    fn resident<'a>(_: &Self::View<'a>) -> usize
    where
        Self: 'a,
    {
        0
    }

    /// Uniquely owns the copied bytes with output Residency at most their length.
    fn try_copy<'a>(
        plan: &Self::View<'a>,
        budget: &resident::Budget<'d>,
    ) -> Result<storage::Owned<resident::Budget<'d>>, Error>
    where
        Self: 'a,
    {
        storage::Owned::try_from_slice(budget, buffer::Read::as_slice(plan))
            .map_err(Error::Resident)
    }

    fn try_retain<'a>(
        plan: &Self::View<'a>,
        budget: &resident::Budget<'d>,
    ) -> Result<Retained<'d>, Error>
    where
        Self: 'a,
    {
        Self::try_copy(plan, budget).map(Into::into)
    }

    /// Optionally retains the original backing without copying payload bytes.
    /// Existing charges require residency at most `limit`; fresh charges must also fit.
    /// A decline or error preserves the borrowed plan for fallback.
    fn try_retain_with<'a>(
        _: &Self::View<'a>,
        _: &resident::Budget<'d>,
        _: usize,
    ) -> Result<Option<Retained<'d>>, Error>
    where
        Self: 'a,
    {
        Ok(None)
    }
}

// SAFETY: The immutable provided view retains its exact range and original registration.
unsafe impl<'d, L: provided::Layout> Retain<'d> for storage::Shared<provided::Owner<'d, L>>
where
    resident::Kind<'d>: From<provided::Account<'d, L>>,
{
    fn resident<'a>(plan: &Self::View<'a>) -> usize
    where
        Self: 'a,
    {
        plan.resident()
    }

    fn try_retain<'a>(
        plan: &Self::View<'a>,
        budget: &resident::Budget<'d>,
    ) -> Result<Retained<'d>, Error>
    where
        Self: 'a,
    {
        let std::ops::ControlFlow::Continue(owner) = provided::Prefix::from_view(*plan)
            .try_account_with(*budget, ())
            .map_err(Error::Account)?;
        Ok(owner)
    }

    fn try_retain_with<'a>(
        plan: &Self::View<'a>,
        budget: &resident::Budget<'d>,
        limit: usize,
    ) -> Result<Option<Retained<'d>>, Error>
    where
        Self: 'a,
    {
        Ok(provided::Prefix::from_view(*plan)
            .try_account_with(*budget, resident::Cap(limit))
            .map_err(Error::Account)?
            .continue_value())
    }
}

// SAFETY: The immutable view clones its original owner and preserves its exact range.
unsafe impl<'d, O> Retain<'d> for storage::Shared<Option<O>>
where
    Option<O>: buffer::raw::Retain + buffer::Residency + ResidentBacking<'d> + Unpin,
{
    fn resident<'a>(plan: &Self::View<'a>) -> usize
    where
        Self: 'a,
    {
        plan.resident()
    }

    fn try_retain<'a>(
        plan: &Self::View<'a>,
        _: &resident::Budget<'d>,
    ) -> Result<Retained<'d>, Error>
    where
        Self: 'a,
    {
        if plan.is_empty() {
            return Ok(Retained::EMPTY);
        }
        let inner = plan.try_clone().map_err(resident::AllocError::Retain)?;
        Ok(inner.into())
    }

    fn try_retain_with<'a>(
        plan: &Self::View<'a>,
        budget: &resident::Budget<'d>,
        limit: usize,
    ) -> Result<Option<Retained<'d>>, Error>
    where
        Self: 'a,
    {
        let resident = plan.as_slice().len().max(Self::resident(plan));
        let additional = plan.source.owner().additional(*budget, resident);
        if resident::Cap(limit).check(*budget, additional).is_break() {
            return Ok(None);
        }
        Self::try_retain(plan, budget).map(Some)
    }
}