o3 0.62.0

shared-nothing primitives
Documentation
use std::ops;

use crate::{
    base::num::bounded,
    buffer::{
        self,
        bytes::{self, bounded::Value},
    },
};

/// An unchecked value awaiting its caller-owned extent proof.
#[repr(transparent)]
pub struct Input<T, const MAX: usize = { super::MAX }>(T);

impl<T, const MAX: usize> Input<T, MAX> {
    pub(in crate::buffer) fn into_inner(self) -> T {
        self.0
    }

    /// # Safety
    /// `value` must expose at most `MAX` bytes.
    /// Shared access must preserve the admitted extent.
    pub const unsafe fn new(value: T) -> Self {
        Self(value)
    }
}

impl<'a, const MAX: usize> Input<&'a [u8], MAX> {
    /// Transfers the unchanged source and its admitted extent into a borrowed prefix.
    pub const fn into_prefix(self) -> bytes::Prefix<'a, MAX> {
        bytes::Prefix(self.0)
    }
}

impl<S: ?Sized> Input<buffer::View<'_, S>> {
    /// # Safety
    /// `range` must be contained in this view's unchanged initialized extent.
    pub unsafe fn select(mut self, range: ops::Range<usize>) -> Self {
        // SAFETY: the caller proves this subrange of the retained source borrow.
        self.0.proof = unsafe { self.0.proof.get_unchecked(range) };
        self
    }
}

impl<T: AsRef<[u8]>> Input<T> {
    pub(in crate::buffer) fn count(
        value: &Value<T>,
    ) -> bounded::raw::U32<0, { super::MAX as u32 }> {
        let len = value.as_ref().len();
        // SAFETY: admission proves this stable extent fits the signed byte count
        // domain, so conversion preserves the exact u32 value.
        unsafe { bounded::raw::U32::from_proven(len as u32) }
    }
}