dope-uring 0.77.0

Owned io_uring backend for dope
Documentation
use std::{marker, mem, os::fd, ptr};

use dope_sys::io::{
    file::{self, Mode, Offset, Open, Sync},
    reactor::complete,
    request,
};
use o3::buffer::bytes::{self, bounded};

use crate::{
    Backend,
    reactor::{op::files, sq, types},
};

/// An SQE tied to every borrowed address encoded in it.
#[repr(transparent)]
pub struct Borrowed<'a>(pub(super) sq::Entry, marker::PhantomData<&'a mut ()>);

impl<'a> Borrowed<'a> {
    pub fn watch(
        watch: &'a <Backend as file::raw::Directory>::Watch,
        output: bounded::Value<&'a mut [mem::MaybeUninit<u8>]>,
    ) -> Self {
        Self::file_read(fd::AsFd::as_fd(watch), output, 0)
    }

    pub(crate) const fn new(entry: sq::Entry) -> Self {
        Self(entry, marker::PhantomData)
    }

    pub(crate) fn user_data<T: complete::raw::Token>(self, token: T) -> sq::Submit<'a, T> {
        self.0.bind(token)
    }

    fn file_read(
        file: fd::BorrowedFd<'a>,
        output: bounded::Value<&'a mut [mem::MaybeUninit<u8>]>,
        offset: u64,
    ) -> Self {
        let output = output.into_inner();
        let raw = fd::AsRawFd::as_raw_fd(&file);
        let mut entry = sq::Entry::new(files::READ, sq::Descriptor::File(raw));
        let fields = entry.fields();
        fields.command.address = output.as_mut_ptr() as u64;
        fields.command.length = output.len() as u32;
        fields.command.offset = offset.to_ne_bytes();
        Self::new(entry)
    }
}

impl<'a> request::Request<'a> for Borrowed<'a> {
    type Buffer = bytes::Prefix<'a, { i32::MAX as usize }>;
    type Options = types::OpenHow;
    type Metadata = crate::raw::Metadata;

    fn options<const NO_SYMLINKS: bool>(flags: Open, mode: Mode) -> Self::Options {
        let links = if NO_SYMLINKS {
            libc::RESOLVE_NO_SYMLINKS
        } else {
            libc::RESOLVE_NO_MAGICLINKS
        };
        types::OpenHow::new(
            flags.raw() as u32 as u64,
            mode.raw() as u64,
            libc::RESOLVE_BENEATH | links,
        )
    }

    fn open(dir: fd::BorrowedFd<'a>, path: &'a std::ffi::CStr, options: &'a Self::Options) -> Self {
        let raw = fd::AsRawFd::as_raw_fd(&dir);
        let mut entry = sq::Entry::new(files::OPEN, sq::Descriptor::File(raw));
        let fields = entry.fields();
        fields.command.address = path.as_ptr() as u64;
        fields.command.length = mem::size_of::<Self::Options>() as u32;
        fields.command.offset = (ptr::from_ref(options) as u64).to_ne_bytes();
        Self::new(entry)
    }

    fn metadata(file: fd::BorrowedFd<'a>, output: &'a mut Self::Metadata) -> Self {
        let raw = fd::AsRawFd::as_raw_fd(&file);
        let mut entry = sq::Entry::new(files::METADATA, sq::Descriptor::File(raw));
        let fields = entry.fields();
        fields.command.address = c"".as_ptr() as u64;
        fields.command.length = Self::Metadata::MASK | libc::STATX_INO;
        fields.command.offset = (output.0.as_mut_ptr() as u64).to_ne_bytes();
        fields.command.options = libc::AT_EMPTY_PATH as u32;
        Self::new(entry.flags(sq::Flags::ASYNC))
    }

    fn write(file: fd::BorrowedFd<'a>, input: Self::Buffer, offset: Offset) -> Self {
        let input = input.as_slice();
        let raw = fd::AsRawFd::as_raw_fd(&file);
        let mut entry = sq::Entry::new(files::WRITE, sq::Descriptor::File(raw));
        let fields = entry.fields();
        fields.command.address = input.as_ptr() as u64;
        fields.command.length = input.len() as u32;
        fields.command.offset = offset.get().to_ne_bytes();
        Self::new(entry.flags(sq::Flags::ASYNC))
    }

    fn read(
        file: fd::BorrowedFd<'a>,
        output: bounded::Value<&'a mut [mem::MaybeUninit<u8>]>,
        offset: Offset,
    ) -> Self {
        Self::new(
            Self::file_read(file, output, offset.get())
                .0
                .flags(sq::Flags::ASYNC),
        )
    }

    fn sync(file: fd::BorrowedFd<'a>, mode: Sync) -> Self {
        let raw = fd::AsRawFd::as_raw_fd(&file);
        let mut entry = sq::Entry::new(files::SYNC, sq::Descriptor::File(raw));
        entry.fields().command.options = mode as u32;
        Self::new(entry.flags(sq::Flags::ASYNC))
    }

    fn rename(dir: fd::BorrowedFd<'a>, old: &'a std::ffi::CStr, new: &'a std::ffi::CStr) -> Self {
        let raw = fd::AsRawFd::as_raw_fd(&dir);
        let mut entry = sq::Entry::new(files::RENAME, sq::Descriptor::File(raw));
        let fields = entry.fields();
        fields.command.address = old.as_ptr() as u64;
        fields.command.length = raw as u32;
        fields.command.offset = (new.as_ptr() as u64).to_ne_bytes();
        Self::new(entry.flags(sq::Flags::ASYNC))
    }

    fn unlink(dir: fd::BorrowedFd<'a>, name: &'a std::ffi::CStr) -> Self {
        let raw = fd::AsRawFd::as_raw_fd(&dir);
        let mut entry = sq::Entry::new(files::UNLINK, sq::Descriptor::File(raw));
        entry.fields().command.address = name.as_ptr() as u64;
        Self::new(entry.flags(sq::Flags::ASYNC))
    }
}

const _: () = {
    assert!(mem::size_of::<Borrowed<'static>>() == mem::size_of::<sq::Entry>());
    assert!(mem::size_of::<Option<Borrowed<'static>>>() == 72);
};