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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Reading and writing on any handle, pipes, and the standard handles.
//!
//! The functions take anything that holds a handle ([`AsHandle`]): an
//! [`OwnedHandle`] by reference, or a [`BorrowedHandle`]. They work on
//! files, pipes, terminals and every other object the system reads and
//! writes through a handle. Sockets have their own functions in
//! [`net`](crate::net).
//!
//! Each function is one system call. A read or a write may transfer fewer
//! bytes than asked; a call interrupted by a signal fails with
//! [`ErrorKind::Interrupted`] and may be
//! retried.
//!
//! On Windows a handle may be open for overlapped (asynchronous) operation,
//! when it was not opened by oscall: a file opened with
//! `FILE_FLAG_OVERLAPPED`, the pipe of a child process. The functions here
//! are synchronous on such a handle too: each returns when its operation is
//! complete, as `std`'s do (`NtReadFile`, `NtWriteFile`, and a wait). A
//! file opened that way has no position, so that only [`read_at`] and
//! [`write_at`] work on it; [`read()`] and [`write()`] fail with
//! [`ErrorKind::InvalidInput`].
//!
//! On WebAssembly without WASI every function fails with
//! [`ErrorKind::Unsupported`]. What WASI
//! itself lacks is said in each function.
//!
//! # I/O that completes later
//!
//! Everything above is synchronous: a call returns when its operation is
//! done. Starting an operation and being told later that it completed is
//! something each system does in its own way, and for its own things:
//! there is no portable form of it (`docs/plan.md` says why), and three
//! extension modules.
//!
//! - `io::linux`: io_uring. A `Ring` of submissions and completions shared
//! with the kernel (with a kernel thread that submits, if asked), typed
//! entries for the operations on files, sockets, timers, futexes and
//! children, the multishot ones included, groups of buffers the kernel
//! picks from, and the raw entry for what is left.
//! - `io::windows`: handles opened for overlapped operation (files, with
//! the system's cache or without it, pipes, named pipes), a
//! `CompletionPort`, and an `Operation` that owns the memory of an
//! operation in flight: on files, pipes, sockets and directories.
//! - `io::macos`: POSIX asynchronous I/O, which is for files, and for 16
//! operations of a process at a time. An `Operation` that owns the
//! memory of a read, a write or a flush in flight, several started at
//! once, a wait for one of several, and a signal that tells of them.
//!
//! What is portable is waiting until a socket can be read or written
//! without waiting, and then doing it: [`event`](crate::event).
use fmt;
use PhantomData;
use crate::;
/// Reads from `handle` into `buf`, at the handle's current position for
/// those that have one, and returns the number of bytes read.
///
/// `Ok(0)` means the end of the file, or of a pipe whose write end is
/// closed (or that `buf` is empty).
/// Writes `buf` to `handle`, at the handle's current position for those that
/// have one (at the end of the file for one opened for appending), and
/// returns the number of bytes written, which may be less than `buf.len()`.
///
/// Writing to a pipe whose read end is closed fails with
/// [`ErrorKind::BrokenPipe`]. On Linux and
/// macOS the system also raises `SIGPIPE`, which kills the process unless
/// it is ignored, as it is in every program with Rust's `main`.
/// Reads from `handle` at `offset` bytes from the start of the file, and
/// returns the number of bytes read: `Ok(0)` at or after the end.
///
/// The handle must be seekable. On Linux, macOS and WASI the handle's
/// position is neither used nor changed (`pread`). **On Windows the
/// position moves** to the end of what was read, as there is no way to
/// avoid it on a synchronous handle: do not mix positional and sequential
/// I/O on one handle, nor share a handle between threads that do.
/// Writes `buf` to `handle` at `offset` bytes from the start of the file,
/// and returns the number of bytes written.
///
/// Writing past the end extends the file, and the gap reads as zeroes.
///
/// The handle must be seekable. As for [`read_at`], the handle's position
/// is left alone, except on Windows where it moves to the end of what was
/// written. On a handle opened for appending, Linux appends the data
/// whatever `offset` is; macOS and Windows honour `offset`. Do not rely on
/// either.
/// Reads from `handle` into several buffers, filling each one before using
/// the next, and returns the total number of bytes read.
///
/// One system call (`readv`) on Linux, macOS and WASI, where at most 1024
/// buffers are used. Windows has no such call for files and pipes: only the
/// first buffer that is not empty is read into, which is what a short read
/// looks like. Some WASI hosts (wasmtime) do the same.
/// Writes the content of several buffers to `handle`, in order, and returns
/// the total number of bytes written.
///
/// One system call (`writev`) on Linux, macOS and WASI, where at most 1024
/// buffers are used. Windows has no such call for files and pipes: only the
/// first buffer that is not empty is written, which is what a short write
/// looks like. Some WASI hosts (wasmtime) do the same.
/// A position to [`seek`] to.
/// Moves the position of `handle` and returns the new position, in bytes
/// from the start of the file.
///
/// `seek(handle, SeekFrom::Current(0))` reads the position. A position
/// after the end is allowed (writing there extends the file); one before
/// the start fails with [`ErrorKind::InvalidInput`]. A pipe or a terminal
/// has no position: Linux, macOS and WASI fail with
/// [`ErrorKind::NotSeekable`]; Windows may succeed and do nothing.
/// Creates a pipe and returns its read end and its write end, in that order.
///
/// What is written to the second handle can be read from the first. Both
/// are blocking and private to the process (close-on-exec, not
/// inheritable): see [`set_inheritable`] to give one to a child process.
///
/// `pipe2` on Linux, `CreatePipe` on Windows. macOS has no call that
/// creates the pipe close-on-exec at once: the flag is set right after
/// `pipe`, and a process spawned by another thread in between inherits the
/// pipe.
///
/// WASI has no pipes: fails with [`ErrorKind::Unsupported`].
/// Creates a second handle to the object `handle` refers to.
///
/// The two handles share everything: the position in a file, its open mode,
/// its locks. Closing one leaves the other open. The new handle is private
/// to the process, whatever `handle` is.
///
/// `fcntl(F_DUPFD_CLOEXEC)` on Linux and macOS, `DuplicateHandle` on
/// Windows.
///
/// WASI cannot duplicate a descriptor: fails with
/// [`ErrorKind::Unsupported`].
/// The standard input of the process.
///
/// `None` when the process has none, which only Windows can tell (a
/// graphical program, a detached process) and WebAssembly without WASI.
/// Elsewhere it is always descriptor 0, which the parent process may have
/// left closed: using it then fails with [`ErrorKind::BadHandle`].
///
/// The handle is not owned: it is never closed by oscall.
/// The standard output of the process. See [`stdin`].
/// The standard error output of the process. See [`stdin`].
/// Whether `handle` is a terminal (a console on Windows).
///
/// `false` for anything else, including a handle that is not valid. On
/// Windows, the pseudo-terminals of MSYS and Cygwin are pipes and are not
/// recognised.
/// Sets whether child processes inherit `handle`.
///
/// Every handle oscall creates starts out private. An inheritable handle
/// stays open across `exec` on Linux and macOS (this clears `FD_CLOEXEC`),
/// and is given to the child processes created with handle inheritance on
/// Windows (`HANDLE_FLAG_INHERIT`).
///
/// WASI has no processes: fails with [`ErrorKind::Unsupported`].
/// Whether child processes inherit `handle`. See [`set_inheritable`].
///
/// WASI has no processes: fails with [`ErrorKind::Unsupported`].
/// Sets whether [`read`] and [`write()`] on a pipe return at once instead of
/// waiting: for data to read, or for room to write.
///
/// On a non-blocking pipe:
///
/// - reading an empty pipe fails with [`ErrorKind::WouldBlock`], unless
/// its write end is closed, which is the end as ever: `Ok(0)`;
/// - writing transfers what fits and returns that count, and fails with
/// [`ErrorKind::WouldBlock`] when nothing fits.
///
/// The mode belongs to the end of the pipe, not to the handle: its
/// [duplicates](duplicate) share it, and so does a process that inherited
/// the handle. Each end of a [`pipe`] has its own, and both start blocking.
///
/// `fcntl(F_SETFL)` with `O_NONBLOCK` on Linux and macOS, where it works on
/// the other handles that can make a call wait as well: FIFOs, terminals
/// and devices (and has no effect on files). `SetNamedPipeHandleState`
/// with `PIPE_NOWAIT` on Windows, where it only works on pipes, anonymous
/// or named, and fails on anything else. Sockets have
/// [`net::set_nonblocking`](crate::net::set_nonblocking).
///
/// On Windows the system reports a non-blocking read that would wait with
/// the error it uses for a broken pipe: oscall makes it
/// [`ErrorKind::WouldBlock`] (the code of `WSAEWOULDBLOCK`). And a pipe
/// there takes a non-blocking write whole or not at all, which it reports
/// as a write of nothing: oscall then asks how much room the pipe has
/// (`NtQueryInformationFile`) and writes that much, or tries shorter and
/// shorter writes when the system reports none. A write is one system call
/// when the data fits, a few when part of it does, and about as many as
/// the length has bits before it reports a pipe that is full.
///
/// WASI has no pipes of its own, but its standard handles may be some:
/// `fd_fdstat_set_flags`, which hosts honour as they can.
/// A buffer to write from, for [`write_vectored`].
///
/// It is a `&[u8]` with the layout the system wants for vectored I/O:
/// `struct iovec` on Linux, macOS and WASI, `WSABUF` on Windows. A slice of
/// `IoSlice` can therefore be handed to the system as it is.
///
/// On Windows a buffer cannot be longer than `u32::MAX` bytes: a longer
/// slice is cut to that length, which a write reports as a short write.
;
// SAFETY: an `IoSlice` is a `&[u8]`.
unsafe
// SAFETY: as above.
unsafe
/// A buffer to read into, for [`read_vectored`].
///
/// It is a `&mut [u8]` with the layout the system wants for vectored I/O:
/// see [`IoSlice`], including the limit on its length on Windows.
;
// SAFETY: an `IoSliceMut` is a `&mut [u8]`.
unsafe
// SAFETY: as above.
unsafe
// ---------------------------------------------------------------------------
// Extensions
// ---------------------------------------------------------------------------
// Their documentation is in the files: a module each.