o3 0.62.0

shared-nothing primitives
Documentation
use std::num::NonZero;

use crate::{
    base::Either,
    buffer::{self, segments::queue},
};

// SAFETY: the immutable queue and its admitted byte interval outlive every transition.
unsafe impl<'a, T: buffer::raw::Segment> buffer::raw::Scatter<'a> for queue::Iter<'a, T> {
    fn remaining(&self) -> usize {
        self.remaining()
    }

    unsafe fn advance<const MAX: usize>(
        &mut self,
        cap: NonZero<usize>,
    ) -> buffer::raw::Chunk<'a, MAX> {
        let view = unsafe { queue::raw::take_view(self, cap) }.into_storage();
        // SAFETY: this span is nonempty and occupies at most cap <= MAX.
        unsafe { buffer::raw::Chunk::new(view.as_slice()) }
    }
}

// SAFETY: the exclusive borrow delegates every observation and transition to
// the same exact cursor while preventing independent advancement of its state.
unsafe impl<'data, C: buffer::raw::Scatter<'data> + ?Sized> buffer::raw::Scatter<'data> for &mut C {
    fn remaining(&self) -> usize {
        C::remaining(self)
    }

    unsafe fn advance<const MAX: usize>(
        &mut self,
        cap: NonZero<usize>,
    ) -> buffer::raw::Chunk<'data, MAX> {
        // SAFETY: the borrowed cursor has the exact state specified by the caller.
        unsafe { C::advance(self, cap) }
    }
}

// SAFETY: the slice lifetime is the source lifetime, and advancing can only
// replace it with an exact suffix of the same allocation.
unsafe impl<'data> buffer::raw::Scatter<'data> for &'data [u8] {
    fn remaining(&self) -> usize {
        self.len()
    }

    unsafe fn advance<const MAX: usize>(
        &mut self,
        cap: NonZero<usize>,
    ) -> buffer::raw::Chunk<'data, MAX> {
        // SAFETY: the caller proves cap <= this slice's length.
        let (bytes, suffix) = unsafe { self.split_at_unchecked(cap.get()) };
        *self = suffix;
        // SAFETY: nonzero cap <= MAX proves a nonempty bounded prefix,
        // and bytes retains the source slice lifetime.
        unsafe { buffer::raw::Chunk::new(bytes) }
    }
}

// SAFETY: the active variant retains one complete cursor unchanged. Each
// operation forwards the same exact remaining-byte proof to that cursor.
unsafe impl<'a, L, R> buffer::raw::Scatter<'a> for Either<L, R>
where
    L: buffer::raw::Scatter<'a>,
    R: buffer::raw::Scatter<'a>,
{
    fn remaining(&self) -> usize {
        match self {
            Self::Left(cursor) => cursor.remaining(),
            Self::Right(cursor) => cursor.remaining(),
        }
    }

    unsafe fn advance<const MAX: usize>(
        &mut self,
        cap: NonZero<usize>,
    ) -> buffer::raw::Chunk<'a, MAX> {
        match self {
            // SAFETY: the active cursor retains the admitted remaining bytes.
            Self::Left(cursor) => unsafe { cursor.advance(cap) },
            // SAFETY: same proof as the left branch.
            Self::Right(cursor) => unsafe { cursor.advance(cap) },
        }
    }
}

const _: [(); 3 * size_of::<usize>()] = [(); size_of::<Either<&[u8], &[u8]>>()];