o3 0.60.0

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

use crate::{
    buffer,
    buffer::{bytes::scope, storage::range, write::consume},
};
/// A validated prefix tied to its exclusive original source.
pub type Consumed<'a, S, const MAX: u32> = buffer::Input<&'a mut S, consume::scope::Prefix<MAX>>;
/// One exclusive target paired with its original owner's selected range.
pub type Slice<'a, 'id, S> = Selection<'a, S, scope::Range<'id>>;

pub(in crate::buffer) type Selection<'a, S, P = usize> = buffer::Input<&'a mut S, P>;

impl<S, P> buffer::Input<S, P> {
    /// # Safety
    /// The source retains the original owner and unchanged bytes for this proof.
    pub unsafe fn from_parts(source: S, proof: P) -> Self {
        Self { source, proof }
    }

    pub fn into_parts(self) -> (S, P) {
        (self.source, self.proof)
    }
}

impl<S> Selection<'_, S> {
    pub(in crate::buffer) const fn len(&self) -> usize {
        self.proof
    }
}

impl<'a, S, const MAX: u32> From<Consumed<'a, S, MAX>> for Selection<'a, S> {
    fn from(prefix: Consumed<'a, S, MAX>) -> Self {
        let (source, end) = prefix.into_parts();
        Self {
            source,
            proof: end.count().get() as usize,
        }
    }
}

impl<'a, S: buffer::raw::Source> Selection<'a, S> {
    /// Admits one prefix length and retains its exact exclusive source.
    pub(in crate::buffer) fn try_new(
        source: &'a mut S,
        len: usize,
    ) -> Result<Self, consume::Error> {
        consume::Error::check(len, source.as_ref().len())?;
        Ok(Self { source, proof: len })
    }

    pub(in crate::buffer) fn new(source: &'a mut S, max: usize) -> Self {
        let len = max.min(source.as_ref().len());
        Self { source, proof: len }
    }
}

impl<'d> Selection<'_, &'d mut [u8]> {
    pub(in crate::buffer) fn take(self) -> &'d mut [u8] {
        let (source, len) = self.into_parts();
        let bytes = mem::take(source);
        // SAFETY: this prefix retains the exact admitted slice through exclusive mutation.
        let (head, tail) = unsafe { bytes.split_at_mut_unchecked(len) };
        *source = tail;
        head
    }
}

impl<'a, S: buffer::raw::Store> Slice<'a, '_, S> {
    /// Selections cannot escape the exclusive operation.
    pub fn try_with<T>(
        source: &'a mut S,
        bounds: ops::Range<usize>,
        run: impl for<'id> FnOnce(Slice<'a, 'id, S>) -> T,
    ) -> Result<T, range::Error> {
        scope::Owner::with(source, |owner| {
            let proof = owner.borrow().try_get(bounds)?;
            Ok(run(Selection {
                source: owner.into_inner(),
                proof,
            }))
        })
    }

    pub(in crate::buffer) fn try_project(
        mut source: S,
        bounds: ops::Range<usize>,
    ) -> Result<S, crate::mem::Rejected<S, range::Error>> {
        match Slice::try_with(&mut source, bounds, S::project) {
            Ok(()) => Ok(source),
            Err(error) => Err(crate::mem::Rejected::new(source, error)),
        }
    }

    pub(super) fn apply(self) {
        if self.proof.is_empty()
            && let Some(empty) = S::empty()
        {
            *self.source = empty;
            return;
        }
        S::restrict(self);
    }
}

const _: () = {
    assert!(size_of::<Slice<'static, 'static, Vec<u8>>>() == 3 * size_of::<usize>());
    assert!(
        size_of::<Option<Slice<'static, 'static, Vec<u8>>>>()
            == size_of::<Slice<'static, 'static, Vec<u8>>>()
    );
};