dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use dope_sys::io::socket::{
    self,
    msg::{Header, Iovec},
};
use o3::buffer::bytes::bounded;
use std::mem;

use crate::io::{
    socket::msg::{MAX_IOVECS, Message},
    transfer,
};

/// A send message retaining at most `N` iovecs and their exact live byte sum.
pub struct Vectored<'a, const N: usize = MAX_IOVECS> {
    header: &'a mut mem::MaybeUninit<Header>,
    pub(in crate::io::socket::msg) iovs: &'a [Iovec],
    bytes: transfer::Len,
}

impl<'a, const N: usize> Vectored<'a, N> {
    #[must_use]
    pub fn into_message(self) -> Message<'a> {
        let header = self.header.write(Header::new());
        header.bind(self.iovs);
        Message { header }
    }

    #[must_use]
    pub const fn bytes(&self) -> transfer::Len {
        self.bytes
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.bytes == transfer::Len::ZERO
    }
}

impl<'a, const N: usize> Vectored<'a, N> {
    /// Projects a sole descriptor with its admitted byte bound.
    pub fn contiguous(&self) -> Option<transfer::View<'a>> {
        let [iovec] = self.iovs else {
            return None;
        };
        // SAFETY: the graph retains the sole descriptor's admitted bytes for 'a.
        let bytes = unsafe { bounded::raw::Input::new(socket::raw::Iovec::as_slice(iovec)) };
        Some(transfer::View::from_prefix(bytes.into_prefix()))
    }

    pub(in crate::io::socket::msg) fn from_parts(
        header: &'a mut mem::MaybeUninit<Header>,
        iovs: &'a [Iovec],
        bytes: transfer::Len,
    ) -> Self {
        Self {
            header,
            iovs,
            bytes,
        }
    }
}