Skip to main content

compio_driver/sys/op/asyncify/
mod.rs

1use crate::sys::prelude::*;
2
3#[cfg(io_uring)]
4mod iour;
5
6#[cfg(polling)]
7mod poll;
8
9#[cfg(windows)]
10mod iocp;
11
12#[cfg(stub)]
13mod stub;
14
15/// Spawn a blocking function in the thread pool.
16pub struct Asyncify<F, D> {
17    pub(crate) f: Option<F>,
18    pub(crate) data: Option<D>,
19}
20
21impl<F, D> Asyncify<F, D> {
22    /// Create [`Asyncify`].
23    pub fn new(f: F) -> Self {
24        Self {
25            f: Some(f),
26            data: None,
27        }
28    }
29}
30
31impl<F, D> IntoInner for Asyncify<F, D> {
32    type Inner = D;
33
34    fn into_inner(mut self) -> Self::Inner {
35        self.data.take().expect("the data should not be None")
36    }
37}
38
39/// Spawn a blocking function with a file descriptor in the thread pool.
40pub struct AsyncifyFd<S, F, D> {
41    pub(crate) fd: SharedFd<S>,
42    pub(crate) f: Option<F>,
43    pub(crate) data: Option<D>,
44}
45
46impl<S, F, D> AsyncifyFd<S, F, D> {
47    /// Create [`AsyncifyFd`].
48    pub fn new(fd: SharedFd<S>, f: F) -> Self {
49        Self {
50            fd,
51            f: Some(f),
52            data: None,
53        }
54    }
55}
56
57impl<S, F, D> IntoInner for AsyncifyFd<S, F, D> {
58    type Inner = D;
59
60    fn into_inner(mut self) -> Self::Inner {
61        self.data.take().expect("the data should not be None")
62    }
63}
64
65/// Spawn a blocking function with two file descriptors in the thread pool.
66pub struct AsyncifyFd2<S1, S2, F, D> {
67    pub(crate) fd1: SharedFd<S1>,
68    pub(crate) fd2: SharedFd<S2>,
69    pub(crate) f: Option<F>,
70    pub(crate) data: Option<D>,
71}
72
73impl<S1, S2, F, D> AsyncifyFd2<S1, S2, F, D> {
74    /// Create [`AsyncifyFd2`].
75    pub fn new(fd1: SharedFd<S1>, fd2: SharedFd<S2>, f: F) -> Self {
76        Self {
77            fd1,
78            fd2,
79            f: Some(f),
80            data: None,
81        }
82    }
83}
84
85impl<S1, S2, F, D> IntoInner for AsyncifyFd2<S1, S2, F, D> {
86    type Inner = D;
87
88    fn into_inner(mut self) -> Self::Inner {
89        self.data.take().expect("the data should not be None")
90    }
91}