o3 0.59.0

shared-nothing primitives
Documentation
use crate::{
    buffer,
    buffer::{
        segments,
        segments::{Chain, Key, Pool, Totals, chain::edit::Part, handles},
        storage,
    },
    depot::arena::linked::{fifo::ends, slots},
};

/// Fresh, unaliased metadata capacity owned by one prepared build.
pub struct Exact<T, S> {
    handle: Pool<T>,
    source: S,
}

impl<T: buffer::raw::Segment, S: buffer::raw::Sequence<T>> Exact<T, S> {
    pub fn try_new(source: S) -> Result<Self, crate::alloc::Error> {
        let handle = Exact::try_with_totals(source.totals())?.handle;
        Ok(Self { handle, source })
    }

    pub fn try_collect(self) -> Result<segments::Many<T>, S::Error> {
        let mut owners = self.source.owners();
        let exact = Exact {
            handle: self.handle,
            source: self.source.totals(),
        };
        // SAFETY: the immutable sequence retains its admitted totals and yields the corresponding owners.
        unsafe { exact.try_collect_from(&mut owners) }
    }
}

impl<'a, T: buffer::raw::Segment> Exact<T, &'a Totals> {
    pub(in crate::buffer) fn try_with_totals(
        source: &'a Totals,
    ) -> Result<Self, crate::alloc::Error> {
        let parts = handles::Parts::try_new(segments::Plan {
            segments: source.count.get() as usize,
            chains: 1,
        })?;
        Ok(Self {
            handle: Pool::allocate(parts)?,
            source,
        })
    }

    /// # Safety
    /// Success yields the admitted count of at least two owners with these logical
    /// and resident lengths. Failure yields at most that count before its error.
    pub(in crate::buffer) unsafe fn try_collect_from<E>(
        self,
        owners: &mut impl Iterator<Item = Result<storage::NonEmpty<T>, E>>,
    ) -> Result<segments::Many<T>, E> {
        let nodes = handles::nodes(&self.handle);
        let mut links = ends::Exclusive::empty();
        for value in owners {
            let value = value?;
            // SAFETY: each owner fits the private pool. On error the pool drops
            // the initialized prefix before the caller's borrowed iterator.
            unsafe { slots::Vacant::insert_one(nodes, value).append(&mut links) };
        }
        Ok(unsafe { self.handle.finish(links, *self.source) })
    }
}

impl<T: buffer::raw::Segment> Exact<T, Totals> {
    #[inline(always)]
    pub(in crate::buffer::segments) fn try_reserve(
        current: segments::Count,
        growth: Totals,
    ) -> Result<Self, crate::alloc::Error> {
        let required = segments::Count::new(growth.count.get());
        let handle = Pool::allocate(handles::Parts::try_reserve(current, required)?)?;
        Ok(Self {
            handle,
            source: growth,
        })
    }

    /// # Safety
    /// These are the two unchanged parts admitted by this reservation's growth.
    pub(in crate::buffer::segments) unsafe fn join(
        self,
        first: impl Part<T>,
        second: impl Part<T>,
    ) -> segments::Many<T> {
        let nodes = handles::nodes(&self.handle);
        // SAFETY: both nonempty parts fit the private pool and own disjoint runs in input order.
        unsafe {
            let mut links = first.move_to(nodes);
            links.append_chain(nodes, second.move_to(nodes));
            self.handle.finish(links, self.source)
        }
    }
}

impl<T: buffer::raw::Segment> Pool<T> {
    /// # Safety
    /// This pool owns the complete partition described by the totals, at least two
    /// nodes, and one vacant descriptor.
    unsafe fn finish(
        self,
        links: ends::Exclusive<slots::Index>,
        totals: Totals,
    ) -> segments::Many<T> {
        let descs = handles::descriptors(&self);
        // SAFETY: the pool owns one available descriptor for this completed sequence.
        let claim =
            unsafe { slots::Vacant::insert_one(descs, handles::Descriptor { links, totals }) };
        let key = Key::multi(unsafe { claim.into_index() });
        let mut sequence = Chain::new(self);
        sequence.key = key;
        // SAFETY: the published descriptor owns the complete multi-node partition.
        unsafe { segments::Many::new(sequence) }
    }
}

const _: () = assert!(
    size_of::<Exact<storage::Shared, &segments::Many<storage::Shared>>>() == 2 * size_of::<usize>()
);