system_interface/io/
read_ready.rs

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
use crate::io::IsReadWrite;
use io_lifetimes::raw::{AsRawFilelike, FromRawFilelike};
#[cfg(not(any(windows, target_os = "redox")))]
use rustix::io::ioctl_fionread;
use std::io::{self, Seek, SeekFrom, Stdin, StdinLock};
#[cfg(not(target_os = "redox"))]
use std::net;
use std::process::{ChildStderr, ChildStdout};
#[cfg(windows)]
use {
    std::{mem::MaybeUninit, os::windows::io::AsRawSocket},
    windows_sys::Win32::Networking::WinSock::{ioctlsocket, FIONREAD, SOCKET},
};

/// Extension for readable streams that can indicate the number of bytes
/// ready to be read immediately.
pub trait ReadReady {
    /// Return the number of bytes which are ready to be read immediately.
    ///
    /// The returned number may be greater than the number of bytes actually
    /// readable if the end of the stream is known to be reachable without
    /// blocking.
    fn num_ready_bytes(&self) -> io::Result<u64>;
}

/// Implement `ReadReady` for `Stdin`.
#[cfg(not(any(windows, target_os = "redox")))]
impl ReadReady for Stdin {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(ioctl_fionread(self)?)
    }
}

/// Implement `ReadReady` for `Stdin`.
#[cfg(any(windows, target_os = "redox"))]
impl ReadReady for Stdin {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Return the conservatively correct result.
        Ok(0)
    }
}

/// Implement `ReadReady` for `StdinLock`.
#[cfg(not(any(windows, target_os = "redox")))]
impl<'a> ReadReady for StdinLock<'a> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(ioctl_fionread(self)?)
    }
}

/// Implement `ReadReady` for `StdinLock`.
#[cfg(any(windows, target_os = "redox"))]
impl<'a> ReadReady for StdinLock<'a> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Return the conservatively correct result.
        Ok(0)
    }
}

impl crate::io::ReadReady for std::fs::File {
    #[inline]
    fn num_ready_bytes(&self) -> std::io::Result<u64> {
        let (read, _write) = self.is_read_write()?;
        if read {
            // If it's a file, we can query how many bytes are left.
            let metadata = self.metadata()?;
            if metadata.is_file() {
                // When `File::tell` is stable use that, but for now, create a
                // temporary `File` so that we can get a `&mut File` and call
                // `seek` on it.
                let mut tmp = unsafe { std::fs::File::from_raw_filelike(self.as_raw_filelike()) };
                let current = tmp.seek(SeekFrom::Current(0));
                std::mem::forget(tmp);
                return Ok(metadata.len() - current?);
            }

            // Otherwise, try our luck with `FIONREAD`.
            #[cfg(unix)]
            if let Ok(n) = ioctl_fionread(self) {
                return Ok(n);
            }

            // It's something without a specific length that we can't query,
            // so return the conservatively correct answer.
            return Ok(0);
        }
        Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "stream is not readable",
        ))
    }
}

/// Implement `ReadReady` for `std::net::TcpStream`.
#[cfg(not(any(windows, target_os = "redox")))]
impl ReadReady for net::TcpStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(ioctl_fionread(self)?)
    }
}

/// Implement `ReadReady` for `std::net::TcpStream`.
#[cfg(not(any(windows, target_os = "redox")))]
#[cfg(feature = "cap_std_impls")]
impl ReadReady for cap_std::net::TcpStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        use io_lifetimes::AsSocketlike;
        self.as_socketlike_view::<net::TcpStream>()
            .num_ready_bytes()
    }
}

/// Implement `ReadReady` for `std::net::TcpStream`.
#[cfg(target_os = "redox")]
impl ReadReady for net::TcpStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Return the conservatively correct result.
        Ok(0)
    }
}

/// Implement `ReadReady` for `std::net::TcpStream`.
#[cfg(target_os = "redox")]
impl ReadReady for cap_std::net::TcpStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        use io_lifetimes::AsSocketlike;
        self.as_socketlike_view::<net::TcpStream>()
            .num_ready_bytes()
    }
}

/// Implement `ReadReady` for `std::os::unix::net::UnixStream`.
#[cfg(unix)]
impl ReadReady for std::os::unix::net::UnixStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(ioctl_fionread(self)?)
    }
}

/// Implement `ReadReady` for `&[u8]`.
impl ReadReady for &[u8] {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(self.len() as u64)
    }
}

/// Implement `ReadReady` for `std::io::Empty`.
impl ReadReady for std::io::Empty {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Empty has zero bytes, but say 1 so that consumers know that they
        // can immediately attempt to read 1 byte and it won't block.
        Ok(1)
    }
}

/// Implement `ReadReady` for `std::io::Repeat`.
impl ReadReady for std::io::Repeat {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Unlimited bytes available immediately.
        Ok(u64::MAX)
    }
}

/// Implement `ReadReady` for `std::collections::VecDeque<T>`.
impl<T> ReadReady for std::collections::VecDeque<T> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(self.len() as u64)
    }
}

/// Implement `ReadReady` for `Box`.
impl<R: ReadReady> ReadReady for Box<R> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        self.as_ref().num_ready_bytes()
    }
}

/// Implement `ReadReady` for `std::io::BufReader<R>`.
impl<R: std::io::Read + ReadReady> ReadReady for std::io::BufReader<R> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        let buffer = self.buffer();
        match self.get_ref().num_ready_bytes() {
            Ok(n) => Ok(buffer.len() as u64 + n),
            Err(_) if !buffer.is_empty() => Ok(buffer.len() as u64),
            Err(e) => Err(e),
        }
    }
}

/// Implement `ReadReady` for `std::io::Cursor<T>`.
impl<T: AsRef<[u8]>> ReadReady for std::io::Cursor<T> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(self.get_ref().as_ref().len() as u64 - self.position())
    }
}

/// Implement `ReadReady` for `std::io::Take<T>`.
impl<T: ReadReady> ReadReady for std::io::Take<T> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(std::cmp::min(
            self.limit(),
            self.get_ref().num_ready_bytes()?,
        ))
    }
}

/// Implement `ReadReady` for `std::io::Chain<T, U>`.
impl<T: ReadReady, U> ReadReady for std::io::Chain<T, U> {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Just return the ready bytes for the first source, since we don't
        // know if it'll block before we exhaust it and move to the second.
        self.get_ref().0.num_ready_bytes()
    }
}

/// Implement `ReadReady` for `std::net::TcpStream`.
#[cfg(windows)]
impl ReadReady for net::TcpStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        let mut arg = MaybeUninit::<std::os::raw::c_ulong>::uninit();
        if unsafe { ioctlsocket(self.as_raw_socket() as SOCKET, FIONREAD, arg.as_mut_ptr()) } == 0 {
            Ok(unsafe { arg.assume_init() }.into())
        } else {
            Err(io::Error::last_os_error())
        }
    }
}

/// Implement `ReadReady` for `std::net::TcpStream`.
#[cfg(windows)]
#[cfg(feature = "cap_std_impls")]
impl ReadReady for cap_std::net::TcpStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        use io_lifetimes::AsSocketlike;
        self.as_socketlike_view::<net::TcpStream>()
            .num_ready_bytes()
    }
}

/// Implement `ReadReady` for `std::net::TcpStream`.
#[cfg(feature = "socket2")]
impl ReadReady for socket2::Socket {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        use io_lifetimes::AsSocketlike;
        self.as_socketlike_view::<std::net::TcpStream>()
            .num_ready_bytes()
    }
}

#[cfg(all(not(windows), feature = "os_pipe"))]
impl ReadReady for os_pipe::PipeReader {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(ioctl_fionread(self)?)
    }
}

#[cfg(all(windows, feature = "os_pipe"))]
impl ReadReady for os_pipe::PipeReader {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Return the conservatively correct result.
        Ok(0)
    }
}

#[cfg(not(windows))]
impl ReadReady for ChildStdout {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(ioctl_fionread(self)?)
    }
}

#[cfg(windows)]
impl ReadReady for ChildStdout {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Return the conservatively correct result.
        Ok(0)
    }
}

#[cfg(not(windows))]
impl ReadReady for ChildStderr {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(ioctl_fionread(self)?)
    }
}

#[cfg(windows)]
impl ReadReady for ChildStderr {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        // Return the conservatively correct result.
        Ok(0)
    }
}

#[cfg(feature = "socketpair")]
impl ReadReady for socketpair::SocketpairStream {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        socketpair::SocketpairStream::num_ready_bytes(self)
    }
}

#[cfg(feature = "ssh2")]
impl ReadReady for ssh2::Channel {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        Ok(u64::from(self.read_window().available))
    }
}

#[cfg(feature = "char-device")]
impl ReadReady for char_device::CharDevice {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        char_device::CharDevice::num_ready_bytes(self)
    }
}

/// Implement `ReadReady` for `cap_std::fs::File`.
#[cfg(feature = "cap_std_impls")]
impl ReadReady for cap_std::fs::File {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        use io_lifetimes::AsFilelike;
        self.as_filelike_view::<std::fs::File>().num_ready_bytes()
    }
}

/// Implement `ReadReady` for `cap_std::fs_utf8::File`.
#[cfg(feature = "cap_std_impls_fs_utf8")]
impl ReadReady for cap_std::fs_utf8::File {
    #[inline]
    fn num_ready_bytes(&self) -> io::Result<u64> {
        use io_lifetimes::AsFilelike;
        self.as_filelike_view::<std::fs::File>().num_ready_bytes()
    }
}