1#![stable(feature = "rust1", since = "1.0.0")]
4
5#[cfg(target_os = "hermit")]
6use hermit_abi as libc;
7#[cfg(target_os = "motor")]
8use moto_rt::libc;
9
10#[cfg(target_os = "motor")]
11use super::owned::OwnedFd;
12use crate::alloc::Allocator;
13#[cfg(not(target_os = "trusty"))]
14use crate::fs;
15use crate::io;
16use crate::macros::rustdoc_skip_body;
17#[cfg(target_os = "hermit")]
18use crate::os::hermit::io::OwnedFd;
19#[cfg(all(not(target_os = "hermit"), not(target_os = "motor")))]
20use crate::os::raw;
21#[cfg(all(doc, not(any(target_family = "wasm", target_env = "sgx", target_os = "l4re"))))]
22use crate::os::unix::io::AsFd;
23#[cfg(unix)]
24use crate::os::unix::io::OwnedFd;
25#[cfg(target_os = "wasi")]
26use crate::os::wasi::io::OwnedFd;
27#[cfg(not(target_os = "trusty"))]
28use crate::sys::{AsInner, FromInner, IntoInner};
29
30#[stable(feature = "rust1", since = "1.0.0")]
32#[cfg(all(not(target_os = "hermit"), not(target_os = "motor")))]
33pub type RawFd = raw::c_int;
34#[stable(feature = "rust1", since = "1.0.0")]
35#[cfg(any(target_os = "hermit", target_os = "motor"))]
36pub type RawFd = i32;
37
38#[stable(feature = "rust1", since = "1.0.0")]
44pub trait AsRawFd {
45 #[stable(feature = "rust1", since = "1.0.0")]
71 fn as_raw_fd(&self) -> RawFd;
72}
73
74#[stable(feature = "from_raw_os", since = "1.1.0")]
77pub trait FromRawFd {
78 #[stable(feature = "from_raw_os", since = "1.1.0")]
115 unsafe fn from_raw_fd(fd: RawFd) -> Self;
116}
117
118#[stable(feature = "into_raw_os", since = "1.4.0")]
121pub trait IntoRawFd {
122 #[must_use = "losing the raw file descriptor may leak resources"]
146 #[stable(feature = "into_raw_os", since = "1.4.0")]
147 fn into_raw_fd(self) -> RawFd;
148}
149
150#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
151impl AsRawFd for RawFd {
152 #[inline]
153 fn as_raw_fd(&self) -> RawFd {
154 *self
155 }
156}
157#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
158impl IntoRawFd for RawFd {
159 #[inline]
160 fn into_raw_fd(self) -> RawFd {
161 self
162 }
163}
164#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
165impl FromRawFd for RawFd {
166 #[inline]
167 unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
168 fd
169 }
170}
171
172#[stable(feature = "rust1", since = "1.0.0")]
173#[cfg(not(target_os = "trusty"))]
174impl AsRawFd for fs::File {
175 #[inline]
176 #[rustdoc_skip_body]
177 fn as_raw_fd(&self) -> RawFd {
178 self.as_inner().as_raw_fd()
179 }
180}
181#[stable(feature = "from_raw_os", since = "1.1.0")]
182#[cfg(not(target_os = "trusty"))]
183impl FromRawFd for fs::File {
184 #[inline]
185 #[rustdoc_skip_body]
186 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
187 unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
188 }
189}
190#[stable(feature = "into_raw_os", since = "1.4.0")]
191#[cfg(not(target_os = "trusty"))]
192impl IntoRawFd for fs::File {
193 #[inline]
194 #[rustdoc_skip_body]
195 fn into_raw_fd(self) -> RawFd {
196 self.into_inner().into_inner().into_raw_fd()
197 }
198}
199
200#[stable(feature = "asraw_stdio", since = "1.21.0")]
201#[cfg(not(target_os = "trusty"))]
202impl AsRawFd for io::Stdin {
203 #[inline]
204 fn as_raw_fd(&self) -> RawFd {
205 libc::STDIN_FILENO
206 }
207}
208
209#[stable(feature = "asraw_stdio", since = "1.21.0")]
210impl AsRawFd for io::Stdout {
211 #[inline]
212 fn as_raw_fd(&self) -> RawFd {
213 libc::STDOUT_FILENO
214 }
215}
216
217#[stable(feature = "asraw_stdio", since = "1.21.0")]
218impl AsRawFd for io::Stderr {
219 #[inline]
220 fn as_raw_fd(&self) -> RawFd {
221 libc::STDERR_FILENO
222 }
223}
224
225#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
226#[cfg(not(target_os = "trusty"))]
227impl<'a> AsRawFd for io::StdinLock<'a> {
228 #[inline]
229 fn as_raw_fd(&self) -> RawFd {
230 libc::STDIN_FILENO
231 }
232}
233
234#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
235impl<'a> AsRawFd for io::StdoutLock<'a> {
236 #[inline]
237 fn as_raw_fd(&self) -> RawFd {
238 libc::STDOUT_FILENO
239 }
240}
241
242#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
243impl<'a> AsRawFd for io::StderrLock<'a> {
244 #[inline]
245 fn as_raw_fd(&self) -> RawFd {
246 libc::STDERR_FILENO
247 }
248}
249
250#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
266impl<T: AsRawFd> AsRawFd for crate::sync::Arc<T> {
267 #[inline]
268 fn as_raw_fd(&self) -> RawFd {
269 (**self).as_raw_fd()
270 }
271}
272
273#[stable(feature = "asfd_rc", since = "1.69.0")]
274impl<T: AsRawFd> AsRawFd for crate::rc::Rc<T> {
275 #[inline]
276 fn as_raw_fd(&self) -> RawFd {
277 (**self).as_raw_fd()
278 }
279}
280
281#[unstable(feature = "unique_rc_arc", issue = "112566")]
282impl<T: AsRawFd + ?Sized> AsRawFd for crate::rc::UniqueRc<T> {
283 #[inline]
284 fn as_raw_fd(&self) -> RawFd {
285 (**self).as_raw_fd()
286 }
287}
288
289#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
290impl<T: AsRawFd, A: Allocator> AsRawFd for Box<T, A> {
291 #[inline]
292 fn as_raw_fd(&self) -> RawFd {
293 (**self).as_raw_fd()
294 }
295}
296
297#[stable(feature = "anonymous_pipe", since = "1.87.0")]
298#[cfg(not(target_os = "trusty"))]
299impl AsRawFd for io::PipeReader {
300 #[rustdoc_skip_body]
301 fn as_raw_fd(&self) -> RawFd {
302 self.0.as_raw_fd()
303 }
304}
305
306#[stable(feature = "anonymous_pipe", since = "1.87.0")]
307#[cfg(not(target_os = "trusty"))]
308impl FromRawFd for io::PipeReader {
309 #[rustdoc_skip_body]
310 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
311 Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
312 }
313}
314
315#[stable(feature = "anonymous_pipe", since = "1.87.0")]
316#[cfg(not(target_os = "trusty"))]
317impl IntoRawFd for io::PipeReader {
318 #[rustdoc_skip_body]
319 fn into_raw_fd(self) -> RawFd {
320 self.0.into_raw_fd()
321 }
322}
323
324#[stable(feature = "anonymous_pipe", since = "1.87.0")]
325#[cfg(not(target_os = "trusty"))]
326impl AsRawFd for io::PipeWriter {
327 #[rustdoc_skip_body]
328 fn as_raw_fd(&self) -> RawFd {
329 self.0.as_raw_fd()
330 }
331}
332
333#[stable(feature = "anonymous_pipe", since = "1.87.0")]
334#[cfg(not(target_os = "trusty"))]
335impl FromRawFd for io::PipeWriter {
336 #[rustdoc_skip_body]
337 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
338 Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
339 }
340}
341
342#[stable(feature = "anonymous_pipe", since = "1.87.0")]
343#[cfg(not(target_os = "trusty"))]
344impl IntoRawFd for io::PipeWriter {
345 #[rustdoc_skip_body]
346 fn into_raw_fd(self) -> RawFd {
347 self.0.into_raw_fd()
348 }
349}