dope-uring 0.77.0

Owned io_uring backend for dope
Documentation
pub(in crate::reactor::ring) mod axis;
pub(in crate::reactor::ring) mod cleanup;
pub(in crate::reactor::ring) mod closing;

use std::{mem, ptr};

use dope_sys::{
    io::{Close, Retry},
    kernel,
    ring::create,
};
use o3::mem::Rejected;

use crate::{
    Mapping, error,
    reactor::{
        cq,
        mappings::{Buffer, Region, Unmap},
        ring, sq,
    },
};

type Cause = create::Cause<kernel::Errno<error::Setup>, kernel::Errno<error::Map>>;

#[derive(Debug)]
pub(crate) struct Closing(State<Unmap>);
pub(crate) struct Pending(State<Region>);

pub(crate) enum Open {
    Failure(Mapping),
    Cleanup { failure: Mapping, unmap: Unmap },
}

#[derive(Debug)]
pub(super) struct Mapped {
    pub(super) sq: sq::raw::State,
    pub(super) completion: cq::raw::State,
    pub(super) maps: ring::ffi::Map,
}

#[derive(Debug)]
struct State<C> {
    current: C,
    next: Option<Buffer<u8>>,
}

impl Closing {
    pub(crate) fn failure(&self) -> crate::Unmap {
        (&self.0.current).into()
    }

    pub(crate) fn close(current: Buffer<u8>, next: Option<Buffer<u8>>) -> Result<(), Self> {
        Pending(State {
            current: current.into_region(),
            next,
        })
        .try_close()
    }

    pub(crate) const fn dirty(current: Unmap, next: Option<Buffer<u8>>) -> Self {
        Self(State { current, next })
    }
}

impl Retry for Closing {
    type Failure = crate::Unmap;
    type Owner = Pending;

    fn recover(self) -> Rejected<Pending, crate::Unmap> {
        let State { current, next } = self.0;
        let cause = (&current).into();
        Rejected::new(
            Pending(State {
                current: current.into(),
                next,
            }),
            cause,
        )
    }
}

impl Close for Pending {
    type State = Closing;

    fn try_close(self) -> Result<(), Closing> {
        let State { current, next } = self.0;
        match current.try_close() {
            Ok(()) => next.map_or(Ok(()), |next| Closing::close(next, None)),
            Err(current) => Err(Closing::dirty(current, next)),
        }
    }
}

impl Open {
    pub(super) fn recover(self, previous: Option<Buffer<u8>>) -> ring::Error {
        match self {
            Self::Failure(failure) => cleanup::Cleanup::finish(
                None,
                Cause::Map(failure),
                previous.map_or(Ok(()), |map| Closing::close(map, None)),
            ),
            Self::Cleanup { failure, unmap } => ring::Error::Cleanup(cleanup::Cleanup::new(
                None,
                Cause::Map(failure),
                Closing::dirty(unmap, previous),
            )),
        }
    }
}

const _: () = {
    assert!(
        mem::size_of::<Mapped>()
            == mem::size_of::<sq::raw::State>()
                + mem::size_of::<cq::raw::State>()
                + mem::size_of::<ring::ffi::Map>()
    );
    assert!(mem::align_of::<Mapped>() == mem::align_of::<ring::ffi::Map>());
    assert!(mem::size_of::<Option<Mapped>>() == mem::size_of::<Mapped>());
    assert!(mem::size_of::<ring::ffi::Map>() == 2 * mem::size_of::<Buffer<u8>>());
    assert!(mem::align_of::<ring::ffi::Map>() == mem::align_of::<Buffer<u8>>());
    assert!(mem::size_of::<crate::Unmap>() == 3 * mem::size_of::<usize>());
    assert!(mem::align_of::<Unmap>() == mem::align_of::<ptr::NonNull<libc::c_void>>());
};