o3 0.60.0

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

use crate::depot::{
    arena::slab::{
        self, entries,
        key::{self, cycle::Policy},
        storage::{self, Plane},
    },
    queue::links,
};

pub struct Entry<
    'a,
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    P,
    S: storage::Storage,
    const LIVE: bool,
    const ISSUED: bool,
> {
    pub(in crate::depot) core: &'a entries::Core<T, MAX, M, N, S>,
    pub(in crate::depot) slot: &'a storage::Slot<T, MAX, M>,
    pub(in crate::depot) plane: marker::PhantomData<fn(P) -> P>,
}

impl<
    'a,
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    P,
    S: storage::Storage,
    const LIVE: bool,
    const ISSUED: bool,
> Entry<'a, T, MAX, M, N, P, S, LIVE, ISSUED>
{
    pub(in crate::depot) fn slot_index(&self) -> links::Index {
        // SAFETY: this entry retains its exact slot in the original allocation.
        unsafe { self.slot.index(self.core.slots.as_ref()) }
    }

    pub(in crate::depot) fn generation(&self) -> key::Generation<MAX> {
        <M::Cycle as Policy>::generation(self.slot.metadata.generation.get())
    }

    /// # Safety
    /// The caller completes this exact slot's initialization or consuming transition.
    pub(super) unsafe fn phase<const NEXT_LIVE: bool, const NEXT_ISSUED: bool>(
        self,
    ) -> Entry<'a, T, MAX, M, N, P, S, NEXT_LIVE, NEXT_ISSUED> {
        let this = mem::ManuallyDrop::new(self);
        Entry {
            core: this.core,
            slot: this.slot,
            plane: this.plane,
        }
    }

    fn unlink_live(&mut self) {
        let plane = &self.core.slots;
        let pos = self.slot.metadata.position.get();
        let tail = self.core.live.get().wrapping_sub(1);
        if pos != tail {
            // SAFETY: the live entry owns one position of this initialized dense prefix.
            unsafe {
                let last = plane
                    .tail()
                    .get_unchecked(tail as usize)
                    .get()
                    .assume_init();
                plane
                    .tail()
                    .get_unchecked(pos as usize)
                    .set(mem::MaybeUninit::new(last));
                plane
                    .as_ref()
                    .get_unchecked(last as usize)
                    .metadata
                    .position
                    .set(pos);
            }
        }
        self.core.live.set(tail);
    }

    fn release(&mut self) {
        let index = self.slot_index().raw();
        let head = self.core.partition(index).replace(index);
        // SAFETY: the entry owns this non-value slot and the original free partition.
        unsafe {
            self.slot.set_links(crate::depot::raw::SlabLinks {
                next: head,
                prev: entries::NONE,
            })
        };
        self.slot.metadata.state.set(slab::State::Free);
        if head != entries::NONE {
            // SAFETY: the previous head remains free in this exact owner.
            unsafe {
                self.core
                    .slots
                    .as_ref()
                    .get_unchecked(head as usize)
                    .set_prev(index)
            };
        }
    }

    fn retire(&mut self) {
        match <M::Cycle as Policy>::next::<MAX>(self.slot.metadata.generation.get()) {
            Ok(epoch) => {
                self.slot.metadata.generation.set(epoch);
                self.release();
            }
            Err(retired) => {
                self.slot.metadata.state.set(slab::State::Retired(retired));
                if let Some(count) = self.core.slots.retired() {
                    count.set(count.get().wrapping_add(1));
                }
            }
        }
    }
}

impl<
    'a,
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    P,
    S: storage::Storage,
    const ISSUED: bool,
> Entry<'a, T, MAX, M, N, P, S, true, ISSUED>
{
    pub(in crate::depot) fn index(&self) -> u32 {
        self.slot_index().raw()
    }

    pub(in crate::depot) fn value(&self) -> &T {
        // SAFETY: this entry's live phase retains the initialized value.
        unsafe { self.slot.value() }
    }

    pub(in crate::depot) fn value_mut(&mut self) -> &mut T {
        // SAFETY: the live phase retains the original exclusive slot loan.
        unsafe { &mut *self.slot.value_ptr() }
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, P, S: storage::Storage>
    Entry<'a, T, MAX, M, N, P, S, true, false>
{
    pub(in crate::depot::arena::slab) fn detach(self) -> entries::Pending<'a, T, MAX, M, N, P, S> {
        let mut this = mem::ManuallyDrop::new(self);
        this.unlink_live();
        Entry {
            core: this.core,
            slot: this.slot,
            plane: this.plane,
        }
    }

    pub(in crate::depot) fn remove(self) -> T {
        // SAFETY: the consuming live entry owns the value and immediately retires its slot.
        let value = unsafe { self.slot.take_value() };
        self.detach().reject();
        value
    }

    pub(in crate::depot) fn discard(self) {
        // SAFETY: ownership of this initialized value passes to the terminal phase.
        drop(unsafe { self.phase::<true, true>() });
    }

    pub(super) fn own(self) -> Entry<'a, T, MAX, M, N, P, S, true, true> {
        // SAFETY: the unchanged initialized entry transfers destruction responsibility.
        unsafe { self.phase() }
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, P, S: storage::Storage>
    Entry<'a, T, MAX, M, N, P, S, false, true>
{
    pub(in crate::depot::arena::slab) fn unissued(
        self,
    ) -> entries::Pending<'a, T, MAX, M, N, P, S> {
        // SAFETY: the caller continues the same reservation without releasing its identity.
        unsafe { self.phase() }
    }
}

impl<'a, T, const MAX: u32, M: entries::Mode, const N: usize, P, S: storage::Storage>
    Entry<'a, T, MAX, M, N, P, S, true, true>
{
    pub(in crate::depot::arena::slab) fn publish(self) -> entries::Busy<'a, T, MAX, M, N, P, S> {
        // SAFETY: publication transfers the same live slot back to its physical owner.
        unsafe { self.phase() }
    }
}

impl<
    T,
    const MAX: u32,
    M: entries::Mode,
    const N: usize,
    P,
    S: storage::Storage,
    const LIVE: bool,
    const ISSUED: bool,
> Drop for Entry<'_, T, MAX, M, N, P, S, LIVE, ISSUED>
{
    fn drop(&mut self) {
        if ISSUED {
            if LIVE {
                // SAFETY: this terminal phase owns the initialized value's sole destruction.
                unsafe { self.slot.drop_value() };
                self.unlink_live();
            }
            self.retire();
        } else if !LIVE {
            self.release();
        } else if M::REENTRANT {
            self.slot.metadata.state.set(slab::State::Occupied);
        }
    }
}