o3 0.59.0

shared-nothing primitives
Documentation
use crate::{
    buffer,
    buffer::{
        segments,
        segments::{
            Chain, Front, Handle, Totals,
            chain::edit::{Cause, Promotion},
            handles::{self, Occupied},
        },
        storage::NonEmpty,
    },
    depot::{arena::linked::fifo::ends, capacity},
};

impl<T: buffer::raw::Segment, H: Handle<T>> Chain<T, H> {
    pub fn try_insert(
        &mut self,
        item: NonEmpty<T>,
    ) -> Result<Front<'_, T, H>, crate::mem::Rejected<NonEmpty<T>, segments::Error>> {
        let error = 'rejected: {
            let prior = self.key.state();
            // SAFETY: this exclusive chain retains the partition selected by its key.
            let (count, len, resident) = unsafe { self.totals_at(prior.as_ref()) };
            let growth = match Totals::try_new(count, len, resident, &item) {
                Ok(growth) => growth,
                Err(error) => break 'rejected error.into(),
            };
            let Some(node) = handles::nodes(&self.handle).vacant() else {
                break 'rejected Cause::ONE_NODE.into_error();
            };
            let promotion = if matches!(prior, Some(Occupied::Single(_))) {
                let Some(desc) = handles::descriptors(&self.handle).vacant() else {
                    break 'rejected segments::Error::ChainCapacity(capacity::Shortage::ONE);
                };
                Some(Promotion::Vacant(desc))
            } else {
                None
            };
            // SAFETY: the new singleton and any descriptor claim belong to this unchanged target.
            unsafe {
                let index = node.insert(item).into_index();
                let moved = ends::Exclusive::from_ends(index, index);
                self.key
                    .append(&self.handle, prior, promotion, moved, growth);
            }
            return Ok(Front { sequence: self });
        };
        Err(crate::mem::Rejected::new(item, error))
    }
}