dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use dope_sys::{
    engine::ring::Backend as _,
    io::{
        reactor::complete::Outcome as _,
        socket::option::tuning::{self, Occupied as _},
    },
    kernel::Returned as _,
    table::token::Tuning as _,
    table::token::host::{Drain as _, Flight as _},
};

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

impl<'d> Envelope<'d> {
    pub(crate) fn tuning<'event>(
        &self,
        state: Occupied<'_>,
        drain: flight::Drain<'d>,
        control: route::Control,
        output: o3::mem::slot::Vacant<'event, io::Event<'d>>,
    ) -> Result<o3::mem::slot::Occupied<'event, io::Event<'d>>, fault::Fault> {
        let outcome = self.tuning_value(&state, control)?;
        let target = state.target();
        let Some(flight) = drain.bind(target) else {
            state.clear();
            let echo = fault::Echo::new(target);
            return Err(self.reject(
                fault::Subject::Tuning { echo, control },
                fault::Violation::Stale(echo),
            ));
        };
        let observed = flight.kind();
        let event = match observed.into() {
            Shape::Connect(proof) => {
                let option_failed = state.option_failed();
                state.clear();
                outputs::connected(
                    flight.bind(proof).complete(),
                    outcome
                        .map(|_| ())
                        .map_err(|errno| classify(errno, option_failed)),
                )
            }
            Shape::Tuning(proof) => {
                state.clear();
                Kind::Tuning(completion::Completion::from_value(
                    flight.bind(proof),
                    outcome,
                ))
                .into()
            }
            _ => {
                state.clear();
                let echo = fault::Echo::new(flight.end(Flow::Last));
                return Err(self.reject(
                    fault::Subject::Tuning { echo, control },
                    fault::Violation::Tuning(observed),
                ));
            }
        };
        Ok(output.insert(event))
    }

    pub(super) fn reclaim_tuning(
        &mut self,
        lifecycle: &mut Lifecycle<'d, impl io::Report>,
        tune: Tune,
        control: route::Control,
    ) -> Result<(), fault::Fault> {
        let state = match lifecycle
            .local
            .backend
            .tuning()
            .complete(tune.slot().raw(), &mut self.parts.outcome)
        {
            Ok(Some(state)) => state,
            Ok(None) => return Ok(()),
            Err(missing) => {
                let cause = match missing {
                    tuning::Missing::Slot => fault::Violation::Slot,
                    tuning::Missing::Vacant => fault::Violation::Control,
                };
                return Err(self.reject(fault::Subject::Control(control), cause));
            }
        };
        let drain = flight::Drain::new(lifecycle.local.flights, lifecycle.local.driver);
        let result = self.tuning_value(&state, control)?;
        let target = state.target();
        let Some(flight) = drain.bind(target) else {
            state.clear();
            let echo = fault::Echo::new(target);
            return Err(self.reject(
                fault::Subject::Tuning { echo, control },
                fault::Violation::Stale(echo),
            ));
        };
        let observed = flight.kind();
        let event = match observed.into() {
            Shape::Connect(proof) => {
                let option_failed = state.option_failed();
                state.clear();
                io::Reclaimed::Connect {
                    echo: fault::Echo::new(flight.bind(proof).end(Flow::Last)),
                    result: result
                        .map(|_| ())
                        .map_err(|errno| classify(errno, option_failed)),
                }
            }
            Shape::Tuning(proof) => {
                state.clear();
                io::Reclaimed::Tuning {
                    echo: fault::Echo::new(flight.bind(proof).end(Flow::Last)),
                    result: result.map(|_| ()),
                }
            }
            _ => {
                state.clear();
                let echo = fault::Echo::new(flight.end(Flow::Last));
                return Err(self.reject(
                    fault::Subject::Tuning { echo, control },
                    fault::Violation::Tuning(observed),
                ));
            }
        };
        lifecycle.report.report(event);
        Ok(())
    }

    fn tuning_value(
        &self,
        state: &Occupied<'_>,
        control: route::Control,
    ) -> Result<Result<u32, Errno>, fault::Fault> {
        let (expected, value) = state.output();
        let cause = match self.parts.outcome.bytes() {
            Ok(Ok(actual)) if actual.get() as usize == expected => return Ok(Ok(value)),
            Ok(Ok(actual)) => Returned::value(actual),
            Ok(Err(error)) => return Ok(Err(error)),
            Err(error) => error,
        };
        Err(self.reject(
            fault::Subject::Control(control),
            fault::Violation::Outcome(cause),
        ))
    }
}

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