o3 0.62.0

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

use crate::{alloc, depot::map};

type Fields<P> = alloc::raw::Tail<
    alloc::raw::Tail<alloc::raw::Layout<u8>, mem::MaybeUninit<u64>>,
    mem::MaybeUninit<P>,
>;

pub struct Layout<P> {
    bundle: alloc::raw::Bundle<Fields<P>>,
    buckets: usize,
    plan: map::Plan,
}

impl<P> Layout<P> {
    pub(crate) const fn bundle(&self) -> &alloc::raw::Bundle<Fields<P>> {
        &self.bundle
    }

    pub const fn buckets(&self) -> usize {
        self.buckets
    }

    pub const fn shape(&self) -> map::raw::Shape<P> {
        map::raw::Shape {
            plan: self.plan,
            value: marker::PhantomData,
        }
    }

    pub fn try_plan(plan: map::Plan) -> Result<Self, crate::alloc::Overflow> {
        let buckets = plan.buckets();
        let bundle = alloc::raw::Recipe::try_build(Self::recipe(buckets))?;
        Ok(Self {
            bundle,
            buckets,
            plan,
        })
    }

    /// # Safety
    /// This exact plan and payload recipe previously passed layout admission.
    pub(in crate::depot::map) unsafe fn restore(plan: map::Plan) -> Self {
        let buckets = plan.buckets();
        let bundle = unsafe { alloc::raw::Recipe::build(Self::recipe(buckets)) };
        Self {
            bundle,
            buckets,
            plan,
        }
    }

    fn recipe(buckets: usize) -> impl alloc::raw::Recipe<Fields = Fields<P>> {
        use alloc::raw::Recipe as _;

        alloc::raw::Root::<u8>::new(buckets)
            .tail::<mem::MaybeUninit<u64>>(buckets)
            .tail::<mem::MaybeUninit<P>>(buckets)
    }
}

impl<P> alloc::raw::Recipe for Layout<P> {
    type Fields = Fields<P>;

    fn try_build(self) -> Result<alloc::raw::Bundle<Self::Fields>, alloc::Overflow> {
        Ok(self.bundle)
    }

    unsafe fn build(self) -> alloc::raw::Bundle<Self::Fields> {
        self.bundle
    }
}