o3 0.61.0

shared-nothing primitives
Documentation
use pool::{borrowed, state};

use crate::buffer::{self, pool, pool::owners};

/// An exclusive initialized range retaining its original pool and slot.
pub struct Slice<'pool> {
    pub(in crate::buffer::pool::borrowed) span:
        pool::Span<owners::Plain, pool::Borrowed<'pool>, state::Initialized>,
    pub(in crate::buffer::pool::borrowed) data: *mut u8,
}

impl buffer::Residency for Slice<'_> {
    fn resident(&self) -> usize {
        self.span.capacity().get() as usize
    }
}

impl<'pool> Slice<'pool> {
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn len(&self) -> usize {
        self.span.len as usize
    }

    pub fn as_slice(&self) -> &[u8] {
        // SAFETY: this exclusive initialized span retains this exact visible range.
        unsafe { buffer::raw::range(self.data, 0, self.len()) }
    }

    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        // SAFETY: this original slot has no conflicting writable capability.
        unsafe { buffer::raw::range_mut(self.data, 0, self.len()) }
    }

    pub fn try_frame(
        &mut self,
        prefix: &[u8],
        suffix: usize,
    ) -> Result<borrowed::Frame<'_, 'pool>, borrowed::Error> {
        let start = (self.data.addr() - self.span.data().addr()) as u32;
        if prefix.len() > start as usize {
            return Err(borrowed::Error::Prefix {
                requested: prefix.len(),
                available: start,
            });
        }
        let spare = self.span.capacity().get() - start - self.span.len;
        if suffix > spare as usize {
            return Err(borrowed::Error::Suffix {
                requested: suffix,
                available: spare,
            });
        }
        // SAFETY: both admissions retain the exact prefix and tail in this slot.
        unsafe {
            buffer::raw::SliceExt::copy_to(prefix, self.data.sub(prefix.len()));
        }
        Ok(borrowed::Frame {
            payload: self,
            prefix: prefix.len() as u32,
            suffix: suffix as u32,
        })
    }
}

const _: () = {
    assert!(size_of::<Slice<'static>>() == 3 * size_of::<usize>());
    assert!(size_of::<Option<Slice<'static>>>() == size_of::<Slice<'static>>());
};