• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Implementations of io-lifetimes' traits for async-std's types. In the
2 //! future, we'll prefer to have crates provide their own impls; this is
3 //! just a temporary measure.
4 
5 #[cfg(any(unix, target_os = "wasi"))]
6 use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
7 #[cfg(windows)]
8 use crate::{
9     AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, FromHandle, FromSocket, IntoHandle,
10     IntoSocket, OwnedHandle, OwnedSocket,
11 };
12 #[cfg(unix)]
13 use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
14 #[cfg(target_os = "wasi")]
15 use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
16 #[cfg(windows)]
17 use std::os::windows::io::{
18     AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket,
19 };
20 
21 unsafe impl FilelikeViewType for async_std::fs::File {}
22 
23 #[cfg(any(unix, target_os = "wasi"))]
24 impl AsFd for async_std::fs::File {
25     #[inline]
as_fd(&self) -> BorrowedFd<'_>26     fn as_fd(&self) -> BorrowedFd<'_> {
27         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
28     }
29 }
30 
31 #[cfg(windows)]
32 impl AsHandle for async_std::fs::File {
33     #[inline]
as_handle(&self) -> BorrowedHandle<'_>34     fn as_handle(&self) -> BorrowedHandle<'_> {
35         unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
36     }
37 }
38 
39 #[cfg(any(unix, target_os = "wasi"))]
40 impl IntoFd for async_std::fs::File {
41     #[inline]
into_fd(self) -> OwnedFd42     fn into_fd(self) -> OwnedFd {
43         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
44     }
45 }
46 
47 #[cfg(any(unix, target_os = "wasi"))]
48 impl From<async_std::fs::File> for OwnedFd {
49     #[inline]
from(owned: async_std::fs::File) -> Self50     fn from(owned: async_std::fs::File) -> Self {
51         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
52     }
53 }
54 
55 #[cfg(windows)]
56 impl IntoHandle for async_std::fs::File {
57     #[inline]
into_handle(self) -> OwnedHandle58     fn into_handle(self) -> OwnedHandle {
59         unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
60     }
61 }
62 
63 #[cfg(windows)]
64 impl From<async_std::fs::File> for OwnedHandle {
65     #[inline]
from(owned: async_std::fs::File) -> Self66     fn from(owned: async_std::fs::File) -> Self {
67         unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
68     }
69 }
70 
71 #[cfg(any(unix, target_os = "wasi"))]
72 impl FromFd for async_std::fs::File {
73     #[inline]
from_fd(owned: OwnedFd) -> Self74     fn from_fd(owned: OwnedFd) -> Self {
75         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
76     }
77 }
78 
79 #[cfg(any(unix, target_os = "wasi"))]
80 impl From<OwnedFd> for async_std::fs::File {
81     #[inline]
from(owned: OwnedFd) -> Self82     fn from(owned: OwnedFd) -> Self {
83         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
84     }
85 }
86 
87 #[cfg(windows)]
88 impl FromHandle for async_std::fs::File {
89     #[inline]
from_handle(owned: OwnedHandle) -> Self90     fn from_handle(owned: OwnedHandle) -> Self {
91         unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
92     }
93 }
94 
95 #[cfg(windows)]
96 impl From<OwnedHandle> for async_std::fs::File {
97     #[inline]
from(owned: OwnedHandle) -> Self98     fn from(owned: OwnedHandle) -> Self {
99         unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
100     }
101 }
102 
103 #[cfg(any(unix, target_os = "wasi"))]
104 impl AsFd for async_std::net::TcpStream {
105     #[inline]
as_fd(&self) -> BorrowedFd<'_>106     fn as_fd(&self) -> BorrowedFd<'_> {
107         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
108     }
109 }
110 
111 #[cfg(windows)]
112 impl AsSocket for async_std::net::TcpStream {
113     #[inline]
as_socket(&self) -> BorrowedSocket<'_>114     fn as_socket(&self) -> BorrowedSocket<'_> {
115         unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
116     }
117 }
118 
119 #[cfg(any(unix, target_os = "wasi"))]
120 impl IntoFd for async_std::net::TcpStream {
121     #[inline]
into_fd(self) -> OwnedFd122     fn into_fd(self) -> OwnedFd {
123         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
124     }
125 }
126 
127 #[cfg(any(unix, target_os = "wasi"))]
128 impl From<async_std::net::TcpStream> for OwnedFd {
129     #[inline]
from(owned: async_std::net::TcpStream) -> Self130     fn from(owned: async_std::net::TcpStream) -> Self {
131         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
132     }
133 }
134 
135 #[cfg(windows)]
136 impl IntoSocket for async_std::net::TcpStream {
137     #[inline]
into_socket(self) -> OwnedSocket138     fn into_socket(self) -> OwnedSocket {
139         unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
140     }
141 }
142 
143 #[cfg(windows)]
144 impl From<async_std::net::TcpStream> for OwnedSocket {
145     #[inline]
from(owned: async_std::net::TcpStream) -> Self146     fn from(owned: async_std::net::TcpStream) -> Self {
147         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
148     }
149 }
150 
151 #[cfg(any(unix, target_os = "wasi"))]
152 impl FromFd for async_std::net::TcpStream {
153     #[inline]
from_fd(owned: OwnedFd) -> Self154     fn from_fd(owned: OwnedFd) -> Self {
155         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
156     }
157 }
158 
159 #[cfg(any(unix, target_os = "wasi"))]
160 impl From<OwnedFd> for async_std::net::TcpStream {
161     #[inline]
from(owned: OwnedFd) -> Self162     fn from(owned: OwnedFd) -> Self {
163         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
164     }
165 }
166 
167 #[cfg(windows)]
168 impl FromSocket for async_std::net::TcpStream {
169     #[inline]
from_socket(owned: OwnedSocket) -> Self170     fn from_socket(owned: OwnedSocket) -> Self {
171         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
172     }
173 }
174 
175 #[cfg(windows)]
176 impl From<OwnedSocket> for async_std::net::TcpStream {
177     #[inline]
from(owned: OwnedSocket) -> Self178     fn from(owned: OwnedSocket) -> Self {
179         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
180     }
181 }
182 
183 #[cfg(any(unix, target_os = "wasi"))]
184 impl AsFd for async_std::net::TcpListener {
185     #[inline]
as_fd(&self) -> BorrowedFd<'_>186     fn as_fd(&self) -> BorrowedFd<'_> {
187         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
188     }
189 }
190 
191 #[cfg(windows)]
192 impl AsSocket for async_std::net::TcpListener {
193     #[inline]
as_socket(&self) -> BorrowedSocket<'_>194     fn as_socket(&self) -> BorrowedSocket<'_> {
195         unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
196     }
197 }
198 
199 #[cfg(any(unix, target_os = "wasi"))]
200 impl IntoFd for async_std::net::TcpListener {
201     #[inline]
into_fd(self) -> OwnedFd202     fn into_fd(self) -> OwnedFd {
203         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
204     }
205 }
206 
207 #[cfg(any(unix, target_os = "wasi"))]
208 impl From<async_std::net::TcpListener> for OwnedFd {
209     #[inline]
from(owned: async_std::net::TcpListener) -> Self210     fn from(owned: async_std::net::TcpListener) -> Self {
211         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
212     }
213 }
214 
215 #[cfg(windows)]
216 impl IntoSocket for async_std::net::TcpListener {
217     #[inline]
into_socket(self) -> OwnedSocket218     fn into_socket(self) -> OwnedSocket {
219         unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
220     }
221 }
222 
223 #[cfg(windows)]
224 impl From<async_std::net::TcpListener> for OwnedSocket {
225     #[inline]
from(owned: async_std::net::TcpListener) -> Self226     fn from(owned: async_std::net::TcpListener) -> Self {
227         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
228     }
229 }
230 
231 #[cfg(any(unix, target_os = "wasi"))]
232 impl FromFd for async_std::net::TcpListener {
233     #[inline]
from_fd(owned: OwnedFd) -> Self234     fn from_fd(owned: OwnedFd) -> Self {
235         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
236     }
237 }
238 
239 #[cfg(any(unix, target_os = "wasi"))]
240 impl From<OwnedFd> for async_std::net::TcpListener {
241     #[inline]
from(owned: OwnedFd) -> Self242     fn from(owned: OwnedFd) -> Self {
243         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
244     }
245 }
246 
247 #[cfg(windows)]
248 impl FromSocket for async_std::net::TcpListener {
249     #[inline]
from_socket(owned: OwnedSocket) -> Self250     fn from_socket(owned: OwnedSocket) -> Self {
251         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
252     }
253 }
254 
255 #[cfg(windows)]
256 impl From<OwnedSocket> for async_std::net::TcpListener {
257     #[inline]
from(owned: OwnedSocket) -> Self258     fn from(owned: OwnedSocket) -> Self {
259         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
260     }
261 }
262 
263 #[cfg(any(unix, target_os = "wasi"))]
264 impl AsFd for async_std::net::UdpSocket {
265     #[inline]
as_fd(&self) -> BorrowedFd<'_>266     fn as_fd(&self) -> BorrowedFd<'_> {
267         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
268     }
269 }
270 
271 #[cfg(windows)]
272 impl AsSocket for async_std::net::UdpSocket {
273     #[inline]
as_socket(&self) -> BorrowedSocket<'_>274     fn as_socket(&self) -> BorrowedSocket<'_> {
275         unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
276     }
277 }
278 
279 #[cfg(any(unix, target_os = "wasi"))]
280 impl IntoFd for async_std::net::UdpSocket {
281     #[inline]
into_fd(self) -> OwnedFd282     fn into_fd(self) -> OwnedFd {
283         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
284     }
285 }
286 
287 #[cfg(any(unix, target_os = "wasi"))]
288 impl From<async_std::net::UdpSocket> for OwnedFd {
289     #[inline]
from(owned: async_std::net::UdpSocket) -> Self290     fn from(owned: async_std::net::UdpSocket) -> Self {
291         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
292     }
293 }
294 
295 #[cfg(windows)]
296 impl IntoSocket for async_std::net::UdpSocket {
297     #[inline]
into_socket(self) -> OwnedSocket298     fn into_socket(self) -> OwnedSocket {
299         unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
300     }
301 }
302 
303 #[cfg(windows)]
304 impl From<async_std::net::UdpSocket> for OwnedSocket {
305     #[inline]
from(owned: async_std::net::UdpSocket) -> Self306     fn from(owned: async_std::net::UdpSocket) -> Self {
307         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
308     }
309 }
310 
311 #[cfg(any(unix, target_os = "wasi"))]
312 impl FromFd for async_std::net::UdpSocket {
313     #[inline]
from_fd(owned: OwnedFd) -> Self314     fn from_fd(owned: OwnedFd) -> Self {
315         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
316     }
317 }
318 
319 #[cfg(any(unix, target_os = "wasi"))]
320 impl From<OwnedFd> for async_std::net::UdpSocket {
321     #[inline]
from(owned: OwnedFd) -> Self322     fn from(owned: OwnedFd) -> Self {
323         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
324     }
325 }
326 
327 #[cfg(windows)]
328 impl FromSocket for async_std::net::UdpSocket {
329     #[inline]
from_socket(owned: OwnedSocket) -> Self330     fn from_socket(owned: OwnedSocket) -> Self {
331         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
332     }
333 }
334 
335 #[cfg(windows)]
336 impl From<OwnedSocket> for async_std::net::UdpSocket {
337     #[inline]
from(owned: OwnedSocket) -> Self338     fn from(owned: OwnedSocket) -> Self {
339         unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
340     }
341 }
342 
343 #[cfg(any(unix, target_os = "wasi"))]
344 impl AsFd for async_std::io::Stdin {
345     #[inline]
as_fd(&self) -> BorrowedFd<'_>346     fn as_fd(&self) -> BorrowedFd<'_> {
347         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
348     }
349 }
350 
351 #[cfg(windows)]
352 impl AsHandle for async_std::io::Stdin {
353     #[inline]
as_handle(&self) -> BorrowedHandle<'_>354     fn as_handle(&self) -> BorrowedHandle<'_> {
355         unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
356     }
357 }
358 
359 #[cfg(any(unix, target_os = "wasi"))]
360 impl AsFd for async_std::io::Stdout {
361     #[inline]
as_fd(&self) -> BorrowedFd<'_>362     fn as_fd(&self) -> BorrowedFd<'_> {
363         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
364     }
365 }
366 
367 #[cfg(windows)]
368 impl AsHandle for async_std::io::Stdout {
369     #[inline]
as_handle(&self) -> BorrowedHandle<'_>370     fn as_handle(&self) -> BorrowedHandle<'_> {
371         unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
372     }
373 }
374 
375 #[cfg(any(unix, target_os = "wasi"))]
376 impl AsFd for async_std::io::Stderr {
377     #[inline]
as_fd(&self) -> BorrowedFd<'_>378     fn as_fd(&self) -> BorrowedFd<'_> {
379         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
380     }
381 }
382 
383 #[cfg(windows)]
384 impl AsHandle for async_std::io::Stderr {
385     #[inline]
as_handle(&self) -> BorrowedHandle<'_>386     fn as_handle(&self) -> BorrowedHandle<'_> {
387         unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
388     }
389 }
390 
391 #[cfg(unix)]
392 impl AsFd for async_std::os::unix::net::UnixStream {
393     #[inline]
as_fd(&self) -> BorrowedFd<'_>394     fn as_fd(&self) -> BorrowedFd<'_> {
395         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
396     }
397 }
398 
399 #[cfg(unix)]
400 impl IntoFd for async_std::os::unix::net::UnixStream {
401     #[inline]
into_fd(self) -> OwnedFd402     fn into_fd(self) -> OwnedFd {
403         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
404     }
405 }
406 
407 #[cfg(unix)]
408 impl From<async_std::os::unix::net::UnixStream> for OwnedFd {
409     #[inline]
from(owned: async_std::os::unix::net::UnixStream) -> Self410     fn from(owned: async_std::os::unix::net::UnixStream) -> Self {
411         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
412     }
413 }
414 
415 #[cfg(unix)]
416 impl FromFd for async_std::os::unix::net::UnixStream {
417     #[inline]
from_fd(owned: OwnedFd) -> Self418     fn from_fd(owned: OwnedFd) -> Self {
419         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
420     }
421 }
422 
423 #[cfg(unix)]
424 impl From<OwnedFd> for async_std::os::unix::net::UnixStream {
425     #[inline]
from(owned: OwnedFd) -> Self426     fn from(owned: OwnedFd) -> Self {
427         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
428     }
429 }
430 
431 #[cfg(unix)]
432 impl AsFd for async_std::os::unix::net::UnixListener {
433     #[inline]
as_fd(&self) -> BorrowedFd<'_>434     fn as_fd(&self) -> BorrowedFd<'_> {
435         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
436     }
437 }
438 
439 #[cfg(unix)]
440 impl IntoFd for async_std::os::unix::net::UnixListener {
441     #[inline]
into_fd(self) -> OwnedFd442     fn into_fd(self) -> OwnedFd {
443         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
444     }
445 }
446 
447 #[cfg(unix)]
448 impl From<async_std::os::unix::net::UnixListener> for OwnedFd {
449     #[inline]
from(owned: async_std::os::unix::net::UnixListener) -> Self450     fn from(owned: async_std::os::unix::net::UnixListener) -> Self {
451         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
452     }
453 }
454 
455 #[cfg(unix)]
456 impl FromFd for async_std::os::unix::net::UnixListener {
457     #[inline]
from_fd(owned: OwnedFd) -> Self458     fn from_fd(owned: OwnedFd) -> Self {
459         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
460     }
461 }
462 
463 #[cfg(unix)]
464 impl From<OwnedFd> for async_std::os::unix::net::UnixListener {
465     #[inline]
from(owned: OwnedFd) -> Self466     fn from(owned: OwnedFd) -> Self {
467         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
468     }
469 }
470 
471 #[cfg(unix)]
472 impl AsFd for async_std::os::unix::net::UnixDatagram {
473     #[inline]
as_fd(&self) -> BorrowedFd<'_>474     fn as_fd(&self) -> BorrowedFd<'_> {
475         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
476     }
477 }
478 
479 #[cfg(unix)]
480 impl IntoFd for async_std::os::unix::net::UnixDatagram {
481     #[inline]
into_fd(self) -> OwnedFd482     fn into_fd(self) -> OwnedFd {
483         unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
484     }
485 }
486 
487 #[cfg(unix)]
488 impl From<async_std::os::unix::net::UnixDatagram> for OwnedFd {
489     #[inline]
from(owned: async_std::os::unix::net::UnixDatagram) -> Self490     fn from(owned: async_std::os::unix::net::UnixDatagram) -> Self {
491         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
492     }
493 }
494 
495 #[cfg(unix)]
496 impl FromFd for async_std::os::unix::net::UnixDatagram {
497     #[inline]
from_fd(owned: OwnedFd) -> Self498     fn from_fd(owned: OwnedFd) -> Self {
499         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
500     }
501 }
502 
503 #[cfg(unix)]
504 impl From<OwnedFd> for async_std::os::unix::net::UnixDatagram {
505     #[inline]
from(owned: OwnedFd) -> Self506     fn from(owned: OwnedFd) -> Self {
507         unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
508     }
509 }
510