1#![stable(feature = "rust1", since = "1.0.0")]
4
5#[cfg(doc)]
6use crate::os::windows::io::{AsHandle, AsSocket};
7use crate::os::windows::io::{OwnedHandle, OwnedSocket};
8use crate::os::windows::raw;
9use crate::sys_common::{AsInner, FromInner, IntoInner};
10use crate::{fs, io, net, ptr, sys};
11
12#[stable(feature = "rust1", since = "1.0.0")]
14pub type RawHandle = raw::HANDLE;
15
16#[stable(feature = "rust1", since = "1.0.0")]
18pub type RawSocket = raw::SOCKET;
19
20#[stable(feature = "rust1", since = "1.0.0")]
22pub trait AsRawHandle {
23 #[stable(feature = "rust1", since = "1.0.0")]
40 fn as_raw_handle(&self) -> RawHandle;
41}
42
43#[stable(feature = "from_raw_os", since = "1.1.0")]
45pub trait FromRawHandle {
46 #[stable(feature = "from_raw_os", since = "1.1.0")]
72 unsafe fn from_raw_handle(handle: RawHandle) -> Self;
73}
74
75#[stable(feature = "into_raw_os", since = "1.4.0")]
78pub trait IntoRawHandle {
79 #[must_use = "losing the raw handle may leak resources"]
89 #[stable(feature = "into_raw_os", since = "1.4.0")]
90 fn into_raw_handle(self) -> RawHandle;
91}
92
93#[stable(feature = "rust1", since = "1.0.0")]
94impl AsRawHandle for fs::File {
95 #[inline]
96 fn as_raw_handle(&self) -> RawHandle {
97 self.as_inner().as_raw_handle() as RawHandle
98 }
99}
100
101#[stable(feature = "asraw_stdio", since = "1.21.0")]
102impl AsRawHandle for io::Stdin {
103 fn as_raw_handle(&self) -> RawHandle {
104 stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_INPUT_HANDLE) as RawHandle })
105 }
106}
107
108#[stable(feature = "asraw_stdio", since = "1.21.0")]
109impl AsRawHandle for io::Stdout {
110 fn as_raw_handle(&self) -> RawHandle {
111 stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_OUTPUT_HANDLE) as RawHandle })
112 }
113}
114
115#[stable(feature = "asraw_stdio", since = "1.21.0")]
116impl AsRawHandle for io::Stderr {
117 fn as_raw_handle(&self) -> RawHandle {
118 stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_ERROR_HANDLE) as RawHandle })
119 }
120}
121
122#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
123impl<'a> AsRawHandle for io::StdinLock<'a> {
124 fn as_raw_handle(&self) -> RawHandle {
125 stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_INPUT_HANDLE) as RawHandle })
126 }
127}
128
129#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
130impl<'a> AsRawHandle for io::StdoutLock<'a> {
131 fn as_raw_handle(&self) -> RawHandle {
132 stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_OUTPUT_HANDLE) as RawHandle })
133 }
134}
135
136#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
137impl<'a> AsRawHandle for io::StderrLock<'a> {
138 fn as_raw_handle(&self) -> RawHandle {
139 stdio_handle(unsafe { sys::c::GetStdHandle(sys::c::STD_ERROR_HANDLE) as RawHandle })
140 }
141}
142
143fn stdio_handle(raw: RawHandle) -> RawHandle {
146 if raw == sys::c::INVALID_HANDLE_VALUE { ptr::null_mut() } else { raw }
153}
154
155#[stable(feature = "from_raw_os", since = "1.1.0")]
156impl FromRawHandle for fs::File {
157 #[inline]
158 unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
159 unsafe {
160 let handle = handle as sys::c::HANDLE;
161 fs::File::from_inner(sys::fs::File::from_inner(FromInner::from_inner(
162 OwnedHandle::from_raw_handle(handle),
163 )))
164 }
165 }
166}
167
168#[stable(feature = "into_raw_os", since = "1.4.0")]
169impl IntoRawHandle for fs::File {
170 #[inline]
171 fn into_raw_handle(self) -> RawHandle {
172 self.into_inner().into_raw_handle() as *mut _
173 }
174}
175
176#[stable(feature = "rust1", since = "1.0.0")]
178pub trait AsRawSocket {
179 #[stable(feature = "rust1", since = "1.0.0")]
189 fn as_raw_socket(&self) -> RawSocket;
190}
191
192#[stable(feature = "from_raw_os", since = "1.1.0")]
194pub trait FromRawSocket {
195 #[stable(feature = "from_raw_os", since = "1.1.0")]
216 unsafe fn from_raw_socket(sock: RawSocket) -> Self;
217}
218
219#[stable(feature = "into_raw_os", since = "1.4.0")]
222pub trait IntoRawSocket {
223 #[must_use = "losing the raw socket may leak resources"]
233 #[stable(feature = "into_raw_os", since = "1.4.0")]
234 fn into_raw_socket(self) -> RawSocket;
235}
236
237#[stable(feature = "rust1", since = "1.0.0")]
238impl AsRawSocket for net::TcpStream {
239 #[inline]
240 fn as_raw_socket(&self) -> RawSocket {
241 self.as_inner().socket().as_raw_socket()
242 }
243}
244#[stable(feature = "rust1", since = "1.0.0")]
245impl AsRawSocket for net::TcpListener {
246 #[inline]
247 fn as_raw_socket(&self) -> RawSocket {
248 self.as_inner().socket().as_raw_socket()
249 }
250}
251#[stable(feature = "rust1", since = "1.0.0")]
252impl AsRawSocket for net::UdpSocket {
253 #[inline]
254 fn as_raw_socket(&self) -> RawSocket {
255 self.as_inner().socket().as_raw_socket()
256 }
257}
258
259#[stable(feature = "from_raw_os", since = "1.1.0")]
260impl FromRawSocket for net::TcpStream {
261 #[inline]
262 unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream {
263 unsafe {
264 let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
265 net::TcpStream::from_inner(sys::net::TcpStream::from_inner(sock))
266 }
267 }
268}
269#[stable(feature = "from_raw_os", since = "1.1.0")]
270impl FromRawSocket for net::TcpListener {
271 #[inline]
272 unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener {
273 unsafe {
274 let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
275 net::TcpListener::from_inner(sys::net::TcpListener::from_inner(sock))
276 }
277 }
278}
279#[stable(feature = "from_raw_os", since = "1.1.0")]
280impl FromRawSocket for net::UdpSocket {
281 #[inline]
282 unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket {
283 unsafe {
284 let sock = sys::net::Socket::from_inner(OwnedSocket::from_raw_socket(sock));
285 net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(sock))
286 }
287 }
288}
289
290#[stable(feature = "into_raw_os", since = "1.4.0")]
291impl IntoRawSocket for net::TcpStream {
292 #[inline]
293 fn into_raw_socket(self) -> RawSocket {
294 self.into_inner().into_socket().into_inner().into_raw_socket()
295 }
296}
297
298#[stable(feature = "into_raw_os", since = "1.4.0")]
299impl IntoRawSocket for net::TcpListener {
300 #[inline]
301 fn into_raw_socket(self) -> RawSocket {
302 self.into_inner().into_socket().into_inner().into_raw_socket()
303 }
304}
305
306#[stable(feature = "into_raw_os", since = "1.4.0")]
307impl IntoRawSocket for net::UdpSocket {
308 #[inline]
309 fn into_raw_socket(self) -> RawSocket {
310 self.into_inner().into_socket().into_inner().into_raw_socket()
311 }
312}