o3 0.59.0

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

use crate::{
    buffer,
    buffer::{bytes::scope, storage::range, write::consume},
};
/// A validated prefix tied to the exclusive owner and the input operation.
pub type Consumed<'a, 'id, S, const MAX: u32> = Selection<'a, S, consume::scope::End<'id, 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 struct Selection<'a, S: ?Sized, P = usize> {
    source: &'a mut S,
    proof: P,
}

impl<'a, S: ?Sized, P> Selection<'a, S, P> {
    /// # Safety
    /// The source must retain the original owner and unchanged bytes for which the proof was issued.
    pub unsafe fn from_parts(source: &'a mut S, proof: P) -> Self {
        Self { source, proof }
    }

    /// # Safety
    /// The projection must preserve the original owner and its exact visible bytes.
    pub unsafe fn map<T: ?Sized>(
        self,
        project: impl FnOnce(&'a mut S) -> &'a mut T,
    ) -> Selection<'a, T, P> {
        Selection {
            source: project(self.source),
            proof: self.proof,
        }
    }

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

impl buffer::bytes::Cursor<'_, '_> {
    #[inline]
    pub fn commit(self) -> usize {
        Selection {
            source: self.target,
            proof: self.consumed,
        }
        .consume();
        self.consumed
    }
}

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>>>()
    );
};