dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use o3::buffer;

use crate::io::transfer::{self, sealed};

/// Exact amount rejected by a bounded progress transition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Exceeded {
    requested: transfer::Len,
    available: transfer::Len,
}

impl Exceeded {
    const fn new(req: transfer::Len, avail: transfer::Len) -> Self {
        Self {
            requested: req,
            available: avail,
        }
    }

    pub const fn requested(self) -> transfer::Len {
        self.requested
    }

    pub const fn available(self) -> transfer::Len {
        self.available
    }
}

/// Monotonic committed prefix of one bounded transfer.
pub struct State {
    limit: transfer::Len,
    remaining: transfer::Len,
}

impl State {
    #[must_use]
    pub const fn new(limit: transfer::Len) -> Self {
        Self {
            limit,
            remaining: limit,
        }
    }

    /// Counts the nonempty prefix before `suffix` as consumed, when one exists.
    #[must_use]
    pub const fn from_tail(limit: transfer::Len, suffix: transfer::Len) -> Option<Self> {
        if limit.is_greater(suffix) {
            Some(Self {
                limit,
                remaining: suffix,
            })
        } else {
            None
        }
    }

    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.remaining.is_zero()
    }

    /// Returns the uncommitted suffix without discarding its backend bound.
    #[must_use]
    pub const fn left(&self) -> transfer::Len {
        self.remaining
    }

    pub(super) const fn limit(&self) -> transfer::Len {
        self.limit
    }

    #[must_use]
    pub const fn len(&self) -> transfer::Len {
        transfer::Len::from_bound(sealed::consumed(self))
    }

    /// Advances an exact scatter cursor within this transfer's remaining budget.
    pub fn scatter<'a>(
        &mut self,
        cursor: &mut impl buffer::raw::Scatter<'a>,
    ) -> Option<buffer::raw::Chunk<'a, { transfer::MAX_BYTES }>> {
        let chunk = sealed::scatter(cursor, self.remaining)?;
        let rest = Self {
            limit: self.remaining,
            remaining: transfer::Len::from_count(chunk.len().into_count()),
        };
        self.remaining = rest.len();
        Some(chunk)
    }

    /// Prepares an exact extended state when the whole amount fits.
    pub fn try_extend(&self, amount: transfer::Len) -> Result<Self, Exceeded> {
        if amount.is_greater(self.remaining) {
            return Err(Exceeded::new(amount, self.remaining));
        }
        Ok(Self {
            limit: self.limit,
            remaining: self.remaining.abs_diff(amount),
        })
    }
}

const _: () = assert!(size_of::<State>() == 2 * size_of::<u32>());