o3 0.59.0

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

use crate::{
    buffer,
    buffer::{raw::Policy as _, resident, storage},
    mem::rc,
};

pub(in crate::buffer) type Rc<P = ()> = crate::mem::raw::Owner<crate::buffer::raw::Header<P>>;

impl<P: crate::buffer::raw::Policy> Rc<P> {
    pub(in crate::buffer) fn capacity(&self) -> num::NonZero<u32> {
        self.get().capacity()
    }

    pub(in crate::buffer) fn seed(&self) -> P {
        self.get().policy
    }

    pub(in crate::buffer) fn data(&self) -> *const u8 {
        crate::buffer::raw::Header::data(self.as_ptr()).as_ptr()
    }

    pub(in crate::buffer) fn is_unique(&self) -> bool {
        use crate::mem::raw::Edge as _;

        self.get().prefix.is_unique()
    }

    pub(in crate::buffer) fn unique(&mut self) -> Option<&mut super::Heap<P>> {
        if !self.is_unique() {
            return None;
        }
        // SAFETY: both transparent owners store the same NonNull<Header<P>>.
        // The sole counted edge is a valid Heap. The exclusive loan keeps the
        // edge inaccessible and every Heap replacement leaves one live edge.
        Some(unsafe { &mut *ptr::from_mut(self).cast::<super::Heap<P>>() })
    }
}

impl Rc<resident::Budget<'_>> {
    pub(in crate::buffer) fn try_own(self) -> Result<Rc, Self> {
        use crate::buffer::{raw::Header, resident::Budget};

        const EXTRA: u32 = {
            assert!(size_of::<Header<Budget<'_>>>() >= size_of::<Header<()>>());
            let extra = size_of::<Header<Budget<'_>>>() - size_of::<Header<()>>();
            assert!(extra <= u32::MAX as usize);
            extra as u32
        };
        let original = self.capacity();
        let Some(capacity) = original.checked_add(EXTRA) else {
            return Err(self);
        };
        if !self.is_unique() || align_of::<Header<Budget<'_>>>() != align_of::<Header<()>>() {
            return Err(self);
        }
        let pointer = self.into_raw();
        // SAFETY: the sole edge owns this policy exclusively. Refunding it ends
        // the resident-domain borrow before the same allocation becomes plain.
        unsafe { pointer.as_ref().policy.refund(original) };
        let mut pointer = pointer.cast::<Header<()>>();
        // SAFETY: admission proved identical allocation size and alignment. The
        // initialized common prefix retains its unique counted edge. Former policy
        // bytes become spare capacity; the initialized byte extent stays unchanged.
        unsafe {
            pointer.as_mut().set_capacity(capacity);
            Ok(Rc::from_raw(pointer))
        }
    }
}

// SAFETY: retaining Rc preserves its allocation and the policy's owner lifetime.
unsafe impl<P: crate::buffer::raw::Policy> buffer::raw::Retain for Rc<P> {
    fn try_retain(&self) -> Result<Self, rc::Error> {
        self.try_clone()
    }
}

const _: () = assert!(size_of::<Rc<()>>() == size_of::<ptr::NonNull<()>>());
const _: () = assert!(size_of::<Option<Rc<()>>>() == size_of::<ptr::NonNull<()>>());

impl<P: buffer::raw::Policy> Rc<P> {
    /// Transfers this edge into an initialized allocation range.
    /// # Safety
    /// `start..start + len` must be initialized and within this allocation.
    pub(in crate::buffer) unsafe fn into_range(
        self,
        start: u32,
        len: u32,
    ) -> storage::Shared<Self> {
        let pointer = crate::buffer::raw::Header::data(self.as_ptr());
        // SAFETY: the owned data pointer is nonnull and the offset stays inside its allocation.
        let pointer = unsafe { pointer.add(start as usize) };
        unsafe { storage::Shared::from_raw(pointer, len as usize, self) }
    }
}