dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use std::mem::ManuallyDrop;

use dope_sys::io::reactor::event::kind;
use o3::mem::slot;

use crate::{
    Errno,
    driver::{
        self,
        retained::flight::{self, storage},
        route,
    },
    io::transfer::Len,
};

pub struct Owned<'d> {
    pub(crate) completion: ManuallyDrop<
        storage::Complete<'d, flight::Target, { flight::INDEX_BITS }, { flight::GENERATION_BITS }>,
    >,
    pub(crate) result: Result<Len, Errno>,
    pub(crate) driver: driver::Invariant<'d>,
}

impl<'d> Owned<'d> {
    pub(crate) fn new(
        done: storage::Complete<
            'd,
            flight::Target,
            { flight::INDEX_BITS },
            { flight::GENERATION_BITS },
        >,
        driver: driver::Invariant<'d>,
        result: Result<Len, Errno>,
    ) -> Self {
        done.terminal();
        Self {
            completion: ManuallyDrop::new(done),
            result,
            driver,
        }
    }

    pub fn token(&self) -> route::Erased<'d, kind::Send> {
        self.completion.value().bind(
            self.driver,
            flight::Echo::from_inner(self.completion.echo()),
        )
    }

    pub const fn outcome(&self) -> Result<Len, Errno> {
        self.result
    }
}

impl<'d> Owned<'d> {
    pub(crate) fn consume(
        owner: slot::Occupied<'_, Self>,
    ) -> storage::Complete<'d, flight::Target, { flight::INDEX_BITS }, { flight::GENERATION_BITS }>
    {
        let mut owner = owner.into_uninit();
        let pointer = owner.get_mut().as_mut_ptr();
        // SAFETY: the original final payload owns this initialized field. Its
        // remaining result and brand have no Drop; only completion moves out.
        let done = unsafe { (&raw const (*pointer).completion).read() };
        ManuallyDrop::into_inner(done)
    }
}

impl Drop for Owned<'_> {
    fn drop(&mut self) {
        // SAFETY: this final owner retains the bound completion exactly once.
        // Finish may have detached the backing; finish handles that same state.
        unsafe { ManuallyDrop::take(&mut self.completion) }.finish();
    }
}