dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use crate::{
    Backend, driver,
    driver::{
        flight,
        life::native::{host::Host, request},
        result, route,
        route::kind,
    },
    io::{
        fd::handles,
        fs, socket,
        socket::{msg, option, pending},
        transfer,
    },
};
use dope_sys::{
    engine::admit::{self, raw::Retain},
    io::{reactor::Full, request::Source, socket::addr},
};

type Native<'a> = <crate::Engine as admit::Admit<Host<Backend>>>::Admission<'a>;

/// Native capacity retained while its owner initializes the final message slot.
#[repr(transparent)]
pub struct Admission<'a, 'd> {
    native: Native<'a>,
    driver: driver::Invariant<'d>,
}

impl<'a, 'd> Admission<'a, 'd> {
    pub fn try_new(context: &'a mut driver::Local<'d>) -> Result<Self, Full> {
        const {
            assert!(std::mem::size_of::<Self>() == std::mem::size_of::<Native<'a>>());
            assert!(std::mem::align_of::<Self>() == std::mem::align_of::<Native<'a>>());
        }
        let local = context;
        let driver = local.driver.brand;
        let native = crate::driver::life::native::Access::new(local).try_admit()?;
        Ok(Self { native, driver })
    }

    /// # Safety
    /// The descriptor, echoed slot and message graph belong to this driver and
    /// remain retained through terminal completion or quiescence.
    pub unsafe fn send_msg<'r>(
        self,
        fd: &'r handles::Descriptor<'d>,
        message: msg::Message<'r>,
        key: flight::raw::Echo,
    ) where
        'd: 'r,
    {
        // SAFETY: the caller retains the admitted slot and immutable graph.
        unsafe {
            Retain::<Host<Backend>>::send_msg(
                self.native,
                <Backend as Source<request::Context>>::target(fd.slot()),
                message,
                key,
            );
        }
    }
}

impl<'d, Tag: route::Tag> flight::Slots<'d, Tag> {
    /// # Safety
    /// Descriptor, target and slots belong to this driver. Keep the descriptor alive and the
    /// bytes fixed and immutable through terminal completion or quiescence.
    pub unsafe fn try_send<'r>(
        &self,
        context: &mut driver::Local<'d>,
        fd: &'r handles::Descriptor<'d>,
        buffer: transfer::View<'r>,
        target: route::Target<'d, Tag>,
    ) -> Result<flight::Flight<'d, kind::Send>, result::SubmitError>
    where
        'd: 'r,
        Tag: route::Select<kind::Send>,
    {
        // SAFETY: native admission cannot access this group's free list.
        let front = unsafe { self.front(target.operation::<kind::Send>()) }?;
        let native = context.native().try_admit()?;
        let reserved = front.reserve();
        // SAFETY: the caller retains this descriptor and immutable prefix.
        unsafe {
            Retain::<Host<Backend>>::send(
                native,
                <Backend as Source<request::Context>>::target(fd.slot()),
                buffer.into_prefix(),
                reserved.key(),
            );
        }
        Ok(reserved.commit())
    }

    /// # Safety
    /// Descriptor, target and slots belong to this driver. Keep the descriptor alive and the
    /// message graph fixed and immutable through terminal completion or quiescence.
    pub unsafe fn try_send_msg<'r>(
        &self,
        context: &mut driver::Local<'d>,
        fd: &'r handles::Descriptor<'d>,
        message: msg::Message<'r>,
        target: route::Target<'d, Tag>,
    ) -> Result<flight::Flight<'d, kind::Send>, result::SubmitError>
    where
        'd: 'r,
        Tag: route::Select<kind::Send>,
    {
        // SAFETY: native admission cannot access this group's free list.
        let front = unsafe { self.front(target.operation::<kind::Send>()) }?;
        let native = context.native().try_admit()?;
        let reserved = front.reserve();
        // SAFETY: the caller retains this descriptor and immutable message graph.
        unsafe {
            Retain::<Host<Backend>>::send_msg(
                native,
                <Backend as Source<request::Context>>::target(fd.slot()),
                message,
                reserved.key(),
            );
        }
        Ok(reserved.commit())
    }

    /// # Safety
    /// Listener, target and slots belong to this driver. Keep the listener alive and the
    /// exclusive peer output fixed through terminal completion or quiescence.
    pub(in crate::driver::retained) unsafe fn try_accept_one(
        &self,
        context: &mut driver::Local<'d>,
        fd: &handles::Descriptor<'d>,
        peer: &mut addr::Storage,
        target: route::Operation<'d, Tag, kind::Accept>,
    ) -> Result<flight::Flight<'d, kind::Accept>, result::SubmitError> {
        // SAFETY: native admission cannot access this group's free list.
        let front = unsafe { self.front(target) }?;
        let native = context.native().try_admit()?;
        let reserved = front.reserve();
        // SAFETY: the caller retains this listener and exclusive peer output.
        unsafe {
            Retain::<Host<Backend>>::accept(
                native,
                <Backend as Source<request::Context>>::target(fd.slot()),
                peer,
                reserved.key(),
            );
        }
        Ok(reserved.commit())
    }

    /// # Safety
    /// Target and slots belong to this driver. Retain every file borrow through completion or quiescence.
    pub unsafe fn try_file<'r, C: kind::Flight>(
        &self,
        context: &mut driver::Local<'d>,
        entry: fs::Submission<'r, 'd, Tag, C>,
    ) -> Result<flight::Flight<'d, C>, result::SubmitError>
    where
        'd: 'r,
    {
        let (raw, target) = entry.into_parts();
        // SAFETY: native admission cannot access this group's free list.
        let front = unsafe { self.front(target) }?;
        let native = context.native().try_admit()?;
        let reserved = front.reserve();
        // SAFETY: the caller retains every file request borrow through completion.
        unsafe { Retain::<Host<Backend>>::file(native, raw, reserved.key()) };
        Ok(reserved.commit())
    }
}

/// # Safety
/// The descriptor, target and slots belong to this driver's scope. Retain the
/// exact address and descriptor through terminal completion or quiescence.
pub unsafe fn connect<'d, Tag, A>(
    context: &mut driver::Local<'d>,
    slots: &flight::Slots<'d, Tag>,
    target: route::Target<'d, Tag>,
    fd: &handles::Descriptor<'d>,
    options: &option::Set,
    address: &A,
) -> Result<flight::Flight<'d, kind::Connect>, pending::Cause>
where
    Tag: route::Tag + route::Select<kind::Connect>,
    A: socket::Prepared,
{
    // SAFETY: the caller retains the original descriptor and address for this
    // driver's typed flight. Native submission borrows both without relocation.
    unsafe {
        request::connect(
            context,
            slots,
            target.operation::<kind::Connect>(),
            fd,
            options,
            address.view().native(),
        )
    }
}