linux_io_uring/
register.rs1use std::{ io, ptr };
2use std::os::unix::io::RawFd;
3use linux_io_uring_sys as sys;
4
5
6unsafe fn execute(fd: RawFd, opcode: libc::c_uint, arg: *const libc::c_void, len: libc::c_uint)
7 -> io::Result<()>
8{
9 if 0 == sys::io_uring_register(fd, opcode, arg, len) {
10 Ok(())
11 } else {
12 Err(io::Error::last_os_error())
13 }
14}
15
16#[allow(clippy::module_inception)]
18pub mod register {
19 use super::*;
20
21 #[non_exhaustive]
22 pub enum Target<'a> {
23 Buffers(&'a [libc::iovec]),
27
28 Files(&'a [RawFd]),
31
32 EventFd(RawFd),
34
35 #[cfg(feature = "unstable")]
36 FilesUpdate { offset: u32, fds: &'a [RawFd] }
37 }
38
39 impl Target<'_> {
40 pub(crate) unsafe fn execute(&self, fd: RawFd) -> io::Result<()> {
41 fn cast_ptr<T>(n: &T) -> *const T {
42 n as *const T
43 }
44
45 match self {
46 Target::Buffers(bufs) =>
47 execute(fd, sys::IORING_REGISTER_BUFFERS, bufs.as_ptr() as *const _, bufs.len() as _),
48 Target::Files(fds) =>
49 execute(fd, sys::IORING_REGISTER_FILES, fds.as_ptr() as *const _, fds.len() as _),
50 Target::EventFd(eventfd) =>
51 execute(fd, sys::IORING_REGISTER_EVENTFD, cast_ptr::<RawFd>(eventfd) as *const _, 1),
52 #[cfg(feature = "unstable")]
53 Target::FilesUpdate { offset, fds } => {
54 let fu = sys::io_uring_files_update {
55 offset: *offset,
56 fds: fds.as_ptr() as *mut _
57 };
58 let fu = &fu as *const sys::io_uring_files_update;
59
60 execute(fd, sys::IORING_REGISTER_FILES_UPDATE, fu as *const _, fds.len() as _)
61 }
62 }
63 }
64 }
65}
66
67pub mod unregister {
69 use super::*;
70
71 #[non_exhaustive]
72 pub enum Target {
73 Buffers,
75
76 Files,
78
79 EventFd,
81 }
82
83 impl Target {
84 pub(crate) fn execute(&self, fd: RawFd) -> io::Result<()> {
85 let opcode = match self {
86 Target::Buffers => sys::IORING_UNREGISTER_BUFFERS,
87 Target::Files => sys::IORING_UNREGISTER_FILES,
88 Target::EventFd => sys::IORING_UNREGISTER_EVENTFD
89 };
90
91 unsafe {
92 execute(fd, opcode, ptr::null(), 0)
93 }
94 }
95 }
96}