dope-uring 0.77.0

Owned io_uring backend for dope
Documentation
use std::{marker::PhantomData, num};

use dope_sys::{
    Errno,
    engine::ring,
    io::{
        reactor::{
            complete::{
                Flags as _, Outcome as _,
                flags::{Flow, Meta as _},
            },
            event::fault,
        },
        socket::option::tuning::{Missing, Occupied as _, Table as _},
    },
    kernel::Returned as _,
    table::{
        Slots,
        reclaim::{self, Queue as _},
        token::{
            self, Codec, Tuning as _,
            frame::{self, Decode as _},
            host::{self, Echo as _},
        },
    },
};

use crate::{
    Observed,
    driver::engine::{
        Decoded, NativeControl, NativeFault, NativeKey, NativeReturned, NativeSlot, Subject,
        Violation,
    },
};

type Tune<C> = <<C as ring::Context>::Token as token::Codec>::Tuning;
type Frame<C> = <<C as ring::Context>::Token as token::Codec>::Frame;

pub(super) struct Parts<C: ring::Context, F: host::Flight> {
    pub(super) raw: Observed<C::Token>,
    flight: PhantomData<fn() -> F>,
}

impl<C: ring::Context, F: host::Flight> From<Observed<C::Token>> for Parts<C, F> {
    fn from(raw: Observed<C::Token>) -> Self {
        Self {
            raw,
            flight: PhantomData,
        }
    }
}

impl<C, F, S, N, H, J> Parts<C, F>
where
    C: ring::Context,
    NativeSlot<C>: TryFrom<u32>,
    F: host::Flight<Key = NativeKey<C>, Fault = NativeFault<C, F, S, N, H, J>>,
    F::Observation: for<'a> From<&'a crate::Observed<C::Token>>,
{
    pub(super) fn reject(
        &self,
        subject: Subject<C, F>,
        cause: Violation<C, F, S, N, H, J>,
    ) -> F::Fault {
        fault::Fault {
            observation: (&self.raw).into(),
            subject,
            violation: cause,
        }
    }

    pub(super) fn flow(&self) -> Flow {
        self.raw.flags.meta().flow()
    }

    fn is_plain(&self) -> bool {
        self.raw.flags.0 == 0
    }

    pub(super) fn unbound(&self) -> F::Fault {
        self.reject(fault::Subject::Unbound, fault::Violation::UserData)
    }

    pub(super) fn frame(&self, control: Frame<C>) -> frame::Control<Tune<C>> {
        if self.is_plain() {
            control.decode()
        } else {
            frame::Control::Unknown
        }
    }

    pub(super) fn control(&self, frame: Frame<C>) -> F::Fault {
        self.reject(
            fault::Subject::Control(frame.control()),
            fault::Violation::Control,
        )
    }

    pub(super) fn bytes(&self) -> Decoded<F::Length, C, F, S, N, H, J> {
        self.plain()?;
        Ok(self
            .raw
            .outcome
            .bytes()
            .map_err(fault::Violation::Outcome)?
            .map(F::Length::from))
    }

    pub(super) fn unit(&self) -> Decoded<(), C, F, S, N, H, J> {
        self.plain()?;
        self.raw.outcome.unit().map_err(fault::Violation::Outcome)
    }

    pub(super) fn plain(&self) -> Result<(), Violation<C, F, S, N, H, J>> {
        if !self.is_plain() {
            return Err(fault::Violation::Result);
        }
        Ok(())
    }

    pub(super) fn tuning_value<const OPTIONS: usize>(
        &self,
        state: &crate::driver::raw::Occupied<'_, NativeKey<C>, OPTIONS>,
        control: NativeControl<C>,
    ) -> Result<Result<u32, Errno>, F::Fault> {
        let (expected, value) = state.output();
        let cause = match self.raw.outcome.bytes() {
            Ok(Ok(actual)) if actual.get() as usize == expected => return Ok(Ok(value)),
            Ok(Ok(actual)) => crate::error::Returned::value(actual),
            Ok(Err(error)) => return Ok(Err(error)),
            Err(error) => error,
        };
        Err(self.reject(
            fault::Subject::Control(control),
            fault::Violation::Outcome(cause),
        ))
    }

    pub(super) fn advance<'a, const OPTIONS: usize>(
        &mut self,
        table: &'a mut crate::driver::raw::Table<NativeKey<C>, OPTIONS>,
        tune: Tune<C>,
        control: NativeControl<C>,
    ) -> Result<Option<crate::driver::raw::Occupied<'a, NativeKey<C>, OPTIONS>>, F::Fault> {
        let slot: num::NonZero<u32> = tune.slot().into();
        match table.complete(slot.get() - 1, &mut self.raw.outcome) {
            Ok(state) => Ok(state),
            Err(missing) => {
                let cause = match missing {
                    Missing::Slot => fault::Violation::Slot,
                    Missing::Vacant => fault::Violation::Control,
                };
                Err(self.reject(fault::Subject::Control(control), cause))
            }
        }
    }

    pub(super) fn reject_raw<A>(&self, fds: &A, cause: fault::Raw<S>) -> F::Fault
    where
        A: Slots<F>,
        A::Retirement: reclaim::Queue<Returned = NativeReturned<C>, Class = J>,
    {
        let closed = match self.raw.token.closed() {
            Some(returned) => fds.retirement().bind(returned),
            None => host::Closed::Missing,
        };
        let failure = match closed {
            host::Closed::Class {
                echo,
                expected,
                observed,
            } => {
                echo.retry();
                fault::Violation::RawClass {
                    cause,
                    expected,
                    observed,
                }
            }
            other => {
                if let host::Closed::Ready(echo) = other {
                    echo.retry();
                }
                fault::Violation::Raw(cause)
            }
        };
        self.reject(fault::Subject::Unbound, failure)
    }

    pub(super) fn returned<A>(&self, returned: NativeReturned<C>, fds: &A) -> Result<(), F::Fault>
    where
        A: Slots<F>,
        A::Retirement: reclaim::Queue<Returned = NativeReturned<C>, Class = J>,
    {
        let failure = match fds.retirement().bind(returned) {
            host::Closed::Ready(echo) => {
                if self.is_plain() && self.raw.outcome.is_zero() {
                    echo.complete();
                    return Ok(());
                }
                echo.retry();
                fault::Violation::Close
            }
            host::Closed::Class {
                echo,
                expected,
                observed,
            } => {
                echo.retry();
                fault::Violation::Class { expected, observed }
            }
            host::Closed::Missing => return Err(self.unbound()),
        };
        Err(self.reject(fault::Subject::Unbound, failure))
    }

    pub(super) fn cancel<const OPTIONS: usize>(
        &self,
        state: &mut crate::driver::raw::Table<NativeKey<C>, OPTIONS>,
        cancel: host::Cancel<NativeKey<C>, Tune<C>>,
    ) -> Result<(), F::Fault> {
        let subject = match &cancel {
            host::Cancel::Flight(echo) => fault::Subject::CancelFlight(fault::Echo::new(*echo)),
            host::Cancel::Tuning(tune) => fault::Subject::CancelTuning(tune.slot()),
        };
        if !self.is_plain() {
            return Err(self.reject(subject, fault::Violation::Control));
        }
        let outcome = self.raw.outcome;
        if outcome.is_error()
            && let host::Cancel::Tuning(tune) = &cancel
        {
            let slot: num::NonZero<u32> = tune.slot().into();
            state.retry_cancel(slot.get() - 1);
        }
        match outcome.canceled() {
            Ok(()) => Ok(()),
            Err(cause) => Err(self.reject(subject, fault::Violation::Outcome(cause))),
        }
    }

    pub(super) fn reject_bound(&self, flight: F, cause: Violation<C, F, S, N, H, J>) -> F::Fault {
        let target = flight.reject(self.flow());
        self.reject(fault::Subject::Target(target), cause)
    }
}