o3 0.60.0

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

use crate::{buffer, buffer::bytes::window::Capacity};

pub(in crate::buffer) struct State<P: crate::buffer::raw::Policy, const MAX_CAPACITY: usize> {
    pub(in crate::buffer) buf: crate::buffer::raw::Rc<P>,
    pub(in crate::buffer::bytes::window) cap: Capacity<MAX_CAPACITY>,
    pub(in crate::buffer::bytes::window) head: u32,
}

impl<P: crate::buffer::raw::Policy, const MAX_CAPACITY: usize> State<P, MAX_CAPACITY> {
    pub(in crate::buffer) fn seed(&self) -> P {
        self.buf.seed()
    }

    pub(in crate::buffer) fn len(&self) -> u32 {
        self.head
    }

    pub(in crate::buffer) fn capacity(&self) -> num::NonZero<u32> {
        self.cap.nonzero()
    }

    pub(in crate::buffer) fn as_slice(&self) -> &[u8] {
        buffer::raw::Prefix::as_slice(self)
    }

    pub(in crate::buffer) fn spare(&mut self) -> buffer::write::Spare<'_> {
        let pointer = self.buf.data().cast_mut().cast::<mem::MaybeUninit<u8>>();
        let start = self.head as usize;
        let len = (self.cap.get() - self.head) as usize;
        // SAFETY: head excludes every published range and this loan owns the unpublished suffix.
        let spare = unsafe { slice::from_raw_parts_mut(pointer.add(start), len) };
        // SAFETY: the same allocation owns the suffix and its unchanged initialization word.
        unsafe { buffer::write::Spare::from_parts(spare, &mut self.head) }
    }
}

// SAFETY: State retains the allocation and its exact initialized prefix;
// shared snapshots cannot mutate that prefix while this state is borrowed.
unsafe impl<P: crate::buffer::raw::Policy, const MAX: usize> buffer::raw::Prefix for State<P, MAX> {
    fn prefix(&self) -> (*mut mem::MaybeUninit<u8>, u32) {
        (self.buf.data().cast_mut().cast(), self.head)
    }
}

const _: () = assert!(size_of::<State<(), 1>>() == 2 * size_of::<usize>());