o3 0.62.0

shared-nothing primitives
Documentation
use crate::buffer::{
    self,
    segments::{queue, scatter::Take},
};

mod scatter;

/// Immutable scattered bytes with an exact length and borrowed cursor.
///
/// # Safety
/// `chunks()` has `len()` remaining; offset cursors exist through the end.
pub unsafe trait Chunks {
    type Cursor<'data>: buffer::raw::Scatter<'data>
    where
        Self: 'data;

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn len(&self) -> usize;
    fn chunks(&self) -> Self::Cursor<'_>;
    fn cursor(&self, offset: usize) -> Option<Self::Cursor<'_>> {
        buffer::raw::Scatter::seek(self.chunks(), offset)
    }

    /// Copies an exact array-sized suffix without changing the retained source.
    fn suffix<const N: usize>(&self) -> Option<[u8; N]> {
        let mut cursor = self.chunks();
        let skipped = buffer::raw::Scatter::remaining(&cursor).checked_sub(N)?;
        Take::<_>::new(&mut cursor, skipped).finish();
        let mut output = [0; N];
        let source = Take::<_>::new(&mut cursor, usize::MAX);
        // SAFETY: admission and exact traversal leave N bytes in the retained
        // source, disjoint from this exclusive array of the same length.
        unsafe {
            buffer::raw::IteratorExt::copy_to(
                source.map(|chunk| chunk.bytes()),
                output.as_mut_ptr(),
            );
        }
        Some(output)
    }
}

// SAFETY: the ring owns each nonempty fragment through the complete queue borrow.
unsafe impl<T: buffer::raw::Segment> Chunks for queue::Fifo<T> {
    type Cursor<'data>
        = queue::Iter<'data, T>
    where
        Self: 'data;

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

    fn chunks(&self) -> queue::Iter<'_, T> {
        self.prefix(usize::MAX).fragments()
    }

    fn cursor(&self, offset: usize) -> Option<queue::Iter<'_, T>> {
        self.prefix(usize::MAX).chunks_from(offset)
    }
}

// SAFETY: the range retains its exact queue and both admitted byte offsets.
unsafe impl<T: buffer::raw::Segment> Chunks for queue::Slice<'_, T> {
    type Cursor<'data>
        = queue::Iter<'data, T>
    where
        Self: 'data;

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

    fn chunks(&self) -> queue::Iter<'_, T> {
        self.fragments()
    }

    fn cursor(&self, offset: usize) -> Option<queue::Iter<'_, T>> {
        self.chunks_from(offset)
    }
}