dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use std::num::NonZero;

use o3::{base::num::bounded, buffer};

use crate::io::transfer::{self, Len, progress};

type Count = bounded::raw::U32<0, { transfer::MAX_BYTES as u32 }>;

pub(super) const fn widen(bytes: transfer::NonZero) -> Count {
    // SAFETY: every constructor retains the transfer or framed payload bound.
    unsafe { Count::from_proven(bytes.into_nonzero().get()) }
}

pub(super) const fn consumed(state: &progress::State) -> Count {
    let prefix = state.limit().into_raw() - state.left().into_raw();
    // SAFETY: the state's constructors and advances retain left <= limit <= MAX_BYTES.
    unsafe { Count::from_proven(prefix as u32) }
}

pub(super) fn scatter<'a>(
    cursor: &mut impl buffer::raw::Scatter<'a>,
    limit: Len,
) -> Option<buffer::raw::Chunk<'a, { transfer::MAX_BYTES }>> {
    let cap = NonZero::new(cursor.remaining().min(limit.into_raw()))?;
    // SAFETY: the transfer limit already fits MAX_BYTES, and cap fits the exact cursor.
    Some(unsafe { cursor.advance(cap) })
}