dope-core 0.73.0

Owned native I/O and completion driver for dope
Documentation
use dope_sys::{
    io::{
        reactor::complete::{Outcome as _, Packet as _},
        socket::option::{
            tuning,
            tuning::{Occupied as _, Table as _},
        },
    },
    kernel::Returned as _,
    probe::Stopped,
    table::token::{Tuning as _, host::Flight as _},
};

use crate::{
    Errno,
    driver::{
        result::poll::fault,
        retained::flight,
        route::{self, kind, kind::Shape},
    },
    io,
    io::{
        event::{
            Kind,
            batch::Flow,
            net::{connect, tuning as completion},
        },
        outputs,
    },
    ring::cqe::{Envelope, Occupied, Rejected, Returned, Tune, Tuning},
};

pub(crate) struct Tuned<'state, 'q, 'd> {
    state: Occupied<'state>,
    flight: Terminal<'q, 'd>,
}

enum Terminal<'q, 'd> {
    Connect(flight::Complete<'q, 'd, kind::Connect>),
    Tuning(flight::Complete<'q, 'd, kind::Tuning>),
}

impl<'state, 'q, 'd> Tuned<'state, 'q, 'd> {
    pub(super) fn complete<'event>(
        self,
        outcome: Result<u32, Errno>,
        output: o3::mem::slot::Vacant<'event, io::Event<'d>>,
    ) -> o3::mem::slot::Occupied<'event, io::Event<'d>> {
        let Self { state, flight } = self;
        let option_failed = state.option_failed();
        state.clear();
        output.insert(match flight {
            Terminal::Connect(bound) => outputs::connected(
                bound.complete(),
                outcome.map(|_| ()).map_err(|errno| classify(errno, option_failed)),
            ),
            Terminal::Tuning(bound) => {
                Kind::Tuning(completion::Completion::from_value(bound, outcome)).into()
            }
        })
    }

    pub(super) fn reclaim(self, result: Result<u32, Errno>) -> io::Reclaimed {
        let Self { state, flight } = self;
        let option_failed = state.option_failed();
        state.clear();
        match flight {
            Terminal::Connect(bound) => io::Reclaimed::Connect {
                echo: fault::Echo::new(bound.end(Flow::Last)),
                result: result.map(|_| ()).map_err(|errno| classify(errno, option_failed)),
            },
            Terminal::Tuning(bound) => io::Reclaimed::Tuning {
                echo: fault::Echo::new(bound.end(Flow::Last)),
                result: result.map(|_| ()),
            },
        }
    }
    pub(super) fn bind(
        states: &'state mut Tuning,
        drain: &'q flight::Drain<'q, 'd>,
        txn: Tune,
        packet: Envelope<'d>,
        control: route::Control,
    ) -> Result<(Self, Result<u32, Errno>), Rejected<'d>> {
        use dope_sys::table::token::host::Drain as _;
        let state = match states.bind(txn.slot().raw()) {
            Ok(state) => state,
            Err(missing) => {
                let cause = match missing {
                    tuning::Missing::Slot => fault::Violation::Slot,
                    tuning::Missing::Vacant => fault::Violation::Control,
                };
                return Err(packet.reject(fault::Subject::Control(control), cause));
            }
        };
        let (expected, value) = state.output();
        let outcome = match packet.0.parts().outcome.bytes() {
            Ok(Ok(actual)) if actual.get() as usize == expected => Ok(Ok(value)),
            Ok(Ok(actual)) => Err(Returned::value(actual)),
            Ok(Err(error)) => Ok(Err(error)),
            Err(error) => Err(error),
        };
        let ((packet, _), outcome) =
            Stopped::attach((packet, fault::Subject::Control(control)), outcome)?;
        let target = state.target();
        let Some(flight) = drain.bind(target) else {
            state.clear();
            let echo = fault::Echo::new(target);
            return Err(packet.reject(
                fault::Subject::Tuning { echo, control },
                fault::Violation::Stale(echo),
            ));
        };
        let observed = flight.kind();
        let flight = match observed.into() {
            Shape::Connect(proof) => Terminal::Connect(flight.bind(proof)),
            Shape::Tuning(proof) => Terminal::Tuning(flight.bind(proof)),
            _ => {
                state.clear();
                let echo = fault::Echo::new(flight.end(Flow::Last));
                return Err(packet.reject(
                    fault::Subject::Tuning { echo, control },
                    fault::Violation::Tuning(observed),
                ));
            }
        };
        Ok((Tuned { state, flight }, outcome))
    }
}

fn classify(errno: Errno, option_failed: bool) -> connect::Error {
    if option_failed {
        connect::Error::Option(errno)
    } else {
        connect::Error::Connect(errno)
    }
}