o3 0.62.0

shared-nothing primitives
Documentation
use std::{num::NonZero, slice::from_ref};

pub use append::Append;
pub use reserved::Reserved;
pub use staged::Staged;

use crate::buffer;

/// A destination whose individual writes append completely or not at all.
pub trait Sink {
    type Error;

    fn write_byte(&mut self, byte: u8) -> Result<(), Self::Error> {
        self.write_slice(from_ref(&byte))
    }

    fn write_slice(&mut self, bytes: &[u8]) -> Result<(), Self::Error>;

    fn write_slices<const N: usize>(&mut self, slices: [&[u8]; N]) -> Result<(), Self::Error>;

    /// Copies exactly `source` into an empty owner, returning that owner on either outcome.
    /// Rejects its existing prefix before writing, or propagates the exact write failure.
    fn try_copy(mut self, source: &[u8]) -> Result<Self, (Self, CopyError<Self::Error>)>
    where
        Self: buffer::raw::Source + Sized,
    {
        if let Some(len) = NonZero::new(self.as_ref().len()) {
            return Err((self, CopyError::Prefix(len)));
        }
        if let Err(error) = self.write_slice(source) {
            return Err((self, CopyError::Write(error)));
        }
        Ok(self)
    }
}

pub trait Init: Sink {
    /// Initializes one complete source after capacity admission. A shortfall
    /// leaves the destination unchanged and does not call the initializer.
    fn try_init<I: buffer::raw::Initializer>(&mut self, source: I) -> Result<(), Self::Error>;
}

/// Failure to copy an exact source into an empty destination.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CopyError<E> {
    Prefix(NonZero<usize>),
    Write(E),
}

mod append;
pub mod consume;
pub mod edit;
pub mod raw;
mod reserved;
pub mod spans;
mod staged;

/// Publishes one initialized writer into its original target.
pub trait Commit: Sized {
    fn commit(out: raw::Spare<'_, 0, Self>);
}

/// Owners whose exclusive unpublished suffix starts one staged writer.
pub trait SpareExt: Sized {
    /// Starts a staged writer over this owner's unpublished suffix.
    fn spare(&mut self) -> raw::Spare<'_>;

    /// Stages the initialized prefix of `N` bytes at `offset` for editing.
    fn try_prefix<const N: usize>(
        &mut self,
        offset: usize,
    ) -> Result<edit::Edit<'_, N>, edit::Error>;
}