dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use std::{marker, mem};

use dope_sys::engine;

use crate::{
    Engine, Errno, driver,
    driver::{
        result,
        retained::{flight, flight::cancel},
        route::{indices, kind},
    },
    io::fd::handles,
};

mod raw;

/// Exact cause of a socket submission rejection.
#[derive(Debug)]
pub enum Cause {
    Submit(result::SubmitError),
    Option(Errno),
}

#[must_use = "a rejected establishment retains its exact descriptor and cause"]
pub struct Rejection<'d> {
    fd: handles::Descriptor<'d>,
    cause: Cause,
}

#[doc(hidden)]
#[must_use = "a pending establishment owns its descriptor and completion authority"]
#[repr(C)]
pub struct Tuning<'d, V = ()> {
    fd: handles::Descriptor<'d>,
    flight: flight::Flight<'d, kind::Tuning>,
    permit: <Engine as engine::Cancel<indices::SlotIndex>>::Permit,
    value: marker::PhantomData<V>,
}

#[doc(hidden)]
#[must_use = "a pending establishment owns its descriptor and completion authority"]
#[repr(C)]
pub struct Connection<'d> {
    fd: handles::Descriptor<'d>,
    flight: flight::Flight<'d, kind::Connect>,
}

impl<'d, V> Tuning<'d, V> {
    #[doc(hidden)]
    pub const fn fd(&self) -> &handles::Descriptor<'d> {
        &self.fd
    }
}

impl<'d, V> Tuning<'d, V> {
    #[cfg(target_os = "linux")]
    pub(super) fn new(
        fd: handles::Descriptor<'d>,
        flight: flight::Flight<'d, kind::Tuning>,
        permit: <Engine as engine::Cancel<indices::SlotIndex>>::Permit,
    ) -> Self {
        Self {
            fd,
            flight,
            permit,
            value: marker::PhantomData,
        }
    }

    pub fn try_cancel(
        &mut self,
        driver: &mut driver::Local<'d>,
    ) -> Result<(), dope_sys::io::reactor::Full> {
        if !self.flight.is_pending() {
            return Ok(());
        }
        engine::Cancel::cancel(driver.native().as_mut(), &self.permit, self.fd.index())
    }
}

impl<'d> Connection<'d> {
    #[doc(hidden)]
    pub const fn fd(&self) -> &handles::Descriptor<'d> {
        &self.fd
    }
}

impl<'d> Connection<'d> {
    pub fn try_cancel(&mut self, driver: &mut driver::Local<'d>) -> Result<(), cancel::Error> {
        driver
            .native()
            .try_stop_at(&mut self.flight, &self.fd)
            .map(|_| ())
    }
}

impl<'d> Rejection<'d> {
    pub(crate) const fn new(fd: handles::Descriptor<'d>, cause: Cause) -> Self {
        Self { fd, cause }
    }

    pub fn into_parts(self) -> (handles::Descriptor<'d>, Cause) {
        (self.fd, self.cause)
    }
}

const _: () = {
    assert!(
        mem::size_of::<Tuning<'_>>()
            == mem::size_of::<(handles::Descriptor<'_>, flight::Flight<'_>)>()
    );
    assert!(
        mem::align_of::<Tuning<'_>>()
            == mem::align_of::<(handles::Descriptor<'_>, flight::Flight<'_>)>()
    );
    assert!(mem::offset_of!(Tuning<'_>, fd) == 0);
    assert!(mem::size_of::<Tuning<'_, u32>>() == mem::size_of::<Tuning<'_>>());
    assert!(
        mem::size_of::<Connection<'_>>()
            == mem::size_of::<(handles::Descriptor<'_>, flight::Flight<'_, kind::Connect>)>()
    );
    assert!(mem::offset_of!(Connection<'_>, fd) == 0);
    assert!(mem::size_of::<Rejection<'_>>() == mem::size_of::<(handles::Descriptor<'_>, Cause)>());
    assert!(
        mem::align_of::<Rejection<'_>>() == mem::align_of::<(handles::Descriptor<'_>, Cause)>()
    );
};