dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use dope_sys::engine::admit;
use dope_sys::io::reactor::event::{Entry, kind};
use o3::mem::slot;

use crate::{
    Backend, Errno,
    driver::{self, life::native::host::Host, retained::flight, route},
    io::{
        fd::handles::Descriptor,
        socket::{option, pending},
    },
};

/// The same descriptor and its observed peer UID, or its pending native owner.
pub enum Query<'d, P = pending::raw::Owner<'d, kind::Tuning>> {
    Ready(Descriptor<'d>, Option<u32>),
    Pending(P),
}

impl<'d> Query<'d> {
    pub fn complete<'slot>(
        owner: slot::Occupied<'slot, pending::raw::Owner<'d, kind::Tuning>>,
        done: &Entry<flight::Complete<'d>, kind::Tuning, Result<u32, Errno>>,
    ) -> pending::raw::Completion<
        'slot,
        'd,
        pending::raw::Owner<'d, kind::Tuning>,
        Result<Option<u32>, Errno>,
    > {
        let (fd, result) = pending::raw::Owner::complete(owner, done)?;
        Ok((fd, result.map(Self::uid)))
    }

    fn uid(value: u32) -> Option<u32> {
        (value != u32::MAX).then_some(value)
    }

    pub fn try_new<Tag: route::Tag + route::Select<kind::Tuning>>(
        driver: &mut driver::Local<'d>,
        slots: &flight::raw::Slots<'d, Tag>,
        target: route::Target<'d, Tag>,
        fd: Descriptor<'d>,
    ) -> Result<Self, pending::Rejection<'d>> {
        let slot = fd.slot();
        // SAFETY: these exact driver-branded owners retain the native slot and
        // flight until terminal completion; native admission cannot reenter slots.
        unsafe {
            admit::raw::Socket::<Host<Backend>, { option::MAX_STREAM_OPTIONS }>::peer(
                &mut *driver.backend,
                (slots, target.operation::<kind::Tuning>()),
                (fd, driver.driver.brand),
                slot,
                &mut driver.retry,
            )
        }
    }
}

impl<'d> From<((Descriptor<'d>, driver::Invariant<'d>), u32)> for Query<'d> {
    fn from(((fd, _), value): ((Descriptor<'d>, driver::Invariant<'d>), u32)) -> Self {
        Self::Ready(fd, Self::uid(value))
    }
}

impl<'d, P>
    From<(
        (Descriptor<'d>, driver::Invariant<'d>),
        flight::Flight<'d, kind::Tuning>,
        P,
    )> for Query<'d, pending::raw::Owner<'d, kind::Tuning, P>>
{
    fn from(
        ((fd, _), flight, permit): (
            (Descriptor<'d>, driver::Invariant<'d>),
            flight::Flight<'d, kind::Tuning>,
            P,
        ),
    ) -> Self {
        Self::Pending(pending::raw::Owner::new(fd, flight, permit))
    }
}