o3 0.62.0

shared-nothing primitives
Documentation
use std::{alloc::Layout, marker, ptr};

use crate::alloc::{self, Exhausted, Overflow};

#[repr(C)]
pub struct Bundle<S> {
    layout: Layout,
    fields: S,
}

impl Bundle<()> {
    /// Checks the complete field sequence declared by a shape without creating
    /// an instance that would already claim its allocation is representable.
    pub const fn try_layout<S: alloc::raw::Shape + ?Sized>(
        len: usize,
        tail_len: usize,
    ) -> Result<Layout, Overflow> {
        let layout = match alloc::raw::Layout::<S::Root>::try_layout(len) {
            Ok(layout) => layout,
            Err(error) => return Err(error),
        };
        let layout = match alloc::raw::Layout::<S::Tail>::try_extend(layout, tail_len) {
            Ok((layout, _)) => layout,
            Err(error) => return Err(error),
        };
        let layout = match alloc::raw::Layout::<S::Extra>::try_extend(layout, len) {
            Ok((layout, _)) => layout,
            Err(error) => return Err(error),
        };
        let layout = match alloc::raw::Layout::<S::Last>::try_extend(layout, len) {
            Ok((layout, _)) => layout,
            Err(error) => return Err(error),
        };
        Ok(layout.pad_to_align())
    }

    pub(crate) fn try_root<T>(len: usize) -> Result<Bundle<alloc::raw::Layout<T>>, Overflow> {
        Ok(Bundle {
            layout: alloc::raw::Layout::<T>::try_layout(len)?,
            fields: alloc::raw::Layout(marker::PhantomData),
        })
    }

    /// # Safety
    /// The caller carries a nominal proof that this exact array layout fits.
    pub(crate) const unsafe fn root<T>(len: usize) -> Bundle<alloc::raw::Layout<T>> {
        Bundle {
            layout: unsafe { alloc::raw::Layout::<T>::layout(len) },
            fields: alloc::raw::Layout(marker::PhantomData),
        }
    }
}

impl<S> Bundle<S> {
    pub(crate) const fn layout(&self) -> Layout {
        self.layout.pad_to_align()
    }

    /// Allocates these exact fields and retains their layout for raw projection.
    pub fn try_allocate(&self) -> Result<alloc::raw::Fields<'_, S>, Exhausted> {
        Ok(alloc::raw::Fields {
            fields: &self.fields,
            pointer: alloc::raw::LayoutExt::try_allocate(self.layout())?,
        })
    }

    /// # Safety
    /// `pointer` identifies a live allocation made with this exact bundle.
    /// No owner or field projection may access the allocation after this call.
    pub unsafe fn deallocate(&self, pointer: ptr::NonNull<u8>) {
        unsafe { alloc::raw::LayoutExt::deallocate(self.layout(), pointer) }
    }

    pub fn try_tail<T>(self, len: usize) -> Result<Bundle<alloc::raw::Tail<S, T>>, Overflow> {
        let tail = alloc::raw::Layout::<T>::try_extend(self.layout, len)?;
        Ok(self.extend(tail))
    }

    /// # Safety
    /// The caller carries a nominal proof that this exact extension fits.
    pub unsafe fn tail<T>(self, len: usize) -> Bundle<alloc::raw::Tail<S, T>> {
        // SAFETY: the nominal proof covers this exact array extension.
        let tail = unsafe { alloc::raw::Layout::<T>::extend(self.layout, len) };
        self.extend(tail)
    }

    fn extend<T>(self, (layout, offset): (Layout, usize)) -> Bundle<alloc::raw::Tail<S, T>> {
        Bundle {
            layout,
            fields: alloc::raw::Tail {
                prior: self.fields,
                offset,
                value: marker::PhantomData,
            },
        }
    }
}