o3 0.60.0

shared-nothing primitives
Documentation
use std::{num::NonZero, ops};

pub use error::Error;

use crate::buffer::{self, hold, resident};

mod error;

/// One source and the proof admitted against that source.
#[must_use]
#[repr(C)]
#[derive(Debug)]
pub struct Input<S, P = ()> {
    pub(in crate::buffer) source: S,
    pub(in crate::buffer) proof: P,
}

#[derive(Debug)]
pub struct Prefix<const MAX: u32> {
    start: u32,
    end: NonZero<u32>,
}

impl<const MAX: u32> Prefix<MAX> {
    pub const fn count(&self) -> NonZero<u32> {
        self.end
    }
}

impl<'a, R> From<&'a mut R> for Input<&'a mut R> {
    fn from(source: &'a mut R) -> Self {
        Self { source, proof: () }
    }
}

impl<R: buffer::Cursor, P> Input<&mut R, P> {
    pub fn as_slice(&self) -> &[u8] {
        self.source.as_ref()
    }

    pub fn try_plan<'s, 'd>(
        &'s self,
        budget: &'s resident::Budget<'d>,
        range: ops::Range<usize>,
    ) -> Result<hold::scope::Deferred<'s, 'd, R>, buffer::storage::range::Error> {
        hold::scope::Deferred::try_plan(&*self.source, budget, range)
    }
}

impl<'a, R: buffer::Cursor + buffer::raw::Source> Input<&'a mut R> {
    pub fn try_prefix<const MAX: u32>(
        self,
        start: usize,
        count: NonZero<usize>,
    ) -> Result<Input<&'a mut R, Prefix<MAX>>, Error> {
        if count.get() > MAX as usize {
            return Err(Error::Maximum { count, maximum: MAX });
        }
        let bounds = start..count.get();
        let len = self.source.as_ref().len();
        if !buffer::is_within(&bounds, len) {
            return Err(Error::Range(buffer::storage::range::Error::new(bounds, len)));
        }
        Ok(Input {
            source: self.source,
            proof: Prefix {
                start: start as u32,
                end: NonZero::<u32>::MIN.saturating_add(count.get() as u32 - 1),
            },
        })
    }
}

impl<R: buffer::Cursor + buffer::raw::Source, const MAX: u32> Input<&mut R, Prefix<MAX>> {
    pub fn left(&self) -> usize {
        self.source.as_ref().len() - self.proof.end.get() as usize
    }

    pub fn view(&self) -> R::View<'_> {
        let bounds = ops::Range {
            start: self.proof.start as usize,
            end: self.proof.end.get() as usize,
        };
        // SAFETY: the input retains the original stable source and its admitted range.
        R::select(unsafe { buffer::raw::view(&*self.source, bounds) })
    }

    pub fn defer<'s, 'd>(
        &'s self,
        budget: &'s resident::Budget<'d>,
    ) -> hold::scope::Deferred<'s, 'd, R> {
        hold::scope::Deferred::new(self.view(), budget)
    }
}

impl<R: buffer::raw::Consume, const MAX: u32> Input<&mut R, Prefix<MAX>> {
    pub fn commit(self) {
        R::consume(self);
    }
}

const _: () = {
    assert!(size_of::<Input<&'static mut buffer::storage::Shared>>() == size_of::<usize>());
    assert!(size_of::<Prefix<{ u32::MAX }>>() == 8);
    assert!(size_of::<Option<Prefix<{ u32::MAX }>>>() == 8);
};