1#![stable(feature = "rust1", since = "1.0.0")]
4
5#[cfg(target_os = "hermit")]
6use hermit_abi as libc;
7
8#[cfg(not(target_os = "trusty"))]
9use crate::fs;
10use crate::io;
11#[cfg(target_os = "hermit")]
12use crate::os::hermit::io::OwnedFd;
13#[cfg(not(target_os = "hermit"))]
14use crate::os::raw;
15#[cfg(all(doc, not(target_arch = "wasm32")))]
16use crate::os::unix::io::AsFd;
17#[cfg(unix)]
18use crate::os::unix::io::OwnedFd;
19#[cfg(target_os = "wasi")]
20use crate::os::wasi::io::OwnedFd;
21use crate::sys_common::FromInner;
22#[cfg(not(target_os = "trusty"))]
23use crate::sys_common::{AsInner, IntoInner};
24
25#[stable(feature = "rust1", since = "1.0.0")]
27#[cfg(not(target_os = "hermit"))]
28pub type RawFd = raw::c_int;
29#[stable(feature = "rust1", since = "1.0.0")]
30#[cfg(target_os = "hermit")]
31pub type RawFd = i32;
32
33#[stable(feature = "rust1", since = "1.0.0")]
39pub trait AsRawFd {
40 #[stable(feature = "rust1", since = "1.0.0")]
66 fn as_raw_fd(&self) -> RawFd;
67}
68
69#[stable(feature = "from_raw_os", since = "1.1.0")]
72pub trait FromRawFd {
73 #[stable(feature = "from_raw_os", since = "1.1.0")]
110 unsafe fn from_raw_fd(fd: RawFd) -> Self;
111}
112
113#[stable(feature = "into_raw_os", since = "1.4.0")]
116pub trait IntoRawFd {
117 #[must_use = "losing the raw file descriptor may leak resources"]
141 #[stable(feature = "into_raw_os", since = "1.4.0")]
142 fn into_raw_fd(self) -> RawFd;
143}
144
145#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
146impl AsRawFd for RawFd {
147 #[inline]
148 fn as_raw_fd(&self) -> RawFd {
149 *self
150 }
151}
152#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
153impl IntoRawFd for RawFd {
154 #[inline]
155 fn into_raw_fd(self) -> RawFd {
156 self
157 }
158}
159#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
160impl FromRawFd for RawFd {
161 #[inline]
162 unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
163 fd
164 }
165}
166
167#[stable(feature = "rust1", since = "1.0.0")]
168#[cfg(not(target_os = "trusty"))]
169impl AsRawFd for fs::File {
170 #[inline]
171 fn as_raw_fd(&self) -> RawFd {
172 self.as_inner().as_raw_fd()
173 }
174}
175#[stable(feature = "from_raw_os", since = "1.1.0")]
176#[cfg(not(target_os = "trusty"))]
177impl FromRawFd for fs::File {
178 #[inline]
179 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
180 unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
181 }
182}
183#[stable(feature = "into_raw_os", since = "1.4.0")]
184#[cfg(not(target_os = "trusty"))]
185impl IntoRawFd for fs::File {
186 #[inline]
187 fn into_raw_fd(self) -> RawFd {
188 self.into_inner().into_inner().into_raw_fd()
189 }
190}
191
192#[stable(feature = "asraw_stdio", since = "1.21.0")]
193#[cfg(not(target_os = "trusty"))]
194impl AsRawFd for io::Stdin {
195 #[inline]
196 fn as_raw_fd(&self) -> RawFd {
197 libc::STDIN_FILENO
198 }
199}
200
201#[stable(feature = "asraw_stdio", since = "1.21.0")]
202impl AsRawFd for io::Stdout {
203 #[inline]
204 fn as_raw_fd(&self) -> RawFd {
205 libc::STDOUT_FILENO
206 }
207}
208
209#[stable(feature = "asraw_stdio", since = "1.21.0")]
210impl AsRawFd for io::Stderr {
211 #[inline]
212 fn as_raw_fd(&self) -> RawFd {
213 libc::STDERR_FILENO
214 }
215}
216
217#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
218#[cfg(not(target_os = "trusty"))]
219impl<'a> AsRawFd for io::StdinLock<'a> {
220 #[inline]
221 fn as_raw_fd(&self) -> RawFd {
222 libc::STDIN_FILENO
223 }
224}
225
226#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
227impl<'a> AsRawFd for io::StdoutLock<'a> {
228 #[inline]
229 fn as_raw_fd(&self) -> RawFd {
230 libc::STDOUT_FILENO
231 }
232}
233
234#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
235impl<'a> AsRawFd for io::StderrLock<'a> {
236 #[inline]
237 fn as_raw_fd(&self) -> RawFd {
238 libc::STDERR_FILENO
239 }
240}
241
242#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
258impl<T: AsRawFd> AsRawFd for crate::sync::Arc<T> {
259 #[inline]
260 fn as_raw_fd(&self) -> RawFd {
261 (**self).as_raw_fd()
262 }
263}
264
265#[stable(feature = "asfd_rc", since = "1.69.0")]
266impl<T: AsRawFd> AsRawFd for crate::rc::Rc<T> {
267 #[inline]
268 fn as_raw_fd(&self) -> RawFd {
269 (**self).as_raw_fd()
270 }
271}
272
273#[unstable(feature = "unique_rc_arc", issue = "112566")]
274impl<T: AsRawFd + ?Sized> AsRawFd for crate::rc::UniqueRc<T> {
275 #[inline]
276 fn as_raw_fd(&self) -> RawFd {
277 (**self).as_raw_fd()
278 }
279}
280
281#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
282impl<T: AsRawFd> AsRawFd for Box<T> {
283 #[inline]
284 fn as_raw_fd(&self) -> RawFd {
285 (**self).as_raw_fd()
286 }
287}
288
289#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
290impl AsRawFd for io::PipeReader {
291 fn as_raw_fd(&self) -> RawFd {
292 self.0.as_raw_fd()
293 }
294}
295
296#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
297impl FromRawFd for io::PipeReader {
298 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
299 Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
300 }
301}
302
303#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
304impl IntoRawFd for io::PipeReader {
305 fn into_raw_fd(self) -> RawFd {
306 self.0.into_raw_fd()
307 }
308}
309
310#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
311impl AsRawFd for io::PipeWriter {
312 fn as_raw_fd(&self) -> RawFd {
313 self.0.as_raw_fd()
314 }
315}
316
317#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
318impl FromRawFd for io::PipeWriter {
319 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
320 Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
321 }
322}
323
324#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
325impl IntoRawFd for io::PipeWriter {
326 fn into_raw_fd(self) -> RawFd {
327 self.0.into_raw_fd()
328 }
329}