o3 0.62.0

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

use crate::{
    buffer::{self, storage},
    mem,
};

mod from;
mod init;
mod owners;
mod tag;

pub(in crate::buffer) use init::{Complete, Init};
pub use tag::Tag;

/// Opaque byte coordinates, usable only under an exact backing-owner proof.
/// Public raw contracts may borrow this representation; it exposes no safe views.
#[derive(Debug)]
#[repr(transparent)]
pub struct Extent(pub(super) ptr::NonNull<[u8]>);

impl Extent {
    pub(in crate::buffer) const EMPTY: Self = Self(ptr::NonNull::slice_from_raw_parts(
        ptr::NonNull::dangling(),
        0,
    ));

    /// # Safety
    /// The inclusive address range from `ptr` through `ptr + len` must not wrap.
    /// The allocation need not remain live; coordinates alone expose no bytes.
    pub(in crate::buffer) const unsafe fn new(ptr: ptr::NonNull<u8>, len: usize) -> Self {
        Self(ptr::NonNull::slice_from_raw_parts(ptr, len))
    }

    pub(in crate::buffer) fn from_bytes(bytes: &[u8]) -> Self {
        Self(ptr::NonNull::from(bytes))
    }

    pub(in crate::buffer) const fn len(&self) -> usize {
        self.0.len()
    }

    /// # Safety
    /// The owner retains this initialized immutable extent through every borrow and move.
    pub(in crate::buffer) const unsafe fn bind<O>(self, owner: O) -> storage::Shared<O> {
        storage::Shared { owner, bytes: self }
    }

    /// # Safety
    /// The range is ordered and contained in this exact nonwrapping extent.
    pub(in crate::buffer) unsafe fn restrict(&self, range: ops::Range<usize>) -> Self {
        let len = range.end - range.start;
        let pointer = self.0.cast::<u8>().as_ptr().wrapping_add(range.start);
        // SAFETY: the caller proves this subrange of the original nonwrapping extent.
        unsafe { Self::new(ptr::NonNull::new_unchecked(pointer), len) }
    }
}

const _: () = {
    assert!(size_of::<Extent>() == 2 * size_of::<usize>());
    assert!(usize::BITS >= 64);
};

#[repr(C, align(8))]
#[derive(Debug)]
pub(in crate::buffer) struct Prefix<T: Copy = u32> {
    refs: mem::raw::Count,
    word: cell::Cell<T>,
}

impl<T: Copy> Prefix<T> {
    pub(in crate::buffer) const fn empty(word: T) -> Self {
        Self {
            refs: mem::raw::Count::empty(),
            word: cell::Cell::new(word),
        }
    }

    pub(in crate::buffer) const fn one(word: T) -> Self {
        Self {
            refs: mem::raw::Count::one(),
            word: cell::Cell::new(word),
        }
    }

    pub(in crate::buffer) fn word(&self) -> T {
        self.word.get()
    }

    /// # Safety
    /// The caller must exclusively own this prefix's configuration state.
    pub(in crate::buffer) unsafe fn write(&self, word: T) {
        self.word.set(word);
    }
}

// SAFETY: each prefix keeps its nominal reference count at one immutable field address.
unsafe impl<T: Copy> mem::raw::Edge for Prefix<T> {
    fn count(&self) -> &mem::raw::Count {
        &self.refs
    }
}

pub(in crate::buffer) fn share<P: buffer::raw::Policy>(
    owner: storage::Owned<P>,
) -> Option<storage::Shared<crate::mem::raw::Owner<buffer::raw::Header<P>>>> {
    let heap = owner.allocation?;
    let len = NonZero::new(owner.len)?;
    // SAFETY: this heap retains the same owner's complete initialized prefix.
    Some(unsafe { buffer::raw::Header::into_range(heap.into_shared(), 0, len.get()) })
}

pub(in crate::buffer::storage) fn length<T: AsRef<[u8]>>(
    owner: &storage::NonEmpty<T>,
) -> NonZero<usize> {
    // SAFETY: only stable, nonempty sources can be admitted, and all exposed mutations preserve one byte.
    unsafe { NonZero::new_unchecked(owner.as_ref().len()) }
}

/// # Safety
/// The source retains a positive initialized prefix throughout this ownership.
pub(in crate::buffer) unsafe fn present<T: buffer::raw::Source>(owner: T) -> storage::NonEmpty<T> {
    storage::NonEmpty(owner)
}

// SAFETY: borrowing this proof retains exactly the source's original extent.
unsafe impl<T: buffer::raw::Source> buffer::raw::Source for storage::NonEmpty<T> {}

// SAFETY: the proof transfers the unchanged source with its original edge and extent.
unsafe impl<T> mem::raw::Convert<T> for storage::NonEmpty<T> {
    fn into(self) -> T {
        self.into_storage()
    }
}

/// # Safety
/// The amount is within this nonempty extent and leaves a nonempty suffix.
pub(in crate::buffer) unsafe fn consume<T: buffer::raw::Segment>(
    owner: &mut storage::NonEmpty<T>,
    amount: NonZero<usize>,
) {
    let extent = unsafe { owner.0.extent() };
    // SAFETY: the caller proved the strict prefix of this same extent.
    *extent = unsafe { extent.restrict(amount.get()..extent.len()) };
}

/// # Safety
/// The amount is a strict prefix; edge was retained from this exact backing.
pub(in crate::buffer) unsafe fn split<T: buffer::raw::Segment>(
    owner: &mut storage::NonEmpty<T>,
    amount: NonZero<usize>,
    edge: T::Backing,
) -> storage::NonEmpty<T> {
    let extent = unsafe { owner.0.extent() };
    // SAFETY: the two nonempty ranges partition the retained source.
    let prefix = unsafe { extent.restrict(0..amount.get()) };
    *extent = unsafe { extent.restrict(amount.get()..extent.len()) };
    storage::NonEmpty(unsafe { prefix.bind(edge) }.into())
}