Skip to main content

compio_driver/sys/extra/
mod.rs

1cfg_select! {
2    windows => {
3        mod iocp;
4        use iocp as sys;
5    }
6    fusion => {
7        mod fusion;
8        mod poll;
9        mod iour;
10        use fusion as sys;
11    }
12    io_uring => {
13        mod iour;
14        use iour as sys;
15    }
16    polling => {
17        mod poll;
18        use poll as sys;
19    }
20    stub => {
21        mod stub;
22        use stub as sys;
23    }
24    _ => {}
25}
26
27use std::{fmt::Debug, io};
28
29#[allow(unused_imports)]
30pub use sys::*;
31
32/// Platform-specific extra data associated with a driver instance.
33///
34/// It can be used to set options for or get extra data from I/O operations.
35#[repr(transparent)]
36pub struct Extra(pub(super) sys::Extra);
37
38impl Debug for Extra {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        Debug::fmt(&self.0, f)
41    }
42}
43
44impl<I: Into<sys::Extra>> From<I> for Extra {
45    fn from(inner: I) -> Self {
46        Self(inner.into())
47    }
48}
49
50impl Extra {
51    pub(crate) fn new(driver: &Driver) -> Self {
52        driver.default_extra().into()
53    }
54}
55
56impl Extra {
57    iour_only! {
58        /// Checks whether this completion reports a notification (2nd CQE returned for a zerocopy op).
59        ///
60        /// # Behaviour
61        ///
62        /// This is only supported on `io_uring` drivers, in which the driver will
63        /// check whether the `IORING_CQE_F_NOTIF` flag was set by the kernel for
64        /// the CQE. On other platforms, this will always return the
65        /// [`Unsupported`] error.
66        ///
67        /// [`Unsupported`]: io::ErrorKind::Unsupported
68        get fn is_notification(&self) -> io::Result<bool> = |extra| Ok(extra.is_notification());
69
70        /// Try to get the buffer ID associated with this operation.
71        ///
72        /// # Behavior
73        ///
74        /// This is only supported on `io_uring` drivers, in which the driver will
75        /// try to extract `buffer_id` returned by the kernel as a part of `flags`.
76        /// If the id cannot be extracted from the flag, an [`InvalidInput`]
77        /// [`io::Error`] will be returned. On other platforms, this will always
78        /// return [`Unsupported`] error.
79        ///
80        /// [`InvalidInput`]: io::ErrorKind::InvalidInput
81        /// [`Unsupported`]: io::ErrorKind::Unsupported
82        get fn buffer_id(&self) -> io::Result<u16> =
83            |extra| extra
84                .buffer_id()
85                .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Buffer id was not set"));
86
87
88        /// Get the personality for this operation.
89        ///
90        /// # Behavior
91        ///
92        /// - If the driver is not `io_uring`, return [`Unsupported`] error,
93        /// - If the personality was not set with [`set_personality`], return `Ok(None)`
94        /// - Otherwise, return `Ok(Some(personality))`
95        ///
96        /// [`Unsupported`]: io::ErrorKind::Unsupported
97        /// [`set_personality`]: Extra::set_personality
98        get fn get_personality(&self) -> io::Result<Option<u16>> = |extra| Ok(extra.get_personality());
99
100        /// Checks whether the underlying socket has more data to be read.
101        ///
102        /// # Behaviour
103        ///
104        /// This method must be used only on `IO_URING`. The driver will try to check whether
105        /// the `IORING_CQE_F_SOCK_NONEMPTY` flag was set by the kernel for the CQE.
106        /// On other platforms, this will always return the [`Unsupported`] error.
107        ///
108        /// [`Unsupported`]: io::ErrorKind::Unsupported
109        get fn sock_nonempty(&self) -> io::Result<bool> = |extra| Ok(extra.sock_nonempty());
110
111        /// Set the `IOSQE_IO_DRAIN` flag for this operation.
112        ///
113        /// This ensures that this operation won't start until all previously submitted operations complete.
114        ///
115        /// See [`io_uring_sqe_set_flags(3)`] for more details.
116        ///
117        /// [`io_uring_sqe_set_flags(3)`]: https://man7.org/linux/man-pages/man3/io_uring_sqe_set_flags.3.html
118        set fn drain(&mut self) = |extra| extra.set_drain();
119
120        /// Set the `IOSQE_IO_LINK` flag for this operation.
121        ///
122        /// This links this operation with the next one. The next operation will not start until this operation
123        /// completed successfully.
124        ///
125        /// See [`io_uring_sqe_set_flags(3)`] for more details.
126        ///
127        /// [`io_uring_sqe_set_flags(3)`]: https://man7.org/linux/man-pages/man3/io_uring_sqe_set_flags.3.html
128        set fn link(&mut self) = |extra| extra.set_link();
129
130        /// Set the `IOSQE_IO_HARDLINK` flag for this operation.
131        ///
132        /// Like link, but the next operation will execute regardless of this operation's result.
133        ///
134        /// See [`io_uring_sqe_set_flags(3)`] for more details.
135        ///
136        /// [`io_uring_sqe_set_flags(3)`]: https://man7.org/linux/man-pages/man3/io_uring_sqe_set_flags.3.html
137        set fn hardlink(&mut self) = |extra| extra.set_hardlink();
138
139        /// Set the personality for this operation.
140        ///
141        /// A personality represents a set of credentials (uid, gid, etc.) that will be used for this operation.
142        ///
143        /// The personality can be retrieved with [`Proactor::register_personality`].
144        ///
145        /// [`Proactor::register_personality`]: crate::Proactor::register_personality
146        set fn personality(&mut self, personality: u16) = |extra| extra.set_personality(personality);
147    }
148}
149
150macro_rules! iour_only {
151    {} => {};
152    {
153        $(#[$doc:meta])*
154        get fn $fn:ident(&$this:ident) -> io::Result<$ret:ty> = |$extra:ident| $body:expr;
155        $($rest:tt)*
156    } => {
157        $(#[$doc])*
158        pub fn $fn (&$this) -> io::Result<$ret> {
159            const UNSUPPORTED: &str = concat!(stringify!($fn), " is only supported on the io_uring driver");
160            #[cfg(io_uring)]
161            if let Some($extra) = $this.try_as_iour() {
162                $body
163            } else {
164                Err(io::Error::new(io::ErrorKind::Unsupported, UNSUPPORTED))
165            }
166            #[cfg(not(io_uring))]
167            Err(io::Error::new(io::ErrorKind::Unsupported, UNSUPPORTED))
168        }
169        iour_only!($($rest)*);
170    };
171    {
172        $(#[$doc:meta])*
173        set fn $val:ident(&mut $this:ident $(, $arg:ident: $arg_ty:ty)*) = |$extra:ident| $body:expr;
174        $($rest:tt)*
175    } => {
176        paste::paste! {
177            $(#[$doc])*
178            #[doc = " This is a no-op when not using `io_uring` driver."]
179            pub fn [<set_ $val>] (&mut $this $(, $arg: $arg_ty)*) {
180                #[cfg(io_uring)]
181                if let Some($extra) = $this.try_as_iour_mut() {
182                    $body
183                }
184                #[cfg(not(io_uring))]
185                {$(let _ = $arg;)*}
186            }
187
188            #[doc = concat!("Call [`set_", stringify!($val), "`] and return the modified `Extra`.")]
189            #[doc = ""]
190            #[doc = concat!("[`set_", stringify!($val), "`]: Self::set_", stringify!($val))]
191            pub fn [<with_ $val>] (mut $this $(, $arg: $arg_ty)*) -> Self {
192                $this.[<set_ $val>]($($arg),*);
193                $this
194            }
195        }
196        iour_only!($($rest)*);
197    };
198}
199
200use iour_only;
201
202use crate::Driver;