o3 0.62.0

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

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

// SAFETY: shared access cannot change the unique allocation or initialized range.
unsafe impl<P: buffer::raw::Policy> buffer::raw::Source for storage::Owned<P> {}

// SAFETY: shared access through this exclusive loan preserves the original backing and prefix.
unsafe impl<P: buffer::raw::Policy> buffer::raw::Source for &mut storage::Owned<P> {}

// SAFETY: Owned owns its heap and tracks only its initialized prefix; an
// absent heap has length zero. Shared access cannot mutate either field.
unsafe impl<P: buffer::raw::Policy> buffer::raw::Prefix for storage::Owned<P> {
    fn prefix(&self) -> (*mut MaybeUninit<u8>, u32) {
        let pointer = self
            .allocation
            .as_ref()
            .map_or(ptr::NonNull::dangling().as_ptr(), |heap| {
                heap.as_ptr().cast_mut()
            });
        (pointer, self.len)
    }
}

// SAFETY: the heap is uniquely owned, disjoint from its length word, and
// every construction and write keeps that word within its u32 capacity.
unsafe impl<P: buffer::raw::Policy> buffer::raw::Exclusive for storage::Owned<P> {
    fn as_mut_ptr(&mut self) -> *mut MaybeUninit<u8> {
        buffer::raw::Prefix::prefix(self).0
    }

    fn len_mut(&mut self) -> &mut u32 {
        &mut self.len
    }

    fn capacity(&self) -> u32 {
        storage::Owned::capacity(self) as u32
    }
}

// SAFETY: the exact owner exposes its complete initialized allocation immutably.
unsafe impl buffer::raw::Source for storage::Exact {}