compio_driver/sys/driver/poll/
op.rs1pub use self::OpCode as PollOpCode;
2use crate::sys::prelude::*;
3
4#[non_exhaustive]
7pub enum OpType {
8 Fd(Multi<RawFd>),
10 #[cfg(aio)]
12 Aio(NonNull<libc::aiocb>),
13}
14
15#[derive(Debug)]
17#[non_exhaustive]
18pub enum Decision {
19 Completed(usize),
21 Wait(Multi<WaitArg>),
23 Blocking,
25 #[cfg(aio)]
27 Aio(AioArg),
28}
29
30#[derive(Debug, Clone, Copy)]
32pub struct WaitArg {
33 pub fd: RawFd,
35 pub interest: Interest,
37}
38
39pub unsafe trait OpCode {
48 type Control: Default;
51
52 unsafe fn init(&mut self, _: &mut Self::Control) {}
59
60 fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision>;
63
64 fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
66 None
67 }
68
69 fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>>;
73
74 unsafe fn set_result(
82 &mut self,
83 _: &mut Self::Control,
84 _: &io::Result<usize>,
85 _: &crate::Extra,
86 ) {
87 }
88}
89
90pub(crate) trait Carry {
91 fn pre_submit(&mut self) -> io::Result<Decision>;
92 fn op_type(&mut self) -> Option<OpType>;
93 fn operate(&mut self) -> Poll<io::Result<usize>>;
94 unsafe fn set_result(&mut self, _: &io::Result<usize>, _: &crate::Extra);
95}
96
97impl OpType {
98 pub fn fd(fd: RawFd) -> Self {
100 Self::Fd(Multi::from_buf([fd]))
101 }
102
103 pub fn multi_fd<I: IntoIterator<Item = RawFd>>(fds: I) -> Self {
105 Self::Fd(Multi::from_iter(fds))
106 }
107}
108
109impl<T: crate::OpCode> Carry for Carrier<T> {
110 fn pre_submit(&mut self) -> io::Result<Decision> {
111 let (op, control) = self.as_poll();
112 op.pre_submit(control)
113 }
114
115 fn op_type(&mut self) -> Option<OpType> {
116 let (op, control) = self.as_poll();
117 op.op_type(control)
118 }
119
120 fn operate(&mut self) -> Poll<io::Result<usize>> {
121 let (op, control) = self.as_poll();
122 op.operate(control)
123 }
124
125 unsafe fn set_result(&mut self, res: &io::Result<usize>, extra: &crate::Extra) {
126 let (op, control) = self.as_poll();
127 unsafe { OpCode::set_result(op, control, res, extra) }
128 }
129}
130
131impl Decision {
132 pub fn wait_for(fd: RawFd, interest: Interest) -> Self {
134 Self::Wait(Multi::from_buf([WaitArg { fd, interest }]))
135 }
136
137 pub fn wait_for_many<I: IntoIterator<Item = WaitArg>>(args: I) -> Self {
139 Self::Wait(Multi::from_iter(args))
140 }
141
142 pub fn wait_readable(fd: RawFd) -> Self {
144 Self::wait_for(fd, Interest::Readable)
145 }
146
147 pub fn wait_writable(fd: RawFd) -> Self {
149 Self::wait_for(fd, Interest::Writable)
150 }
151
152 #[cfg(aio)]
155 pub fn aio(
156 cb: &mut libc::aiocb,
157 submit: unsafe extern "C" fn(*mut libc::aiocb) -> i32,
158 ) -> Self {
159 Self::Aio(AioArg {
160 aiocbp: NonNull::from(cb),
161 submit,
162 })
163 }
164}
165
166impl WaitArg {
167 pub fn readable(fd: RawFd) -> Self {
169 Self {
170 fd,
171 interest: Interest::Readable,
172 }
173 }
174
175 pub fn writable(fd: RawFd) -> Self {
177 Self {
178 fd,
179 interest: Interest::Writable,
180 }
181 }
182}