-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathfallback.rs
More file actions
282 lines (235 loc) · 7.04 KB
/
fallback.rs
File metadata and controls
282 lines (235 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::io;
use compio_buf::{IntoInner, IoBuf, IoBufMut, SetLen};
use rustix::net::RecvFlags;
use socket2::SockAddr;
use crate::{
AsFd, BufferPool, BufferRef, PollFirst,
op::{RecvMsg, TakeBuffer},
sys::op::{Read, ReadAt, Recv, RecvFrom},
};
/// Read a file at specified position into managed buffer.
pub struct ReadManagedAt<S> {
pub(crate) op: ReadAt<BufferRef, S>,
}
impl<S> ReadManagedAt<S> {
/// Create [`ReadManagedAt`].
pub fn new(fd: S, offset: u64, pool: &BufferPool, len: usize) -> io::Result<Self> {
Ok(Self {
op: ReadAt::new(fd, offset, pool.pop()?.with_capacity(len)),
})
}
}
impl<S> TakeBuffer for ReadManagedAt<S> {
type Buffer = BufferRef;
fn take_buffer(self) -> Option<BufferRef> {
Some(self.op.into_inner())
}
}
/// Read a file into managed buffer.
pub struct ReadManaged<S> {
pub(crate) op: Read<BufferRef, S>,
}
impl<S> ReadManaged<S> {
/// Create [`ReadManaged`].
pub fn new(fd: S, pool: &BufferPool, len: usize) -> io::Result<Self> {
Ok(Self {
op: Read::new(fd, pool.pop()?.with_capacity(len)),
})
}
}
impl<S> TakeBuffer for ReadManaged<S> {
type Buffer = BufferRef;
fn take_buffer(self) -> Option<BufferRef> {
Some(self.op.into_inner())
}
}
/// Receive data from remote into managed buffer.
///
/// It is only used for socket operations. If you want to read from a pipe,
/// use [`ReadManaged`].
pub struct RecvManaged<S> {
pub(crate) op: Recv<BufferRef, S>,
}
impl<S> RecvManaged<S> {
/// Create [`RecvManaged`].
pub fn new(fd: S, pool: &BufferPool, len: usize, flags: RecvFlags) -> io::Result<Self> {
Ok(Self {
op: Recv::new(fd, pool.pop()?.with_capacity(len), flags),
})
}
}
impl<S> PollFirst for RecvManaged<S> {
fn poll_first(&mut self) {
self.op.poll_first();
}
}
impl<S> TakeBuffer for RecvManaged<S> {
type Buffer = BufferRef;
fn take_buffer(self) -> Option<BufferRef> {
Some(self.op.into_inner())
}
}
/// Receive data and source address into managed buffer.
pub struct RecvFromManaged<S: AsFd> {
pub(crate) op: RecvFrom<BufferRef, S>,
}
impl<S: AsFd> RecvFromManaged<S> {
/// Create [`RecvFromManaged`].
pub fn new(fd: S, pool: &BufferPool, len: usize, flags: RecvFlags) -> io::Result<Self> {
Ok(Self {
op: RecvFrom::new(fd, pool.pop()?.with_capacity(len), flags),
})
}
}
impl<S: AsFd> PollFirst for RecvFromManaged<S> {
fn poll_first(&mut self) {
self.op.poll_first();
}
}
impl<S: AsFd> TakeBuffer for RecvFromManaged<S> {
type Buffer = (BufferRef, Option<SockAddr>);
fn take_buffer(self) -> Option<Self::Buffer> {
Some(self.op.into_inner())
}
}
/// Receive data into managed buffer, and ancillary data into control buffer.
pub struct RecvMsgManaged<C: IoBufMut, S: AsFd> {
pub(crate) op: RecvMsg<[BufferRef; 1], C, S>,
}
impl<C: IoBufMut, S: AsFd> RecvMsgManaged<C, S> {
/// Create [`RecvMsgManaged`].
pub fn new(
fd: S,
pool: &BufferPool,
len: usize,
control: C,
flags: RecvFlags,
) -> io::Result<Self> {
Ok(Self {
op: RecvMsg::new(fd, [pool.pop()?.with_capacity(len)], control, flags),
})
}
}
impl<C: IoBufMut, S: AsFd> PollFirst for RecvMsgManaged<C, S> {
fn poll_first(&mut self) {
self.op.poll_first();
}
}
impl<C: IoBufMut, S: AsFd> TakeBuffer for RecvMsgManaged<C, S> {
type Buffer = ((BufferRef, C), Option<SockAddr>, usize);
fn take_buffer(self) -> Option<Self::Buffer> {
let (([buf], control), addr, len) = self.op.into_inner();
Some(((buf, control), addr, len))
}
}
/// Read a file at specified position into multiple managed buffers.
pub type ReadMultiAt<S> = ReadManagedAt<S>;
/// Read a file into multiple managed buffers.
pub type ReadMulti<S> = ReadManaged<S>;
/// Receive data from remote into multiple managed buffers.
pub type RecvMulti<S> = RecvManaged<S>;
/// Result of [`RecvFromMulti`].
pub struct RecvFromMultiResult {
buffer: BufferRef,
addr: Option<SockAddr>,
}
impl RecvFromMultiResult {
#[doc(hidden)]
pub unsafe fn new(_: BufferRef) -> Self {
unreachable!("should not be called directly")
}
/// Get the payload data.
pub fn data(&self) -> &[u8] {
self.buffer.as_init()
}
/// Get the source address if applicable.
pub fn addr(&self) -> Option<SockAddr> {
self.addr.clone()
}
}
impl IntoInner for RecvFromMultiResult {
type Inner = BufferRef;
fn into_inner(self) -> Self::Inner {
self.buffer
}
}
/// Receive data and source address multi times into multiple managed buffers.
pub struct RecvFromMulti<S: AsFd> {
pub(crate) op: RecvFromManaged<S>,
pub(crate) len: usize,
}
impl<S: AsFd> RecvFromMulti<S> {
/// Create [`RecvFromMulti`].
pub fn new(fd: S, pool: &BufferPool, flags: RecvFlags) -> io::Result<Self> {
Ok(Self {
op: RecvFromManaged::new(fd, pool, 0, flags)?,
len: 0,
})
}
}
impl<S: AsFd> TakeBuffer for RecvFromMulti<S> {
type Buffer = RecvFromMultiResult;
fn take_buffer(self) -> Option<Self::Buffer> {
let (mut buffer, addr) = self.op.take_buffer()?;
unsafe { buffer.advance_to(self.len) };
Some(RecvFromMultiResult { buffer, addr })
}
}
/// Result of [`RecvMsgMulti`].
pub struct RecvMsgMultiResult {
buffer: BufferRef,
control: BufferRef,
addr: Option<SockAddr>,
}
impl RecvMsgMultiResult {
#[doc(hidden)]
pub unsafe fn new(_: BufferRef, _: usize) -> Self {
unreachable!("should not be called directly")
}
/// Get the payload data.
pub fn data(&self) -> &[u8] {
self.buffer.as_init()
}
/// Get the source address if applicable.
pub fn addr(&self) -> Option<SockAddr> {
self.addr.clone()
}
/// Get the ancillary data.
pub fn ancillary(&self) -> &[u8] {
self.control.as_init()
}
}
impl IntoInner for RecvMsgMultiResult {
type Inner = BufferRef;
fn into_inner(self) -> Self::Inner {
self.buffer
}
}
/// Receive data, ancillary data and source address multi times into multiple
/// managed buffers.
pub struct RecvMsgMulti<S: AsFd> {
pub(crate) op: RecvMsgManaged<BufferRef, S>,
pub(crate) len: usize,
}
impl<S: AsFd> RecvMsgMulti<S> {
/// Create [`RecvMsgMulti`].
pub fn new(fd: S, pool: &BufferPool, control_len: usize, flags: RecvFlags) -> io::Result<Self> {
Ok(Self {
op: RecvMsgManaged::new(fd, pool, 0, pool.pop()?.with_capacity(control_len), flags)?,
len: 0,
})
}
}
impl<S: AsFd> TakeBuffer for RecvMsgMulti<S> {
type Buffer = RecvMsgMultiResult;
fn take_buffer(self) -> Option<Self::Buffer> {
let ((mut buffer, mut control), addr, control_len) = self.op.take_buffer()?;
unsafe { buffer.advance_to(self.len) };
unsafe { control.advance_to(control_len) };
Some(RecvMsgMultiResult {
buffer,
control,
addr,
})
}
}