o3 0.62.0

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

use cycle::Policy;

use crate::{
    alloc,
    base::num,
    depot::{
        arena::slab::{self, external, key::cycle},
        capacity,
        queue::links,
    },
};

#[derive(Clone, Copy)]
pub(in crate::depot) struct SlabLinks {
    pub(in crate::depot) next: u32,
    pub(in crate::depot) prev: u32,
}

union Data<T> {
    links: SlabLinks,
    value: mem::ManuallyDrop<T>,
}

/// State, generation and position of one initialized slab slot.
pub(in crate::depot) struct SlabMetadata<G: Copy, B: Copy, R: Copy> {
    pub(in crate::depot) state: cell::Cell<slab::State<B, R>>,
    pub(in crate::depot) generation: cell::Cell<G>,
    pub(in crate::depot) position: cell::Cell<u32>,
}

pub struct SlabSlot<T, G: Copy, B: Copy, R: Copy> {
    pub(in crate::depot) metadata: SlabMetadata<G, B, R>,
    data: cell::UnsafeCell<Data<T>>,
}

impl<T, G: Copy, B: Copy, R: Copy> SlabSlot<T, G, B, R> {
    pub(in crate::depot) fn free(epoch: G, next: u32, prev: u32) -> Self {
        Self {
            metadata: SlabMetadata {
                state: cell::Cell::new(slab::State::Free),
                generation: cell::Cell::new(epoch),
                position: cell::Cell::new(links::Index::END),
            },
            data: cell::UnsafeCell::new(Data {
                links: SlabLinks { next, prev },
            }),
        }
    }

    pub(in crate::depot) unsafe fn links(&self) -> SlabLinks {
        // SAFETY: caller proves Free/Reserved state selects the SlabLinks union member.
        unsafe { (*self.data.get()).links }
    }

    pub(in crate::depot) unsafe fn set_links(&self, links: SlabLinks) {
        // SAFETY: caller exclusively owns a non-value state before writing SlabLinks.
        unsafe { (*self.data.get()).links = links };
    }

    pub(in crate::depot) unsafe fn set_next(&self, next: u32) {
        // SAFETY: caller proves Free/Reserved state selects the SlabLinks union member.
        unsafe { (*self.data.get()).links.next = next };
    }

    pub(in crate::depot) unsafe fn set_prev(&self, prev: u32) {
        // SAFETY: caller proves Free/Reserved state selects the SlabLinks union member.
        unsafe { (*self.data.get()).links.prev = prev };
    }

    pub(in crate::depot) unsafe fn value(&self) -> &T {
        // SAFETY: caller proves Occupied/Busy state selects an initialized value.
        unsafe { &*(&raw const (*self.data.get()).value).cast::<T>() }
    }

    pub(in crate::depot) unsafe fn value_ptr(&self) -> *mut T {
        // SAFETY: caller owns this value storage. Reserved bytes may only be
        // initialized; an initialized value retains its existing access bounds.
        unsafe { (&raw mut (*self.data.get()).value).cast::<T>() }
    }

    pub(in crate::depot) unsafe fn take_value(&self) -> T {
        // SAFETY: caller owns the initialized value and transitions away from Occupied.
        unsafe { mem::ManuallyDrop::take(&mut (*self.data.get()).value) }
    }

    pub(in crate::depot) unsafe fn drop_value(&self) {
        // SAFETY: caller owns the initialized value and transitions away from Occupied.
        unsafe { mem::ManuallyDrop::drop(&mut (*self.data.get()).value) };
    }
}

pub(in crate::depot) struct SlabCapacity<T, G: Copy, B: Copy, R: Copy> {
    pub(in crate::depot) count: capacity::Count,
    slot: SlotMarker<SlabSlot<T, G, B, R>>,
}

type SlotMarker<S> = marker::PhantomData<fn() -> S>;

impl<T, G: Copy, B: Copy, R: Copy> SlabCapacity<T, G, B, R> {
    pub(in crate::depot) fn new(count: capacity::Count) -> Result<Self, alloc::Overflow> {
        use alloc::raw::Rows;

        if count.get() > Self::MAX {
            return Err(alloc::Overflow);
        }
        Ok(Self {
            count,
            slot: marker::PhantomData,
        })
    }
}

unsafe impl<T, G: Copy, B: Copy, R: Copy> alloc::raw::Shape for SlabCapacity<T, G, B, R> {
    type Root = SlabSlot<T, G, B, R>;
    type Tail = cell::Cell<mem::MaybeUninit<u32>>;
    type Extra = ();
    type Last = ();

    fn len(&self) -> usize {
        self.count.get()
    }
}

unsafe impl<T, G: Copy, B: Copy, R: Copy> alloc::raw::Rows for SlabCapacity<T, G, B, R> {}

const _: () = {
    assert!(mem::size_of::<slab::State<(), ()>>() == 1);
    assert!(mem::size_of::<slab::State<(), convert::Infallible>>() == 1);
    assert!(mem::size_of::<slab::State<convert::Infallible, ()>>() == 1);
    assert!(mem::size_of::<slab::State<convert::Infallible, convert::Infallible>>() == 1);
    assert!(
        mem::size_of::<SlabSlot<u64, u32, (), ()>>()
            == mem::size_of::<SlabSlot<u64, u32, (), convert::Infallible>>()
    );
    assert!(
        mem::size_of::<SlabSlot<u64, u32, (), ()>>()
            == mem::size_of::<SlabSlot<u64, u32, convert::Infallible, ()>>()
    );
    assert!(
        mem::size_of::<SlabSlot<u64, u32, (), ()>>()
            == mem::size_of::<SlabSlot<u64, u32, convert::Infallible, convert::Infallible>>()
    );
};

// SAFETY: the proof retains this exact pinned owner's initialized slot and shared loan.
unsafe impl<'a, T, G: num::Step, Tag> crate::depot::raw::Project<u32>
    for &'a external::Pinned<T, G, Tag>
{
    type Output = &'a T;

    unsafe fn project(self, index: u32) -> Self::Output {
        // SAFETY: this exact live index selects a stamped value in the original shared owner.
        &unsafe { self.core.read(links::Index::new_raw(index)) }.value
    }
}

// SAFETY: the original exclusive pinned loan survives in the projected occupied phase.
unsafe impl<'a, T, G: num::Step, Tag> crate::depot::raw::Project<u32>
    for Pin<&'a mut external::Pinned<T, G, Tag>>
{
    type Output = external::raw::Anchored<'a, T, G, Tag>;

    unsafe fn project(self, index: u32) -> Self::Output {
        // SAFETY: the live proof retains the exact non-sentinel index under this exclusive pin.
        unsafe { external::raw::Anchored::from_slot(self, links::Index::new_raw(index)) }
    }
}

// SAFETY: the live proof retains this exact pinned owner's initialized slot and shared loan.
unsafe impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
    crate::depot::raw::Project<u32> for &'a slab::Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
    cycle::Mode<RECYCLE>: Policy,
    S: slab::Storage,
{
    type Output = &'a T;

    unsafe fn project(self, index: u32) -> Self::Output {
        // SAFETY: a live slot is below the retained capacity, which excludes the sentinel.
        let index = unsafe { links::Index::new_raw(index) };
        // SAFETY: the original projection retains this initialized slot's shared loan.
        unsafe { self.core.read(index) }
    }
}

// SAFETY: the exact exclusive pinned loan remains in Anchored until its consuming transition.
unsafe impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
    crate::depot::raw::Project<u32>
    for Pin<&'a mut slab::Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>>
where
    cycle::Mode<RECYCLE>: Policy,
    S: slab::Storage,
{
    type Output = slab::raw::Anchored<'a, T, Tag, MAX, RECYCLE, PARTITIONS, S>;

    unsafe fn project(self, index: u32) -> Self::Output {
        // SAFETY: a live slot is below the retained capacity, which excludes the sentinel.
        let index = unsafe { links::Index::new_raw(index) };
        let store = self.into_ref().get_ref();
        // SAFETY: the original projection retains this exclusive pinned slot.
        let entry = unsafe { store.core.project(index) };
        // SAFETY: the live projection retains the original exclusive pinned store loan.
        unsafe { slab::raw::Anchored::pinned(entry) }
    }
}