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::{
    Errno,
    driver::{retained::flight, route::kind},
    io::{
        event::net::{connect, tuning},
        fd::handles::Descriptor,
    },
};

use super::{Connection, Tuning};

impl<'d, V> Tuning<'d, V> {
    fn resolve<'slot>(
        mut owner: slot::Occupied<'slot, Self>,
        done: &tuning::Completion<'d>,
    ) -> Result<
        (slot::Occupied<'slot, Descriptor<'d>>, Result<u32, Errno>),
        slot::Occupied<'slot, Self>,
    > {
        if !owner.flight.matches(done.token()) {
            return Err(owner);
        }
        let value = ptr::from_mut(owner.get_mut());
        let result = done.outcome();
        drop(owner.into_uninit());
        // SAFETY: the matching terminal completion ends the original flight.
        // Descriptor is the repr(C) prefix; the affine owner transfers only
        // that initialized field after destroying the remaining fields once.
        unsafe {
            ptr::drop_in_place(&raw mut (*value).flight);
            ptr::drop_in_place(&raw mut (*value).permit);
            Ok((
                slot::Vacant::new(&mut *value.cast::<MaybeUninit<Descriptor<'d>>>()).assume_init(),
                result,
            ))
        }
    }
}

impl<'d> Tuning<'d> {
    pub fn complete<'slot>(
        owner: slot::Occupied<'slot, Self>,
        done: &tuning::Completion<'d>,
    ) -> Result<
        (slot::Occupied<'slot, Descriptor<'d>>, Result<(), Errno>),
        slot::Occupied<'slot, Self>,
    > {
        let (fd, result) = Self::resolve(owner, done)?;
        Ok((fd, result.map(|_| ())))
    }
}

impl<'d> Tuning<'d, u32> {
    pub fn complete<'slot>(
        owner: slot::Occupied<'slot, Self>,
        done: &tuning::Completion<'d>,
    ) -> Result<
        (
            slot::Occupied<'slot, Descriptor<'d>>,
            Result<Option<u32>, Errno>,
        ),
        slot::Occupied<'slot, Self>,
    > {
        let (fd, result) = Self::resolve(owner, done)?;
        Ok((fd, result.map(crate::io::socket::peer::uid)))
    }
}

impl<'d> Connection<'d> {
    pub fn complete<'slot>(
        mut owner: slot::Occupied<'slot, Self>,
        done: &connect::Completion<'d>,
    ) -> Result<
        (
            slot::Occupied<'slot, Descriptor<'d>>,
            Result<(), connect::Error>,
        ),
        slot::Occupied<'slot, Self>,
    > {
        if !owner.flight.matches(done.token()) {
            return Err(owner);
        }
        let value = ptr::from_mut(owner.get_mut());
        let result = done.outcome();
        drop(owner.into_uninit());
        // SAFETY: the matching terminal completion ends the flight. The
        // descriptor prefix remains initialized in its original exclusive slot.
        unsafe {
            ptr::drop_in_place(&raw mut (*value).flight);
            Ok((
                slot::Vacant::new(&mut *value.cast::<MaybeUninit<Descriptor<'d>>>()).assume_init(),
                result,
            ))
        }
    }

    /// The original backing must have Connection's size and alignment, with
    /// only its descriptor prefix initialized and no surviving pin obligation.
    pub unsafe fn install<'slot>(
        mut fd: slot::Occupied<'slot, Descriptor<'d>>,
        flight: flight::Flight<'d, kind::Connect>,
    ) -> slot::Occupied<'slot, Self> {
        let value = ptr::from_mut(fd.get_mut()).cast::<Self>();
        drop(fd.into_uninit());
        // SAFETY: the caller owns the full vacant tail of this exact backing.
        // Writing the flight initializes the only remaining Connection field.
        unsafe {
            (&raw mut (*value).flight).write(flight);
            slot::Vacant::new(&mut *value.cast::<MaybeUninit<Self>>()).assume_init()
        }
    }
}