o3 0.62.0

shared-nothing primitives
Documentation
use std::{cell, mem, pin};

use crate::{
    alloc::tail,
    depot::{
        arena::pool::{self, shared::Resident},
        capacity,
    },
};

pub mod release;

/// Resets a pinned value in place before its slot is recycled.
pub trait Recycle {
    type Seed: Copy;
    type Release;

    fn from_seed(seed: Self::Seed) -> Self;
    fn recycle(self: pin::Pin<&mut Self>) -> Self::Release;
}

#[derive(Debug)]
pub struct Header<T, C = ()> {
    pub(in crate::depot::arena::pool) free: cell::Cell<*mut pool::raw::Entry<T, C>>,
    pub(in crate::depot::arena::pool) context: mem::ManuallyDrop<C>,
}

/// Fixed initialized storage; leases return slots without changing their contents.
#[repr(transparent)]
pub struct Store<T> {
    block: tail::raw::Block<Header<T>, pool::raw::Entry<T>, capacity::Count>,
}

impl<T> Store<T> {
    pub fn try_new(capacity: capacity::Count) -> Result<Self, crate::alloc::Error>
    where
        T: Default,
    {
        Self::try_from_fn(capacity, |_| T::default())
    }

    /// Visits every index in `0..capacity` exactly once in order before publishing slots.
    /// The retained extent also admits zero residents without invoking `init`.
    pub fn try_from_fn(
        capacity: capacity::Count,
        init: impl FnMut(u32) -> T,
    ) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            block: pool::raw::Entry::try_new(capacity, (), init)?,
        })
    }

    pub fn vacancies(&self) -> &Header<T> {
        tail::raw::View::new(&self.block).header()
    }

    pub fn take(&self) -> Option<pool::raw::Unique<'_, T>> {
        self.vacancies().take()
    }
}

impl<T> Header<T> {
    pub fn take(&self) -> Option<pool::raw::Unique<'_, T>> {
        Some(pool::raw::Vacant::new(self)?.reserve())
    }
}

impl<T> Store<Resident<T>> {
    pub fn vacancy(&self) -> Option<pool::raw::Vacant<'_, Resident<T>>> {
        self.vacancies().vacancy()
    }
}

impl<T> Header<Resident<T>> {
    pub fn vacancy(&self) -> Option<pool::raw::Vacant<'_, Resident<T>>> {
        pool::raw::Vacant::new(self)
    }
}