dope-core 0.75.0

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

use o3::mem::slot;

use crate::{
    Backend,
    driver::{
        self,
        life::native::host::Host,
        result,
        retained::flight,
        route::{self, kind},
    },
    io::socket::StreamSpec,
};
use dope_sys::engine::admit::Guard;

use super::{CreatedSlot, Creation, Descriptor, SocketSlot};

impl<'d> Creation<'d> {
    pub fn try_new<ID: route::Tag + route::Select<kind::Socket>>(
        driver: &mut driver::Local<'d>,
        slots: &flight::Slots<'d, ID>,
        request: route::Bound<'d, ID, SocketSlot<'d>>,
        socket: StreamSpec,
    ) -> Result<Self, result::SubmitError> {
        let (target, socket_slot) = request.into_parts();
        // SAFETY: native capacity admission cannot change this flight free list.
        // The checked head remains borrowed until the admitted reserve below.
        let front = unsafe { slots.front(target.operation::<kind::Socket>()) }?;
        let native = driver.native().try_admit()?;
        let reserved = front.reserve();
        Guard::<Host<Backend>>::socket(
            native,
            socket_slot.outbound(),
            socket.domain(),
            reserved.key(),
        );
        Ok(socket_slot.begin(reserved.commit()))
    }

    /// Consumes the original creation after its native authority is quiescent.
    pub fn detach(mut owner: slot::Occupied<'_, Self>) {
        let value = ptr::from_mut(owner.get_mut());
        drop(owner.into_uninit());
        // SAFETY: the affine slot owns both initialized fields. The flight is
        // retired before the descriptor lease; the backing no longer drops Self.
        unsafe {
            ptr::drop_in_place(&raw mut (*value).flight);
            ptr::drop_in_place(&raw mut (*value).lease);
        }
    }
}

impl<'d> CreatedSlot<'d> {
    pub fn join<'slot>(
        made: slot::Occupied<'_, Self>,
        mut creating: slot::Occupied<'slot, Creation<'d>>,
        token: route::Erased<'d, kind::Socket>,
    ) -> Result<slot::Occupied<'slot, Descriptor<'d>>, slot::Occupied<'slot, Creation<'d>>> {
        if !creating.flight.matches(token) || !made.inner.item().is_same(creating.lease.slot()) {
            return Err(creating);
        }
        let value = ptr::from_mut(creating.get_mut());
        drop(made.into_uninit());
        drop(creating.into_uninit());
        // SAFETY: both matching halves are consumed. Creation has a checked
        // descriptor-sized, aligned prefix; only its lease encoding changes.
        // Activation precedes flight release, as on the native creation path.
        unsafe {
            let lease = (&raw const (*value).lease).read().activate();
            ptr::drop_in_place(&raw mut (*value).flight);
            Ok(
                slot::Vacant::new(&mut *value.cast::<MaybeUninit<Descriptor<'d>>>())
                    .insert(Descriptor { lease }),
            )
        }
    }
}