o3 0.60.0

shared-nothing primitives
Documentation
use std::{borrow::Borrow, fmt, ops};

use crate::{
    buffer,
    buffer::{
        hold, segments,
        storage::{self, regions},
        write::consume,
    },
    mem::rc,
};
pub trait Seal:
    Sized + Borrow<regions::Shared<Self::Backing>> + From<regions::Shared<Self::Backing>>
{
    type Backing: super::Retain;
    type Retention: Default + fmt::Debug;

    const EMPTY: Self;

    fn try_copy(
        source: Result<buffer::raw::Slices<'_, 1>, consume::Error>,
        domain: &Self::Retention,
    ) -> Result<storage::NonEmpty<Self>, hold::CopyError>;
}

/// Retained bytes that narrow without copying their payload.
/// # Safety
/// Shared access preserves the range, and consuming advances its exact prefix.
pub unsafe trait Segment:
    Seal + super::Source + super::Store + consume::Consume + buffer::Residency
{
    /// # Safety
    /// Preserve this owner's exact initialized extent and its original backing.
    unsafe fn extent(&mut self) -> &mut regions::Extent;

    fn try_share(view: &storage::NonEmpty<buffer::View<'_, Self>>) -> Result<Self, rc::Error>;

    fn try_clone(&self) -> Result<Self, rc::Error> {
        buffer::View::new(self).try_retain()
    }

    fn try_get(&self, range: ops::Range<usize>) -> Result<Option<Self>, rc::Error> {
        buffer::View::new(self)
            .get(range)
            .map(buffer::View::<Self>::try_retain)
            .transpose()
    }

    /// Retains an in-bounds owner subrange without copying its payload.
    /// # Safety
    /// `range` must lie within the owner's visible bytes.
    unsafe fn try_project(&self, range: ops::Range<usize>) -> Result<Self, rc::Error> {
        // SAFETY: the caller admits this range of the unchanged owner.
        unsafe { buffer::raw::view(self, range) }.try_retain()
    }

    /// Retains at most `limit` leading bytes without copying the payload.
    fn prefix(mut self, limit: usize) -> Self {
        buffer::bytes::scope::Range::select_max::<false, _>(&mut self, limit);
        self
    }

    /// Splits off at most `limit` visible bytes without copying the payload.
    fn try_split(mut self, limit: usize) -> Result<(Self, Option<Self>), (rc::Error, Self)> {
        let Some(tail) = segments::Tail::new(&mut self, limit) else {
            return Ok((self, None));
        };
        let prefix = match tail.try_commit() {
            Ok(result) => result,
            Err(error) => return Err((error, self)),
        };
        Ok((prefix, Some(self)))
    }

    /// Removes at most `amount` visible bytes without copying the payload.
    fn suffix(mut self, amount: usize) -> Option<Self> {
        segments::Tail::new(&mut self, amount)?.commit();
        Some(self)
    }
}
// SAFETY: the sealed owner retains the exact extent while shared. Exclusive projection
// only narrows it, and a retained view duplicates the same backing and admitted range.
unsafe impl<O> Segment for regions::Shared<O>
where
    O: regions::End + buffer::Residency + super::Retain,
    Self: Seal,
{
    unsafe fn extent(&mut self) -> &mut regions::Extent {
        unsafe { self.extent_mut() }
    }

    fn try_share(view: &storage::NonEmpty<buffer::View<'_, Self>>) -> Result<Self, rc::Error> {
        view.get_ref().try_clone()
    }
}