o3 0.62.0

shared-nothing primitives
Documentation
use crate::buffer::{write, write::consume};

/// Exclusive editable prefix and staged writes for one buffer owner.
#[must_use = "commit the staged suffix after editing the prefix"]
#[repr(C)]
pub struct Edit<'a, const N: usize> {
    prefix: &'a mut [u8; N],
    pub(in crate::buffer::write) spare: write::raw::Spare<'a>,
}

/// Rejected initialized-prefix edit.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error {
    pub offset: usize,
    pub width: usize,
    pub len: usize,
}

impl Error {
    pub(in crate::buffer) const fn new(offset: usize, width: usize, len: usize) -> Self {
        Self { offset, width, len }
    }
}

impl<'a, const N: usize> Edit<'a, N> {
    /// Stages a new fixed prefix without publishing its owner.
    /// Unlike editing an existing prefix, `len` includes earlier staged bytes and
    /// this new prefix; `commit` publishes the complete staged write.
    pub fn try_new(spare: write::raw::Spare<'a>, prefix: &[u8; N]) -> Result<Self, consume::Error> {
        write::raw::edit(spare, prefix)
    }

    pub(in crate::buffer) fn new(prefix: &'a mut [u8; N], spare: write::raw::Spare<'a>) -> Self {
        Self { prefix, spare }
    }

    /// Stages another suffix while retaining this edit's disjoint fixed prefix.
    pub fn spare(&mut self) -> write::raw::Spare<'_> {
        write::SpareExt::spare(&mut self.spare)
    }

    pub fn prefix(&mut self) -> &mut [u8; N] {
        self.prefix
    }

    pub fn parts(&mut self) -> (&mut [u8; N], write::raw::Spare<'_>) {
        (&mut *self.prefix, write::SpareExt::spare(&mut self.spare))
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        self.spare.len()
    }

    pub fn commit(self) {
        write::Commit::commit(self.spare);
    }
}

const _: () = assert!(size_of::<Edit<'static, 1>>() == 4 * size_of::<usize>());
const _: () = assert!(size_of::<Error>() == 3 * size_of::<usize>());