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
//! `OwnedReadable` and `OwnedWriteable`.

use crate::grip::{AsRawGrip, FromRawGrip, OwnedGrip};
use crate::raw::{RawReadable, RawWriteable};
#[cfg(not(windows))]
use io_lifetimes::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
#[cfg(all(doc, not(windows)))]
use std::net::TcpStream;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use {
    crate::os::windows::{
        AsHandleOrSocket, AsRawHandleOrSocket, BorrowedHandleOrSocket, FromHandleOrSocket,
        FromRawHandleOrSocket, IntoHandleOrSocket, IntoRawHandleOrSocket, OwnedHandleOrSocket,
    },
    io_lifetimes::{FromHandle, OwnedHandle},
    std::os::windows::io::{FromRawHandle, IntoRawHandle},
};

/// An owning I/O handle that implements [`Read`].
///
/// This doesn't implement `Into*` or `From*` traits.
///
/// # Platform-specific behavior
///
/// On Posix-ish platforms, this reads from the handle as if it were a
/// [`File`]. On Windows, this reads from a file-like handle as if it were a
/// [`File`], and from a socket-like handle as if it were a [`TcpStream`].
#[repr(transparent)]
pub struct OwnedReadable(RawReadable);

/// An owning I/O handle that implements [`Write`].
///
/// This doesn't implement `Into*` or `From*` traits.
///
/// # Platform-specific behavior
///
/// On Posix-ish platforms, this writes to the handle as if it were a
/// [`File`]. On Windows, this writes to a file-like handle as if it were a
/// [`File`], and to a socket-like handle as if it were a [`TcpStream`].
#[repr(transparent)]
pub struct OwnedWriteable(RawWriteable);

/// `OwnedReadable` owns its handle.
#[cfg(not(windows))]
impl AsFd for OwnedReadable {
    #[inline]
    fn as_fd(&self) -> BorrowedFd<'_> {
        unsafe { BorrowedFd::borrow_raw(self.0.as_raw_fd()) }
    }
}

/// `OwnedReadable` owns its handle.
#[cfg(not(windows))]
impl IntoFd for OwnedReadable {
    #[inline]
    fn into_fd(self) -> OwnedFd {
        unsafe { OwnedFd::from_raw_fd(self.0.into_raw_fd()) }
    }
}

/// `OwnedReadable` owns its handle.
#[cfg(not(windows))]
impl FromFd for OwnedReadable {
    #[inline]
    fn from_fd(fd: OwnedFd) -> Self {
        unsafe { Self(RawReadable::from_raw_fd(fd.into_raw_fd())) }
    }
}

/// `OwnedWriteable` owns its handle.
#[cfg(not(windows))]
impl AsFd for OwnedWriteable {
    #[inline]
    fn as_fd(&self) -> BorrowedFd<'_> {
        unsafe { BorrowedFd::borrow_raw(self.0.as_raw_fd()) }
    }
}

/// `OwnedWriteable` owns its handle.
#[cfg(not(windows))]
impl IntoFd for OwnedWriteable {
    #[inline]
    fn into_fd(self) -> OwnedFd {
        unsafe { OwnedFd::from_raw_fd(self.0.as_raw_fd()) }
    }
}

/// `OwnedWriteable` owns its handle.
#[cfg(not(windows))]
impl FromFd for OwnedWriteable {
    #[inline]
    fn from_fd(fd: OwnedFd) -> Self {
        unsafe { Self(RawWriteable::from_raw_fd(fd.into_raw_fd())) }
    }
}

// Windows implementations.

/// `OwnedReadable` owns its handle.
#[cfg(windows)]
impl AsHandleOrSocket for OwnedReadable {
    #[inline]
    fn as_handle_or_socket(&self) -> BorrowedHandleOrSocket<'_> {
        unsafe { BorrowedHandleOrSocket::borrow_raw(self.0.as_raw_handle_or_socket()) }
    }
}

/// `OwnedReadable` owns its handle.
#[cfg(windows)]
impl IntoHandleOrSocket for OwnedReadable {
    #[inline]
    fn into_handle_or_socket(self) -> OwnedHandleOrSocket {
        unsafe {
            OwnedHandleOrSocket::from_raw_handle_or_socket(self.0.into_raw_handle_or_socket())
        }
    }
}

/// `OwnedReadable` owns its handle.
#[cfg(windows)]
impl FromHandleOrSocket for OwnedReadable {
    #[inline]
    fn from_handle_or_socket(handle_or_socket: OwnedHandleOrSocket) -> Self {
        unsafe {
            Self(RawReadable::from_raw_handle_or_socket(
                handle_or_socket.into_raw_handle_or_socket(),
            ))
        }
    }
}

/// `OwnedReadable` owns its handle.
#[cfg(windows)]
impl FromHandle for OwnedReadable {
    #[inline]
    fn from_handle(handle: OwnedHandle) -> Self {
        unsafe { Self(RawReadable::from_raw_handle(handle.into_raw_handle())) }
    }
}

/// `OwnedWriteable` owns its handle.
#[cfg(windows)]
impl AsHandleOrSocket for OwnedWriteable {
    #[inline]
    fn as_handle_or_socket(&self) -> BorrowedHandleOrSocket<'_> {
        unsafe { BorrowedHandleOrSocket::borrow_raw(self.0.as_raw_handle_or_socket()) }
    }
}

/// `OwnedWriteable` owns its handle.
#[cfg(windows)]
impl IntoHandleOrSocket for OwnedWriteable {
    #[inline]
    fn into_handle_or_socket(self) -> OwnedHandleOrSocket {
        unsafe {
            OwnedHandleOrSocket::from_raw_handle_or_socket(self.0.into_raw_handle_or_socket())
        }
    }
}

/// `OwnedWriteable` owns its handle.
#[cfg(windows)]
impl FromHandleOrSocket for OwnedWriteable {
    #[inline]
    fn from_handle_or_socket(handle_or_socket: OwnedHandleOrSocket) -> Self {
        unsafe {
            Self(RawWriteable::from_raw_handle_or_socket(
                handle_or_socket.into_raw_handle_or_socket(),
            ))
        }
    }
}

/// `OwnedWriteable` owns its handle.
#[cfg(windows)]
impl FromHandle for OwnedWriteable {
    #[inline]
    fn from_handle(handle: OwnedHandle) -> Self {
        unsafe { Self(RawWriteable::from_raw_handle(handle.into_raw_handle())) }
    }
}

#[cfg(not(windows))]
impl Read for OwnedReadable {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.read(buf)
    }

    #[inline]
    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
        self.0.read_vectored(bufs)
    }

    #[cfg(can_vector)]
    #[inline]
    fn is_read_vectored(&self) -> bool {
        self.0.is_read_vectored()
    }

    #[inline]
    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
        self.0.read_to_end(buf)
    }

    #[inline]
    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
        self.0.read_to_string(buf)
    }

    #[inline]
    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
        self.0.read_exact(buf)
    }
}

#[cfg(windows)]
impl Read for OwnedReadable {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.read(buf)
    }

    #[inline]
    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
        self.0.read_vectored(bufs)
    }

    #[cfg(can_vector)]
    #[inline]
    fn is_read_vectored(&self) -> bool {
        self.0.is_read_vectored()
    }

    #[inline]
    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
        self.0.read_to_end(buf)
    }

    #[inline]
    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
        self.0.read_to_string(buf)
    }

    #[inline]
    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
        self.0.read_exact(buf)
    }
}

#[cfg(not(windows))]
impl Write for OwnedWriteable {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.0.write(buf)
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        self.0.flush()
    }

    #[inline]
    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
        self.0.write_vectored(bufs)
    }

    #[cfg(can_vector)]
    #[inline]
    fn is_write_vectored(&self) -> bool {
        self.0.is_write_vectored()
    }

    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        self.0.write_all(buf)
    }

    #[cfg(write_all_vectored)]
    #[inline]
    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
        self.0.write_all_vectored(bufs)
    }

    #[inline]
    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
        self.0.write_fmt(fmt)
    }
}

#[cfg(windows)]
impl Write for OwnedWriteable {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.0.write(buf)
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        self.0.flush()
    }

    #[inline]
    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
        self.0.write_vectored(bufs)
    }

    #[cfg(can_vector)]
    #[inline]
    fn is_write_vectored(&self) -> bool {
        self.0.is_write_vectored()
    }

    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        self.0.write_all(buf)
    }

    #[cfg(write_all_vectored)]
    #[inline]
    fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
        self.0.write_all_vectored(bufs)
    }

    #[inline]
    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
        self.0.write_fmt(fmt)
    }
}

impl Drop for OwnedReadable {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            let _owned = OwnedGrip::from_raw_grip(self.0.as_raw_grip());
        }
    }
}

impl Drop for OwnedWriteable {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            let _owned = OwnedGrip::from_raw_grip(self.0.as_raw_grip());
        }
    }
}

#[cfg(not(windows))]
impl fmt::Debug for OwnedReadable {
    #[allow(clippy::missing_inline_in_public_items)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Just print the raw fd number.
        f.debug_struct("OwnedReadable")
            .field("fd", &self.0)
            .finish()
    }
}

#[cfg(windows)]
impl fmt::Debug for OwnedReadable {
    #[allow(clippy::missing_inline_in_public_items)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Just print the raw handle or socket.
        f.debug_struct("OwnedReadable")
            .field("handle_or_socket", &self.0)
            .finish()
    }
}

#[cfg(not(windows))]
impl fmt::Debug for OwnedWriteable {
    #[allow(clippy::missing_inline_in_public_items)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Just print the raw fd number.
        f.debug_struct("OwnedWriteable")
            .field("fd", &self.0)
            .finish()
    }
}

#[cfg(windows)]
impl fmt::Debug for OwnedWriteable {
    #[allow(clippy::missing_inline_in_public_items)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Just print the raw handle or socket.
        f.debug_struct("OwnedWriteable")
            .field("handle_or_socket", &self.0)
            .finish()
    }
}