Skip to main content

compio_driver/sys/op/general/
poll.rs

1use rustix::io::{pread, preadv, pwrite, pwritev, read, readv, write, writev};
2
3use crate::{Decision, OpType, PollOpCode as OpCode, sys::op::*};
4
5unsafe impl<T: IoBufMut, S: AsFd> OpCode for ReadAt<T, S> {
6    type Control = AioControl;
7
8    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
9        ctrl.init_mut(&self.fd, &mut self.buffer, self.offset);
10    }
11
12    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
13        ctrl.decide_read()
14    }
15
16    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
17        ctrl.op_type()
18    }
19
20    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
21        poll_io(|| {
22            pread(self.fd.as_fd(), self.buffer.as_uninit(), self.offset).map(|(init, _)| init.len())
23        })
24    }
25}
26
27unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for ReadVectoredAt<T, S> {
28    type Control = AioControl<VectoredControl>;
29
30    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
31        ctrl.init_vec_mut(&self.fd, &mut self.buffer, self.offset);
32    }
33
34    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
35        ctrl.decide_read()
36    }
37
38    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
39        ctrl.op_type()
40    }
41
42    fn operate(&mut self, ctrl: &mut Self::Control) -> Poll<io::Result<usize>> {
43        poll_io(|| preadv(self.fd.as_fd(), ctrl.io_slices_mut(), self.offset))
44    }
45}
46
47unsafe impl<T: IoBuf, S: AsFd> OpCode for WriteAt<T, S> {
48    type Control = AioControl;
49
50    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
51        ctrl.init(&self.fd, &self.buffer, self.offset);
52    }
53
54    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
55        ctrl.decide_write()
56    }
57
58    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
59        ctrl.op_type()
60    }
61
62    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
63        poll_io(|| pwrite(self.fd.as_fd(), self.buffer.as_init(), self.offset))
64    }
65}
66
67unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for WriteVectoredAt<T, S> {
68    type Control = AioControl<VectoredControl>;
69
70    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
71        ctrl.init_vec(&self.fd, &self.buffer, self.offset);
72    }
73
74    fn pre_submit(&mut self, ctrl: &mut Self::Control) -> io::Result<Decision> {
75        ctrl.decide_write()
76    }
77
78    fn op_type(&mut self, ctrl: &mut Self::Control) -> Option<crate::OpType> {
79        ctrl.op_type()
80    }
81
82    fn operate(&mut self, ctrl: &mut Self::Control) -> Poll<io::Result<usize>> {
83        poll_io(|| pwritev(self.fd.as_fd(), ctrl.io_slices(), self.offset))
84    }
85}
86
87unsafe impl<T: IoBufMut, S: AsFd> OpCode for Read<T, S> {
88    type Control = ();
89
90    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
91        Ok(Decision::wait_readable(self.fd.as_fd().as_raw_fd()))
92    }
93
94    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
95        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
96    }
97
98    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
99        poll_io(|| read(self.fd.as_fd(), self.buffer.as_uninit()).map(|(init, _)| init.len()))
100    }
101}
102
103unsafe impl<T: IoVectoredBufMut, S: AsFd> OpCode for ReadVectored<T, S> {
104    type Control = VectoredControl;
105
106    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
107        ctrl.slices = self.buffer.sys_slices();
108    }
109
110    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
111        Ok(Decision::wait_readable(self.fd.as_fd().as_raw_fd()))
112    }
113
114    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
115        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
116    }
117
118    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
119        poll_io(|| readv(self.fd.as_fd(), control.io_slices_mut()))
120    }
121}
122
123unsafe impl<T: IoBuf, S: AsFd> OpCode for Write<T, S> {
124    type Control = ();
125
126    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
127        Ok(Decision::wait_writable(self.fd.as_fd().as_raw_fd()))
128    }
129
130    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
131        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
132    }
133
134    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
135        poll_io(|| write(self.fd.as_fd(), self.buffer.as_init()))
136    }
137}
138
139unsafe impl<T: IoVectoredBuf, S: AsFd> OpCode for WriteVectored<T, S> {
140    type Control = VectoredControl;
141
142    unsafe fn init(&mut self, ctrl: &mut Self::Control) {
143        ctrl.slices = self.buffer.sys_slices();
144    }
145
146    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
147        Ok(Decision::wait_writable(self.fd.as_fd().as_raw_fd()))
148    }
149
150    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
151        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
152    }
153
154    fn operate(&mut self, control: &mut Self::Control) -> Poll<io::Result<usize>> {
155        poll_io(|| writev(self.fd.as_fd(), control.io_slices()))
156    }
157}
158
159unsafe impl<S: AsFd> OpCode for PollOnce<S> {
160    type Control = ();
161
162    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
163        Ok(Decision::wait_for(
164            self.fd.as_fd().as_raw_fd(),
165            self.interest,
166        ))
167    }
168
169    fn op_type(&mut self, _: &mut Self::Control) -> Option<OpType> {
170        Some(OpType::fd(self.fd.as_fd().as_raw_fd()))
171    }
172
173    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
174        Poll::Ready(Ok(0))
175    }
176}
177
178unsafe impl OpCode for Pipe {
179    type Control = ();
180
181    fn pre_submit(&mut self, _: &mut Self::Control) -> io::Result<Decision> {
182        Ok(Decision::Blocking)
183    }
184
185    fn operate(&mut self, _: &mut Self::Control) -> Poll<io::Result<usize>> {
186        Poll::Ready(self.call())
187    }
188}