dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use std::{marker, os::fd};

use dope_sys::io::{file, reactor::event::kind, request::Request};
use o3::alloc;

use crate::{
    Backend,
    driver::route,
    io::fs::{Dir, File, Submission, open, path},
};

/// The native storage and its destructor are identical for all completion types.
#[repr(C)]
pub struct OpenRequest<T = File> {
    directory: Dir,
    name: Box<std::ffi::CStr>,
    beneath: <Backend as file::File>::Options,
    output: marker::PhantomData<fn() -> T>,
}

impl OpenRequest {
    /// Keeps the confined path non-following and close-on-exec for every opening policy.
    pub fn new(path: path::Confined, options: open::Options) -> Self {
        Self::at::<false>(path.directory, path.path.bytes, options)
    }

    pub fn directory(path: path::Confined) -> OpenRequest<Dir> {
        OpenRequest::at::<false>(
            path.directory,
            path.path.bytes,
            open::Options::new(open::Access::Directory),
        )
    }

    pub fn into_path(self) -> path::Confined {
        path::Confined::new(self.directory, path::Owned { bytes: self.name })
    }
}

impl<T> OpenRequest<T> {
    pub(super) fn at<const NO_SYMLINKS: bool>(
        dir: Dir,
        name: Box<std::ffi::CStr>,
        options: open::Options,
    ) -> Self {
        let (flags, mode) = options.nofollow().lower();
        Self {
            directory: dir,
            name,
            beneath: <<Backend as file::File>::Request<'_> as Request>::options::<NO_SYMLINKS>(
                flags, mode,
            ),
            output: marker::PhantomData,
        }
    }

    pub fn submission<'d, Tag>(
        &self,
        target: route::Target<'d, Tag>,
    ) -> Submission<'_, 'd, Tag, kind::Open>
    where
        Tag: route::Tag + route::Select<kind::Open>,
    {
        Submission::bind(
            Request::open(fd::AsFd::as_fd(&self.directory), &self.name, &self.beneath),
            target.operation::<kind::Open>(),
        )
    }
}

impl OpenRequest<Dir> {
    pub fn into_path(self) -> path::Confined {
        path::Confined::new(self.directory, path::Owned { bytes: self.name })
    }

    /// # Safety
    /// `file` is the successful original native completion of this request.
    pub unsafe fn try_complete(&self, file: File) -> Result<Dir, alloc::Exhausted> {
        Dir::try_new(file.into())
    }
}