o3 0.59.0

shared-nothing primitives
Documentation
use std::num::NonZero;

pub(in crate::buffer::segments::chain) use join::Promotion;
pub(in crate::buffer::segments) use remove::{Multi, Remaining};
use segments::{Many, assembly, assembly::total};

use crate::{
    buffer,
    buffer::{
        segments,
        segments::{Chain, Handle, Key, handles},
        storage::NonEmpty,
    },
    depot::{
        arena::linked::{fifo::ends, slots},
        capacity,
    },
};

pub(in crate::buffer::segments) mod drain;

mod insert;
mod join;
mod remove;

pub(in crate::buffer::segments) struct Single<'a, T, H: Handle<T>> {
    pub(in crate::buffer::segments) sequence: &'a mut Chain<T, H>,
    pub(in crate::buffer::segments) index: slots::Index,
}

impl<T: buffer::raw::Segment, H: Handle<T>> Single<'_, T, H> {
    pub(in crate::buffer::segments::chain::edit) fn get_mut(&mut self) -> &mut NonEmpty<T> {
        // SAFETY: Single retains this live index and the sequence's exclusive loan.
        unsafe { &mut *handles::nodes(&self.sequence.handle).value_ptr(self.index) }
    }

    pub(in crate::buffer::segments) fn take(self) -> NonEmpty<T> {
        self.sequence.key = Key::EMPTY;
        unsafe { handles::nodes(&self.sequence.handle).detached(self.index) }
            .take()
            .0
    }

    /// Consumes this whole live key: the retained segment is destroyed and its
    /// slot returns to the store, exactly as releasing the taken owner would.
    pub(in crate::buffer::segments) fn discard(self) {
        self.sequence.key = Key::EMPTY;
        // SAFETY: the nonempty key names this live node exclusively, so the
        // detached slot owns its initialized value and its vacant entitlement.
        drop(unsafe { handles::nodes(&self.sequence.handle).detached(self.index) });
    }
}

pub(in crate::buffer::segments) enum Cause {
    Nodes(capacity::Shortage),
    Descriptors(capacity::Shortage),
}

impl Cause {
    pub(in crate::buffer::segments::chain) const ONE_NODE: Self =
        Self::Nodes(capacity::Shortage::ONE);

    pub(in crate::buffer::segments) fn into_error(self) -> segments::Error {
        match self {
            Self::Nodes(cause) => segments::Error::SegmentCapacity(cause),
            Self::Descriptors(cause) => segments::Error::ChainCapacity(cause),
        }
    }

    pub(in crate::buffer::segments) fn allocation(
        self,
        error: crate::alloc::Error,
    ) -> assembly::Error {
        match self {
            Self::Nodes(shortage) => assembly::Error::Nodes {
                shortage,
                allocation: error,
            },
            Self::Descriptors(shortage) => assembly::Error::Descriptors {
                shortage,
                allocation: error,
            },
        }
    }
}

impl segments::Totals {
    pub(in crate::buffer::segments) fn try_pair<T: buffer::raw::Segment>(
        first: &NonEmpty<T>,
        second: &NonEmpty<T>,
    ) -> Result<Self, total::Error> {
        Self::try_new(
            segments::Count::ONE,
            first.len().get(),
            buffer::Residency::resident(first),
            second,
        )
    }

    pub(in crate::buffer::segments::chain) fn try_new<T: buffer::raw::Segment>(
        current: segments::Count,
        len: usize,
        resident: usize,
        item: &NonEmpty<T>,
    ) -> Result<Self, total::Error> {
        let added = segments::Count::ONE;
        let count = total::Error::count(current, added)?;
        let added = item.len().get();
        let len = total::Error::logical(len, added)?;
        let added = buffer::Residency::resident(item);
        let resident = total::Error::resident(resident, added)?;
        Ok(Self {
            // SAFETY: checked addition of one excludes zero.
            count: unsafe { NonZero::new_unchecked(count.into_raw()) },
            len,
            resident,
        })
    }
}

pub(in crate::buffer::segments) trait Part<T> {
    /// # Safety
    /// The distinct target has room for the entire part. The returned partition owns its nonempty run.
    unsafe fn move_to(self, nodes: &slots::Store<NonEmpty<T>>) -> ends::Exclusive<slots::Index>;
}

impl<T> Part<T> for NonEmpty<T> {
    unsafe fn move_to(self, nodes: &slots::Store<NonEmpty<T>>) -> ends::Exclusive<slots::Index> {
        let index = unsafe { slots::Vacant::insert_one(nodes, self).into_index() };
        unsafe { ends::Exclusive::from_ends(index, index) }
    }
}

impl<T: buffer::raw::Segment> Part<T> for Many<T> {
    unsafe fn move_to(
        mut self,
        nodes: &slots::Store<NonEmpty<T>>,
    ) -> ends::Exclusive<slots::Index> {
        let source = &mut self.0;
        // SAFETY: this Many owns the whole descriptor and its tracked node partition.
        let index = unsafe { source.key.multi_index() };
        let desc = unsafe { handles::descriptors(&source.handle).take::<_, true>(index) };
        source.key = Key::EMPTY;
        // SAFETY: the fresh target owns capacity for this entire unchanged source.
        let reserve =
            unsafe { slots::Reserve::new(nodes, capacity::Count::new(desc.totals.count.get())) };
        unsafe { reserve.move_from(handles::nodes(&source.handle), desc.links) }
    }
}