o3 0.60.0

shared-nothing primitives
Documentation
use std::ops;

use crate::buffer::{
    self, Read as _, hold, resident, storage,
    storage::range,
    write::consume,
};

#[must_use]
pub struct Deferred<'input, 'd, R: buffer::Source + 'input, P = ()> {
    view: R::View<'input>,
    budget: &'input resident::Budget<'d>,
    choice: P,
}

/// One deferred range with its retention policy and resident ceiling locked.
pub type Bound<'input, 'd, R> = Deferred<'input, 'd, R, hold::Choice>;

impl<'input, 'd, R: buffer::Source + 'input, P> Deferred<'input, 'd, R, P> {
    pub fn as_slice(&self) -> &'input [u8] {
        self.view.as_slice()
    }

    pub const fn budget(&self) -> &'input resident::Budget<'d> {
        self.budget
    }

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

    pub fn is_empty(&self) -> bool {
        self.as_slice().is_empty()
    }
}

impl<'input, 'd, R: buffer::Source + 'input> Deferred<'input, 'd, R> {
    pub(in crate::buffer) fn new(view: R::View<'input>, budget: &'input resident::Budget<'d>) -> Self {
        Self {
            view,
            budget,
            choice: (),
        }
    }

    pub fn try_split<const N: usize>(self) -> Result<(&'input [u8; N], Self), consume::Error> {
        let (head, tail) = self.view.try_split()?;
        Ok((head, Self::new(tail, self.budget)))
    }

    pub fn bind(self, policy: hold::Policy) -> Bound<'input, 'd, R>
    where
        R: hold::raw::Retain<'d>,
    {
        let choice = policy.choose(self.len(), || R::resident(&self.view));
        Bound {
            view: self.view,
            budget: self.budget,
            choice,
        }
    }
}

impl<'input, 'd, R: buffer::Cursor + 'input> Deferred<'input, 'd, R> {
    pub fn try_plan(
        source: &'input R,
        budget: &'input resident::Budget<'d>,
        range: ops::Range<usize>,
    ) -> Result<Self, range::Error> {
        Ok(Self::new(source.try_get(range)?, budget))
    }
}

impl<'input, 'd, R: buffer::Complete + 'input> Deferred<'input, 'd, R> {
    pub fn all(source: &'input R, budget: &'input resident::Budget<'d>) -> Self {
        Self::new(source.view(), budget)
    }
}

impl<'input, 'd, R: buffer::Source + 'input> Bound<'input, 'd, R> {
    pub fn defer(self) -> Deferred<'input, 'd, R> {
        Deferred::new(self.view, self.budget)
    }
}

impl<'input, 'd, R: hold::raw::Retain<'d> + 'input> Bound<'input, 'd, R> {
    /// Maximum resident bytes produced when this exact bound is retained.
    pub fn ceiling(&self) -> usize {
        match self.choice {
            hold::Choice::Empty => 0,
            hold::Choice::Copy => self.len(),
            hold::Choice::Share => self.len().max(R::resident(&self.view)),
        }
    }

    #[inline(always)]
    pub fn try_retain(&self) -> Result<hold::Retained<'d>, hold::Error> {
        match self.choice {
            hold::Choice::Empty => Ok(resident::Shared::new().into()),
            hold::Choice::Copy => R::try_copy(&self.view, self.budget).map(hold::Retained::from),
            hold::Choice::Share => R::try_retain(&self.view, self.budget),
        }
    }

    /// Attempts optional Share retention without copying payload bytes.
    /// A decline or error preserves this bound for the caller's fallback.
    pub fn try_retain_with(&self, limit: usize) -> Result<Option<hold::Retained<'d>>, hold::Error> {
        if self.choice != hold::Choice::Share {
            return Ok(None);
        }
        R::try_retain_with(&self.view, self.budget, limit)
    }

    /// Retains this range and ends the bound if resident admission fails.
    pub fn try_into(self) -> Result<resident::Shared<'d>, hold::Error> {
        self.try_retain().map(hold::Retained::into_shared)
    }
}

const _: () = {
    let word = size_of::<usize>();
    assert!(size_of::<Deferred<'static, 'static, storage::Shared>>() == 4 * word);
    assert!(size_of::<Bound<'static, 'static, storage::Shared>>() == 5 * word);
    assert!(size_of::<Option<Bound<'static, 'static, storage::Shared>>>() == 5 * word);
    assert!(size_of::<Result<resident::Shared<'static>, hold::Error>>() == 5 * word);
    assert!(size_of::<Result<hold::Retained<'static>, hold::Error>>() == 5 * word);
};