o3 0.62.0

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

use crate::{
    alloc,
    buffer::{
        self, pool, resident, segments, storage,
        write::{self, Append, Sink as _, consume},
    },
    mem::{Rejected, rc},
};

/// One byte owner whose exposed mutations preserve a nonempty initialized extent.
#[derive(Debug)]
#[repr(transparent)]
pub struct NonEmpty<T>(pub(in crate::buffer::storage) T);

impl<T: buffer::raw::Source> NonEmpty<T> {
    pub fn try_new(value: T) -> Result<Self, Rejected<T, storage::Zero>> {
        if value.as_ref().is_empty() {
            return Err(Rejected::new(value, storage::Zero));
        }
        Ok(Self(value))
    }
}

impl<T> NonEmpty<T> {
    pub const fn as_inner(&self) -> &T {
        &self.0
    }

    pub fn into_storage(self) -> T {
        self.0
    }
}

impl NonEmpty<pool::Span> {
    pub fn try_append<const N: usize>(
        mut value: pool::Span,
        source: &[u8; N],
    ) -> Result<Self, consume::Error> {
        const { assert!(N != 0) };
        value.try_append(source)?.commit();
        Ok(Self(value))
    }
}

impl<'a> From<&'a mut NonEmpty<pool::Span>> for write::raw::Spare<'a> {
    fn from(value: &'a mut NonEmpty<pool::Span>) -> Self {
        write::SpareExt::spare(&mut value.0)
    }
}

impl ops::DerefMut for NonEmpty<pool::Span> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0.as_mut_slice()
    }
}

impl NonEmpty<Vec<u8>> {
    /// Appending the first byte establishes the proof on this exact allocation.
    pub fn try_push(mut value: Vec<u8>, byte: u8) -> Result<Self, Rejected<Vec<u8>, alloc::Error>> {
        if let Err(error) = value.write_byte(byte) {
            return Err(Rejected::new(value, error));
        }
        Ok(Self(value))
    }

    pub fn try_resize(
        mut value: Vec<u8>,
        len: NonZero<usize>,
        byte: u8,
    ) -> Result<Self, Rejected<Vec<u8>, alloc::Error>> {
        let added = len.get().saturating_sub(value.len());
        if let Err(error) = Append::new(&mut value).try_fill(added, byte) {
            return Err(Rejected::new(value, error));
        }
        value.truncate(len.get());
        Ok(Self(value))
    }

    pub fn truncate(&mut self, len: NonZero<usize>) {
        self.0.truncate(len.get());
    }
}

impl<'a> From<&'a mut NonEmpty<Vec<u8>>> for Append<'a> {
    fn from(value: &'a mut NonEmpty<Vec<u8>>) -> Self {
        Append::new(&mut value.0)
    }
}

impl<T: AsRef<[u8]>> AsRef<[u8]> for NonEmpty<T> {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl<T: buffer::Residency> buffer::Residency for NonEmpty<T> {
    fn resident(&self) -> usize {
        self.0.resident()
    }
}

impl<T: AsRef<[u8]>> ops::Deref for NonEmpty<T> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.0.as_ref()
    }
}

impl ops::DerefMut for NonEmpty<Vec<u8>> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0.as_mut_slice()
    }
}

impl<T: AsRef<[u8]>> NonEmpty<T> {
    pub fn len(&self) -> NonZero<usize> {
        storage::raw::length(self)
    }
}

impl<'d> From<NonEmpty<&'d [u8]>> for NonEmpty<storage::Shared<resident::State<'d>>> {
    fn from(source: NonEmpty<&'d [u8]>) -> Self {
        Self(storage::Shared::<resident::State<'_>>::from(source.0))
    }
}

impl From<storage::raw::Init> for NonEmpty<storage::Shared> {
    fn from(init: storage::raw::Init) -> Self {
        Self(init.into_buf().into())
    }
}

impl TryFrom<NonEmpty<&[u8]>> for NonEmpty<storage::Shared> {
    type Error = storage::AllocError;

    fn try_from(source: NonEmpty<&[u8]>) -> Result<Self, Self::Error> {
        let init = storage::raw::Init::try_from(source)?;
        Ok(Self::from(init))
    }
}

impl TryFrom<NonEmpty<Vec<u8>>> for NonEmpty<storage::Shared> {
    type Error = alloc::Exhausted;

    fn try_from(mut source: NonEmpty<Vec<u8>>) -> Result<Self, Self::Error> {
        storage::raw::Tag::try_vec(&mut source.0).map(Self)
    }
}

impl<'d, const N: usize> From<&'d [u8; N]> for NonEmpty<storage::Shared<resident::State<'d>>> {
    fn from(bytes: &'d [u8; N]) -> Self {
        const { assert!(N != 0) };
        Self(storage::Shared::<resident::State<'_>>::from(
            bytes.as_slice(),
        ))
    }
}

impl<T: buffer::raw::Segment> NonEmpty<T> {
    pub fn try_clone(&self) -> Result<Self, rc::Error> {
        NonEmpty(buffer::View::new(&self.0)).try_retain()
    }

    pub(in crate::buffer) fn tail(&mut self, amount: usize) -> Option<segments::Tail<'_, T>> {
        segments::Tail::new(&mut self.0, amount)
    }
}

impl<'d> From<NonEmpty<storage::Shared>> for NonEmpty<storage::Shared<resident::State<'d>>> {
    fn from(owner: NonEmpty<storage::Shared>) -> Self {
        Self(storage::Shared::<resident::State<'_>>::from(owner.0))
    }
}

impl<'d> From<NonEmpty<&mut storage::Shared<resident::State<'d>>>>
    for NonEmpty<storage::Shared<resident::State<'d>>>
{
    fn from(source: NonEmpty<&mut storage::Shared<resident::State<'d>>>) -> Self {
        Self(mem::take(source.0))
    }
}

impl<'a, T: buffer::raw::Segment> NonEmpty<buffer::View<'a, T>> {
    pub(in crate::buffer) fn try_retain(&self) -> Result<NonEmpty<T>, rc::Error> {
        T::try_share(self).map(NonEmpty)
    }
}

const _: () = {
    assert!(size_of::<NonEmpty<Vec<u8>>>() == size_of::<Vec<u8>>());
    assert!(size_of::<Option<NonEmpty<Vec<u8>>>>() == size_of::<Option<Vec<u8>>>());
    assert!(
        size_of::<NonEmpty<storage::Shared<resident::State<'static>>>>()
            == size_of::<storage::Shared<resident::State<'static>>>()
    );
    assert!(
        align_of::<NonEmpty<storage::Shared<resident::State<'static>>>>()
            == align_of::<storage::Shared<resident::State<'static>>>()
    );
    assert!(
        size_of::<Option<NonEmpty<storage::Shared<resident::State<'static>>>>>()
            == size_of::<Option<storage::Shared<resident::State<'static>>>>()
    );
    assert!(size_of::<NonEmpty<storage::Shared>>() == size_of::<storage::Shared>());
    assert!(align_of::<NonEmpty<storage::Shared>>() == align_of::<storage::Shared>());
    assert!(size_of::<Option<NonEmpty<storage::Shared>>>() == size_of::<Option<storage::Shared>>());
    assert!(
        size_of::<NonEmpty<buffer::View<'static, storage::Shared>>>()
            == size_of::<buffer::View<'static, storage::Shared>>()
    );
};