1//! Implementations of io-lifetimes' traits for tokio'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"))]
6use crate::{AsFd, BorrowedFd, FromFd, OwnedFd};
7#[cfg(windows)]
8use crate::{AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, FromHandle, OwnedHandle};
9#[cfg(unix)]
10use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
11#[cfg(target_os = "wasi")]
12use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
13#[cfg(windows)]
14use std::os::windows::io::{AsRawHandle, AsRawSocket, FromRawHandle, IntoRawHandle};
15
16#[cfg(any(unix, target_os = "wasi"))]
17impl AsFd for tokio::fs::File {
18    #[inline]
19    fn as_fd(&self) -> BorrowedFd<'_> {
20        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
21    }
22}
23
24#[cfg(windows)]
25impl AsHandle for tokio::fs::File {
26    #[inline]
27    fn as_handle(&self) -> BorrowedHandle<'_> {
28        unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
29    }
30}
31
32#[cfg(any(unix, target_os = "wasi"))]
33impl FromFd for tokio::fs::File {
34    #[inline]
35    fn from_fd(owned: OwnedFd) -> Self {
36        unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
37    }
38}
39
40#[cfg(any(unix, target_os = "wasi"))]
41impl From<OwnedFd> for tokio::fs::File {
42    #[inline]
43    fn from(owned: OwnedFd) -> Self {
44        unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
45    }
46}
47
48#[cfg(windows)]
49impl FromHandle for tokio::fs::File {
50    #[inline]
51    fn from_handle(owned: OwnedHandle) -> Self {
52        unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
53    }
54}
55
56#[cfg(windows)]
57impl From<OwnedHandle> for tokio::fs::File {
58    #[inline]
59    fn from(owned: OwnedHandle) -> Self {
60        unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
61    }
62}
63
64#[cfg(any(unix, target_os = "wasi"))]
65impl AsFd for tokio::net::TcpStream {
66    #[inline]
67    fn as_fd(&self) -> BorrowedFd<'_> {
68        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
69    }
70}
71
72#[cfg(windows)]
73impl AsSocket for tokio::net::TcpStream {
74    #[inline]
75    fn as_socket(&self) -> BorrowedSocket<'_> {
76        unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
77    }
78}
79
80#[cfg(any(unix, target_os = "wasi"))]
81impl AsFd for tokio::net::TcpListener {
82    #[inline]
83    fn as_fd(&self) -> BorrowedFd<'_> {
84        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
85    }
86}
87
88#[cfg(windows)]
89impl AsSocket for tokio::net::TcpListener {
90    #[inline]
91    fn as_socket(&self) -> BorrowedSocket<'_> {
92        unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
93    }
94}
95
96#[cfg(any(unix, target_os = "wasi"))]
97impl AsFd for tokio::net::UdpSocket {
98    #[inline]
99    fn as_fd(&self) -> BorrowedFd<'_> {
100        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
101    }
102}
103
104#[cfg(windows)]
105impl AsSocket for tokio::net::UdpSocket {
106    #[inline]
107    fn as_socket(&self) -> BorrowedSocket<'_> {
108        unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
109    }
110}
111
112#[cfg(any(unix, target_os = "wasi"))]
113impl AsFd for tokio::io::Stdin {
114    #[inline]
115    fn as_fd(&self) -> BorrowedFd {
116        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
117    }
118}
119
120#[cfg(windows)]
121impl AsHandle for tokio::io::Stdin {
122    #[inline]
123    fn as_handle(&self) -> BorrowedHandle {
124        unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
125    }
126}
127
128#[cfg(any(unix, target_os = "wasi"))]
129impl AsFd for tokio::io::Stdout {
130    #[inline]
131    fn as_fd(&self) -> BorrowedFd<'_> {
132        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
133    }
134}
135
136#[cfg(windows)]
137impl AsHandle for tokio::io::Stdout {
138    #[inline]
139    fn as_handle(&self) -> BorrowedHandle<'_> {
140        unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
141    }
142}
143
144#[cfg(any(unix, target_os = "wasi"))]
145impl AsFd for tokio::io::Stderr {
146    #[inline]
147    fn as_fd(&self) -> BorrowedFd<'_> {
148        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
149    }
150}
151
152#[cfg(windows)]
153impl AsHandle for tokio::io::Stderr {
154    #[inline]
155    fn as_handle(&self) -> BorrowedHandle<'_> {
156        unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
157    }
158}
159
160#[cfg(unix)]
161impl AsFd for tokio::net::UnixStream {
162    #[inline]
163    fn as_fd(&self) -> BorrowedFd<'_> {
164        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
165    }
166}
167
168#[cfg(unix)]
169impl AsFd for tokio::net::UnixListener {
170    #[inline]
171    fn as_fd(&self) -> BorrowedFd<'_> {
172        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
173    }
174}
175
176#[cfg(unix)]
177impl AsFd for tokio::net::UnixDatagram {
178    #[inline]
179    fn as_fd(&self) -> BorrowedFd<'_> {
180        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
181    }
182}
183
184#[cfg(any(unix, target_os = "wasi"))]
185impl AsFd for tokio::process::ChildStdout {
186    #[inline]
187    fn as_fd(&self) -> BorrowedFd<'_> {
188        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
189    }
190}
191
192#[cfg(windows)]
193impl AsHandle for tokio::process::ChildStdin {
194    #[inline]
195    fn as_handle(&self) -> BorrowedHandle<'_> {
196        unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
197    }
198}
199
200#[cfg(windows)]
201impl AsHandle for tokio::process::ChildStdout {
202    #[inline]
203    fn as_handle(&self) -> BorrowedHandle<'_> {
204        unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
205    }
206}
207
208#[cfg(windows)]
209impl AsHandle for tokio::process::ChildStderr {
210    #[inline]
211    fn as_handle(&self) -> BorrowedHandle {
212        unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
213    }
214}
215