system_interface/io/
is_read_write.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
use std::io;
#[cfg(not(windows))]
use {io_lifetimes::AsFilelike, rustix::io::is_read_write};
#[cfg(windows)]
use {
    std::{
        os::windows::io::{AsRawSocket, RawSocket},
        ptr,
    },
    windows_sys::Win32::Networking::WinSock::{
        recv, send, MSG_PEEK, SOCKET, SOCKET_ERROR, WSAEFAULT, WSAESHUTDOWN, WSAEWOULDBLOCK,
    },
};

/// A trait for the `is_read_write` function.
pub trait IsReadWrite {
    /// Test whether the handle is readable and/or writable.
    fn is_read_write(&self) -> io::Result<(bool, bool)>;
}

#[cfg(not(windows))]
impl<T: AsFilelike> IsReadWrite for T {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        Ok(is_read_write(self)?)
    }
}

#[cfg(windows)]
impl IsReadWrite for std::fs::File {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        file_is_read_write(self)
    }
}

#[cfg(all(windows, feature = "cap_std_impls"))]
impl IsReadWrite for cap_std::fs::File {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        use io_lifetimes::AsFilelike;
        file_is_read_write(&self.as_filelike_view::<std::fs::File>())
    }
}

#[cfg(all(windows, feature = "cap_std_impls_fs_utf8"))]
impl IsReadWrite for cap_std::fs_utf8::File {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        use io_lifetimes::AsFilelike;
        file_is_read_write(&self.as_filelike_view::<std::fs::File>())
    }
}

#[cfg(all(windows, feature = "async-std"))]
impl IsReadWrite for async_std::fs::File {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        use io_lifetimes::AsFilelike;
        file_is_read_write(&self.as_filelike_view::<std::fs::File>())
    }
}

#[cfg(all(windows, feature = "async-std"))]
impl IsReadWrite for cap_async_std::fs::File {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        use io_lifetimes::AsFilelike;
        file_is_read_write(&self.as_filelike_view::<std::fs::File>())
    }
}

#[cfg(all(
    windows,
    feature = "async-std",
    feature = "cap_async_std_impls_fs_utf8"
))]
impl IsReadWrite for cap_async_std::fs_utf8::File {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        use io_lifetimes::AsFilelike;
        file_is_read_write(&self.as_filelike_view::<std::fs::File>())
    }
}

#[cfg(windows)]
impl IsReadWrite for std::net::TcpStream {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        raw_socket_is_read_write(self.as_raw_socket())
    }
}

#[cfg(all(windows, feature = "cap_std_impls"))]
impl IsReadWrite for cap_std::net::TcpStream {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        raw_socket_is_read_write(self.as_raw_socket())
    }
}

#[cfg(all(windows, feature = "cap_async_std_impls"))]
impl IsReadWrite for async_std::net::TcpStream {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        raw_socket_is_read_write(self.as_raw_socket())
    }
}

#[cfg(all(windows, feature = "cap_async_std_impls"))]
impl IsReadWrite for cap_async_std::net::TcpStream {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        raw_socket_is_read_write(self.as_raw_socket())
    }
}

#[cfg(windows)]
#[inline]
fn file_is_read_write(file: &std::fs::File) -> std::io::Result<(bool, bool)> {
    cap_fs_ext::IsFileReadWrite::is_file_read_write(file)
}

#[cfg(windows)]
fn raw_socket_is_read_write(raw_socket: RawSocket) -> io::Result<(bool, bool)> {
    let (mut read, mut write) = (true, true);

    // Detect write shutdown. A zero-length `send` doesn't block but does
    // provide a helpful error message.
    let socket = raw_socket as SOCKET;
    let write_result = unsafe { send(socket, ptr::null_mut(), 0, 0) };
    if write_result == SOCKET_ERROR {
        let err = io::Error::last_os_error();
        match err.raw_os_error() {
            Some(WSAESHUTDOWN) => write = false,
            Some(WSAEWOULDBLOCK) => (),
            _ => return Err(err),
        }
    }

    // Detect read shutdown. A normal zero-length `recv` does block, so
    // use deliberately invalid pointer, as we get different error codes in
    // the case of a shut-down stream.
    let read_result = unsafe { recv(socket, usize::MAX as *mut _, 1, MSG_PEEK) };
    if read_result == SOCKET_ERROR {
        let err = io::Error::last_os_error();
        match err.raw_os_error() {
            Some(WSAEFAULT) => (),
            Some(WSAESHUTDOWN) => read = false,
            _ => return Err(err),
        }
    }

    Ok((read, write))
}

#[cfg(all(windows, feature = "socket2"))]
impl IsReadWrite for socket2::Socket {
    #[inline]
    fn is_read_write(&self) -> io::Result<(bool, bool)> {
        use io_lifetimes::AsSocketlike;
        self.as_socketlike_view::<std::net::TcpStream>()
            .is_read_write()
    }
}