o3 0.61.0

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

use resident::Admission as _;

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

/// # Safety
/// Selected views retain this source's unchanged initialized bytes.
/// Copies own those bytes for 'd with residency at most their length.
/// Retained edges preserve them for 'd within max(length, source residency).
/// Optional retention respects its limit and budget. Failure or decline leaves
/// the original owner and selection unchanged.
/// Ok(()) or Ok(true) initializes the exact borrowed destination once.
/// Err or Ok(false) performs no destination write.
pub unsafe trait Retain<'d>: AsRef<[u8]> + Residency {
    fn try_copy<T: From<storage::Owned<resident::Budget<'d>>>>(
        view: buffer::View<'_, Self>,
        budget: &resident::Budget<'d>,
        place: &mut MaybeUninit<T>,
    ) -> Result<(), Error> {
        storage::Owned::try_from_slice(budget, view.as_slice(), place).map_err(Error::Resident)
    }

    fn try_retain<T: From<Retained<'d>> + From<storage::Owned<resident::Budget<'d>>>>(
        view: buffer::View<'_, Self>,
        budget: &resident::Budget<'d>,
        place: &mut MaybeUninit<T>,
    ) -> Result<(), Error> {
        Self::try_copy(view, budget, place)
    }

    fn try_retain_with<T: From<Retained<'d>>>(
        _: buffer::View<'_, Self>,
        _: &resident::Budget<'d>,
        _: usize,
        _: &mut MaybeUninit<T>,
    ) -> Result<bool, Error> {
        Ok(false)
    }
}

// SAFETY: slices copy their selected immutable bytes and issue no retained source edge.
// The original copy initializer writes place once only after allocation succeeds.
unsafe impl<'d> Retain<'d> for [u8] {}
unsafe impl<'d> Retain<'d> for &[u8] {}

// SAFETY: the selected range borrows this exact provided owner and registration.
// Accounting writes this same place once after all fallible admission; decline/error writes none.
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 try_retain<T: From<Retained<'d>> + From<storage::Owned<resident::Budget<'d>>>>(
        view: buffer::View<'_, Self>,
        budget: &resident::Budget<'d>,
        place: &mut MaybeUninit<T>,
    ) -> Result<(), Error> {
        let std::ops::ControlFlow::Continue(()) = provided::Prefix::from_view(view)
            .try_account_with::<_, Retained<'d>, T>(*budget, (), place)
            .map_err(Error::Account)?;
        Ok(())
    }

    fn try_retain_with<T: From<Retained<'d>>>(
        view: buffer::View<'_, Self>,
        budget: &resident::Budget<'d>,
        limit: usize,
        place: &mut MaybeUninit<T>,
    ) -> Result<bool, Error> {
        Ok(provided::Prefix::from_view(view)
            .try_account_with::<_, Retained<'d>, T>(*budget, resident::Cap(limit), place)
            .map_err(Error::Account)?
            .is_continue())
    }
}

fn try_share<'d, O, T: From<Retained<'d>>>(
    view: buffer::View<'_, storage::Shared<Option<O>>>,
    place: &mut MaybeUninit<T>,
) -> Result<(), Error>
where
    Option<O>: buffer::raw::Retain + Residency + ResidentBacking<'d>,
{
    let owner = if view.is_empty() {
        Retained::EMPTY
    } else {
        view.try_clone()
            .map_err(resident::AllocError::Retain)?
            .into()
    };
    place.write(T::from(owner));
    Ok(())
}

// SAFETY: cloning the selected immutable range retains its original allocation edge.
// The shared initializer writes the borrowed place once only after successful retention.
unsafe impl<'d, O> Retain<'d> for storage::Shared<Option<O>>
where
    Option<O>: buffer::raw::Retain + Residency + ResidentBacking<'d>,
{
    fn try_retain<T: From<Retained<'d>> + From<storage::Owned<resident::Budget<'d>>>>(
        view: buffer::View<'_, Self>,
        _: &resident::Budget<'d>,
        place: &mut MaybeUninit<T>,
    ) -> Result<(), Error> {
        try_share(view, place)
    }

    fn try_retain_with<T: From<Retained<'d>>>(
        view: buffer::View<'_, Self>,
        budget: &resident::Budget<'d>,
        limit: usize,
        place: &mut MaybeUninit<T>,
    ) -> Result<bool, Error> {
        let resident = view.len().max(view.resident());
        let additional = view.source.owner().additional(*budget, resident);
        if resident::Cap(limit).check(*budget, additional).is_break() {
            return Ok(false);
        }
        try_share(view, place)?;
        Ok(true)
    }
}