Skip to main content

std/os/fd/
owned.rs

1//! Owned and borrowed Unix-like file descriptors.
2
3#![stable(feature = "io_safety", since = "1.63.0")]
4#![deny(unsafe_op_in_unsafe_fn)]
5
6#[cfg(target_os = "motor")]
7use moto_rt::libc;
8
9use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
10use crate::alloc::Allocator;
11#[cfg(not(target_os = "trusty"))]
12use crate::fs;
13use crate::marker::PhantomData;
14use crate::mem::ManuallyDrop;
15#[cfg(not(any(
16    all(target_family = "wasm", not(target_os = "emscripten")),
17    target_env = "sgx",
18    target_os = "hermit",
19    target_os = "trusty",
20    target_os = "motor"
21)))]
22use crate::sys::cvt;
23#[cfg(not(target_os = "trusty"))]
24use crate::sys::{AsInner, FromInner, IntoInner};
25use crate::{fmt, io};
26
27type ValidRawFd = core::num::niche_types::NotAllOnes<RawFd>;
28
29/// A borrowed file descriptor.
30///
31/// This has a lifetime parameter to tie it to the lifetime of something that owns the file
32/// descriptor. For the duration of that lifetime, it is guaranteed that nobody will close the file
33/// descriptor.
34///
35/// This uses `repr(transparent)` and has the representation of a host file
36/// descriptor, so it can be used in FFI in places where a file descriptor is
37/// passed as an argument, it is not captured or consumed, and it never has the
38/// value `-1`.
39///
40/// This type does not have a [`ToOwned`][crate::borrow::ToOwned]
41/// implementation. Calling `.to_owned()` on a variable of this type will call
42/// it on `&BorrowedFd` and use `Clone::clone()` like `ToOwned` does for all
43/// types implementing `Clone`. The result will be descriptor borrowed under
44/// the same lifetime.
45///
46/// To obtain an [`OwnedFd`], you can use [`BorrowedFd::try_clone_to_owned`]
47/// instead, but this is not supported on all platforms.
48#[derive(Copy, Clone)]
49#[repr(transparent)]
50#[rustc_nonnull_optimization_guaranteed]
51#[stable(feature = "io_safety", since = "1.63.0")]
52pub struct BorrowedFd<'fd> {
53    fd: ValidRawFd,
54    _phantom: PhantomData<&'fd OwnedFd>,
55}
56
57/// An owned file descriptor.
58///
59/// This closes the file descriptor on drop. It is guaranteed that nobody else will close the file
60/// descriptor.
61///
62/// This uses `repr(transparent)` and has the representation of a host file
63/// descriptor, so it can be used in FFI in places where a file descriptor is
64/// passed as a consumed argument or returned as an owned value, and it never
65/// has the value `-1`.
66///
67/// You can use [`AsFd::as_fd`] to obtain a [`BorrowedFd`].
68#[repr(transparent)]
69#[rustc_nonnull_optimization_guaranteed]
70#[stable(feature = "io_safety", since = "1.63.0")]
71pub struct OwnedFd {
72    fd: ValidRawFd,
73}
74
75impl BorrowedFd<'_> {
76    /// Returns a `BorrowedFd` holding the given raw file descriptor.
77    ///
78    /// # Safety
79    ///
80    /// The resource pointed to by `fd` must remain open for the duration of
81    /// the returned `BorrowedFd`.
82    ///
83    /// # Panics
84    ///
85    /// Panics if the raw file descriptor has the value `-1`.
86    #[inline]
87    #[track_caller]
88    #[rustc_const_stable(feature = "io_safety", since = "1.63.0")]
89    #[stable(feature = "io_safety", since = "1.63.0")]
90    pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
91        Self { fd: ValidRawFd::new(fd).expect("fd != -1"), _phantom: PhantomData }
92    }
93
94    /// Converts a `BorrowedFd` into a reference to a file-like type.
95    ///
96    #[cfg_attr(all(unix, not(miri)), doc = "```")]
97    #[cfg_attr(not(unix), doc = "```ignore (needs unix)")]
98    /// # #![feature(fd_view)]
99    /// # use std::fs::File;
100    /// # use std::io;
101    /// # use std::os::fd::AsFd;
102    /// let meta = io::stdout().as_fd().to_view::<File>().metadata().unwrap();
103    /// if meta.is_file() {
104    ///     println!("stdout is a regular file");
105    /// }
106    /// ```
107    #[unstable(feature = "fd_view", issue = "160402")]
108    pub fn to_view<T>(&self) -> &T
109    where
110        Self: AsRef<T>,
111    {
112        self.as_ref()
113    }
114}
115
116impl OwnedFd {
117    /// Creates a new `OwnedFd` instance that shares the same underlying file
118    /// description as the existing `OwnedFd` instance.
119    #[stable(feature = "io_safety", since = "1.63.0")]
120    pub fn try_clone(&self) -> io::Result<Self> {
121        self.as_fd().try_clone_to_owned()
122    }
123}
124
125impl BorrowedFd<'_> {
126    /// Creates a new `OwnedFd` instance that shares the same underlying file
127    /// description as the existing `BorrowedFd` instance.
128    #[cfg(not(any(
129        all(target_family = "wasm", not(target_os = "emscripten")),
130        target_os = "hermit",
131        target_os = "trusty",
132        target_os = "motor"
133    )))]
134    #[stable(feature = "io_safety", since = "1.63.0")]
135    pub fn try_clone_to_owned(&self) -> io::Result<OwnedFd> {
136        // We want to atomically duplicate this file descriptor and set the
137        // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
138        // is a POSIX flag that was added to Linux in 2.6.24.
139        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
140        let cmd = libc::F_DUPFD_CLOEXEC;
141
142        // For ESP-IDF, F_DUPFD is used instead, because the CLOEXEC semantics
143        // will never be supported, as this is a bare metal framework with
144        // no capabilities for multi-process execution. While F_DUPFD is also
145        // not supported yet, it might be (currently it returns ENOSYS).
146        #[cfg(any(target_os = "espidf", target_os = "vita"))]
147        let cmd = libc::F_DUPFD;
148
149        // Avoid using file descriptors below 3 as they are used for stdio
150        let fd = cvt(unsafe { libc::fcntl(self.as_raw_fd(), cmd, 3) })?;
151        Ok(unsafe { OwnedFd::from_raw_fd(fd) })
152    }
153
154    /// Creates a new `OwnedFd` instance that shares the same underlying file
155    /// description as the existing `BorrowedFd` instance.
156    #[cfg(any(
157        all(target_family = "wasm", not(target_os = "emscripten")),
158        target_os = "hermit",
159        target_os = "trusty"
160    ))]
161    #[stable(feature = "io_safety", since = "1.63.0")]
162    pub fn try_clone_to_owned(&self) -> io::Result<OwnedFd> {
163        Err(io::Error::UNSUPPORTED_PLATFORM)
164    }
165
166    /// Creates a new `OwnedFd` instance that shares the same underlying file
167    /// description as the existing `BorrowedFd` instance.
168    #[cfg(target_os = "motor")]
169    #[stable(feature = "io_safety", since = "1.63.0")]
170    pub fn try_clone_to_owned(&self) -> io::Result<OwnedFd> {
171        let fd = moto_rt::fs::duplicate(self.as_raw_fd()).map_err(crate::sys::map_motor_error)?;
172        Ok(unsafe { OwnedFd::from_raw_fd(fd) })
173    }
174}
175
176#[stable(feature = "io_safety", since = "1.63.0")]
177impl AsRawFd for BorrowedFd<'_> {
178    #[inline]
179    fn as_raw_fd(&self) -> RawFd {
180        self.fd.as_inner()
181    }
182}
183
184#[stable(feature = "io_safety", since = "1.63.0")]
185impl AsRawFd for OwnedFd {
186    #[inline]
187    fn as_raw_fd(&self) -> RawFd {
188        self.fd.as_inner()
189    }
190}
191
192#[stable(feature = "io_safety", since = "1.63.0")]
193impl IntoRawFd for OwnedFd {
194    #[inline]
195    fn into_raw_fd(self) -> RawFd {
196        ManuallyDrop::new(self).fd.as_inner()
197    }
198}
199
200#[stable(feature = "io_safety", since = "1.63.0")]
201impl FromRawFd for OwnedFd {
202    /// Constructs a new instance of `Self` from the given raw file descriptor.
203    ///
204    /// # Safety
205    ///
206    /// The resource pointed to by `fd` must be open and suitable for assuming
207    /// [ownership][io-safety]. The resource must not require any cleanup other than `close`.
208    ///
209    /// [io-safety]: io#io-safety
210    ///
211    /// # Panics
212    ///
213    /// Panics if the raw file descriptor has the value `-1`.
214    #[inline]
215    #[track_caller]
216    unsafe fn from_raw_fd(fd: RawFd) -> Self {
217        Self { fd: ValidRawFd::new(fd).expect("fd != -1") }
218    }
219}
220
221#[stable(feature = "io_safety", since = "1.63.0")]
222impl Drop for OwnedFd {
223    #[inline]
224    fn drop(&mut self) {
225        unsafe {
226            // Note that errors are ignored when closing a file descriptor. According to POSIX 2024,
227            // we can and indeed should retry `close` on `EINTR`
228            // (https://pubs.opengroup.org/onlinepubs/9799919799/functions/close.html),
229            // but it is not clear yet how well widely-used implementations are conforming with this
230            // mandate since older versions of POSIX left the state of the FD after an `EINTR`
231            // unspecified. Ignoring errors is "fine" because some of the major Unices (in
232            // particular, Linux) do make sure to always close the FD, even when `close()` is
233            // interrupted, and the scenario is rare to begin with. If we retried on a
234            // not-POSIX-compliant implementation, the consequences could be really bad since we may
235            // close the wrong FD. Helpful link to an epic discussion by POSIX workgroup that led to
236            // the latest POSIX wording: http://austingroupbugs.net/view.php?id=529
237            #[cfg(not(target_os = "hermit"))]
238            {
239                #[cfg(unix)]
240                crate::sys::fs::debug_assert_fd_is_open(self.fd.as_inner());
241
242                let _ = libc::close(self.fd.as_inner());
243            }
244            #[cfg(target_os = "hermit")]
245            let _ = hermit_abi::close(self.fd.as_inner());
246        }
247    }
248}
249
250#[stable(feature = "io_safety", since = "1.63.0")]
251impl fmt::Debug for BorrowedFd<'_> {
252    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253        f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
254    }
255}
256
257#[stable(feature = "io_safety", since = "1.63.0")]
258impl fmt::Debug for OwnedFd {
259    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260        f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
261    }
262}
263
264macro_rules! impl_is_terminal {
265    ($($t:ty),*$(,)?) => {$(
266        #[stable(feature = "is_terminal", since = "1.70.0")]
267        impl io::IsTerminal for $t {
268            #[inline]
269            fn is_terminal(&self) -> bool {
270                crate::sys::io::is_terminal(self)
271            }
272        }
273    )*}
274}
275
276impl_is_terminal!(BorrowedFd<'_>, OwnedFd);
277
278/// A trait to borrow the file descriptor from an underlying object.
279///
280/// This is only available on unix platforms and must be imported in order to
281/// call the method. Windows platforms have a corresponding `AsHandle` and
282/// `AsSocket` set of traits.
283#[stable(feature = "io_safety", since = "1.63.0")]
284pub trait AsFd {
285    /// Borrows the file descriptor.
286    ///
287    /// # Example
288    ///
289    /// ```rust,no_run
290    /// use std::fs::File;
291    /// # use std::io;
292    /// # #[cfg(any(unix, target_os = "wasi"))]
293    /// # use std::os::fd::{AsFd, BorrowedFd};
294    ///
295    /// let mut f = File::open("foo.txt")?;
296    /// # #[cfg(any(unix, target_os = "wasi"))]
297    /// let borrowed_fd: BorrowedFd<'_> = f.as_fd();
298    /// # Ok::<(), io::Error>(())
299    /// ```
300    #[stable(feature = "io_safety", since = "1.63.0")]
301    fn as_fd(&self) -> BorrowedFd<'_>;
302}
303
304#[stable(feature = "io_safety", since = "1.63.0")]
305impl<T: AsFd + ?Sized> AsFd for &T {
306    #[inline]
307    fn as_fd(&self) -> BorrowedFd<'_> {
308        T::as_fd(self)
309    }
310}
311
312#[stable(feature = "io_safety", since = "1.63.0")]
313impl<T: AsFd + ?Sized> AsFd for &mut T {
314    #[inline]
315    fn as_fd(&self) -> BorrowedFd<'_> {
316        T::as_fd(self)
317    }
318}
319
320#[stable(feature = "io_safety", since = "1.63.0")]
321impl AsFd for BorrowedFd<'_> {
322    #[inline]
323    fn as_fd(&self) -> BorrowedFd<'_> {
324        *self
325    }
326}
327
328#[stable(feature = "io_safety", since = "1.63.0")]
329impl AsFd for OwnedFd {
330    #[inline]
331    fn as_fd(&self) -> BorrowedFd<'_> {
332        // Safety: `OwnedFd` and `BorrowedFd` have the same validity
333        // invariants, and the `BorrowedFd` is bounded by the lifetime
334        // of `&self`.
335        unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
336    }
337}
338
339#[stable(feature = "io_safety", since = "1.63.0")]
340#[cfg(not(target_os = "trusty"))]
341impl AsFd for fs::File {
342    #[inline]
343    fn as_fd(&self) -> BorrowedFd<'_> {
344        self.as_inner().as_fd()
345    }
346}
347
348#[stable(feature = "io_safety", since = "1.63.0")]
349#[cfg(not(target_os = "trusty"))]
350impl From<fs::File> for OwnedFd {
351    /// Takes ownership of a [`File`](fs::File)'s underlying file descriptor.
352    #[inline]
353    fn from(file: fs::File) -> OwnedFd {
354        file.into_inner().into_inner().into_inner()
355    }
356}
357
358#[stable(feature = "io_safety", since = "1.63.0")]
359#[cfg(not(target_os = "trusty"))]
360impl From<OwnedFd> for fs::File {
361    /// Returns a [`File`](fs::File) that takes ownership of the given
362    /// file descriptor.
363    #[inline]
364    fn from(owned_fd: OwnedFd) -> Self {
365        Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned_fd)))
366    }
367}
368
369#[stable(feature = "io_safety", since = "1.63.0")]
370#[cfg(not(target_os = "trusty"))]
371impl AsFd for crate::net::TcpStream {
372    #[inline]
373    fn as_fd(&self) -> BorrowedFd<'_> {
374        self.as_inner().socket().as_fd()
375    }
376}
377
378#[stable(feature = "io_safety", since = "1.63.0")]
379#[cfg(not(target_os = "trusty"))]
380impl From<crate::net::TcpStream> for OwnedFd {
381    /// Takes ownership of a [`TcpStream`](crate::net::TcpStream)'s socket file descriptor.
382    #[inline]
383    fn from(tcp_stream: crate::net::TcpStream) -> OwnedFd {
384        tcp_stream.into_inner().into_socket().into_inner().into_inner()
385    }
386}
387
388#[stable(feature = "io_safety", since = "1.63.0")]
389#[cfg(not(target_os = "trusty"))]
390impl From<OwnedFd> for crate::net::TcpStream {
391    #[inline]
392    fn from(owned_fd: OwnedFd) -> Self {
393        Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
394            owned_fd,
395        ))))
396    }
397}
398
399#[stable(feature = "io_safety", since = "1.63.0")]
400#[cfg(not(target_os = "trusty"))]
401impl AsFd for crate::net::TcpListener {
402    #[inline]
403    fn as_fd(&self) -> BorrowedFd<'_> {
404        self.as_inner().socket().as_fd()
405    }
406}
407
408#[stable(feature = "io_safety", since = "1.63.0")]
409#[cfg(not(target_os = "trusty"))]
410impl From<crate::net::TcpListener> for OwnedFd {
411    /// Takes ownership of a [`TcpListener`](crate::net::TcpListener)'s socket file descriptor.
412    #[inline]
413    fn from(tcp_listener: crate::net::TcpListener) -> OwnedFd {
414        tcp_listener.into_inner().into_socket().into_inner().into_inner()
415    }
416}
417
418#[stable(feature = "io_safety", since = "1.63.0")]
419#[cfg(not(target_os = "trusty"))]
420impl From<OwnedFd> for crate::net::TcpListener {
421    #[inline]
422    fn from(owned_fd: OwnedFd) -> Self {
423        Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
424            owned_fd,
425        ))))
426    }
427}
428
429#[stable(feature = "io_safety", since = "1.63.0")]
430#[cfg(not(target_os = "trusty"))]
431impl AsFd for crate::net::UdpSocket {
432    #[inline]
433    fn as_fd(&self) -> BorrowedFd<'_> {
434        self.as_inner().socket().as_fd()
435    }
436}
437
438#[stable(feature = "io_safety", since = "1.63.0")]
439#[cfg(not(target_os = "trusty"))]
440impl From<crate::net::UdpSocket> for OwnedFd {
441    /// Takes ownership of a [`UdpSocket`](crate::net::UdpSocket)'s file descriptor.
442    #[inline]
443    fn from(udp_socket: crate::net::UdpSocket) -> OwnedFd {
444        udp_socket.into_inner().into_socket().into_inner().into_inner()
445    }
446}
447
448#[stable(feature = "io_safety", since = "1.63.0")]
449#[cfg(not(target_os = "trusty"))]
450impl From<OwnedFd> for crate::net::UdpSocket {
451    #[inline]
452    fn from(owned_fd: OwnedFd) -> Self {
453        Self::from_inner(FromInner::from_inner(FromInner::from_inner(FromInner::from_inner(
454            owned_fd,
455        ))))
456    }
457}
458
459/// Implements `AsRef<$t>` conversions from `BorrowedFd` to the specified types.
460///
461/// # Safety
462///
463/// It must be sound to transmute `&BorrowedFd` to `&$t`.
464///
465/// This typically implies that:
466///
467/// - $t must be a wrapper with `BorrowedFd`, `OwnedFd` or `RawFd` being the single non-ZST-field.
468///   Nominally it also requires repr(transparent), but std can cheat because we can stay in
469///   sync with rustc's layout implementation details.
470/// - `&$t` must not have any methods that would allow closing the file descriptor.
471/// - `&$t` impls must tolerate instances being created from any kind of file-descriptor without
472///   going through some dedicated constructor function, akin to `From<OwnedFd>` being a noop
473///   conversion.
474pub(crate) macro unsafe_impl_fd_asrefs {
475    ($($t:ty),*$(,)?) => {$(
476        #[unstable(feature = "fd_view", issue = "160402")]
477        #[unstable_feature_bound(fd_view)]
478        impl AsRef<$t> for BorrowedFd<'_> {
479            #[inline]
480            fn as_ref(&self) -> &$t {
481                use core::mem;
482
483                // These are neither necessary nor sufficient, but generally
484                // a type that implements them is more likely to have the required behavior.
485                fn type_check<T: From<OwnedFd> + Into<OwnedFd> + AsFd>() {}
486
487                // sanity check in case layouts change
488                const {
489                    assert!(mem::size_of::<RawFd>() == mem::size_of::<$t>());
490                    assert!(mem::size_of::<RawFd>() == mem::size_of::<BorrowedFd<'_>>());
491                }
492                type_check::<$t>();
493                // SAFETY: See macro-level requirements.
494                unsafe { mem::transmute::<&BorrowedFd<'_>, &$t>(self) }
495            }
496        }
497    )*}
498}
499
500#[cfg(not(target_os = "trusty"))]
501unsafe_impl_fd_asrefs!(
502    crate::fs::File,
503    crate::net::TcpStream,
504    crate::net::TcpListener,
505    crate::net::UdpSocket,
506);
507
508#[cfg(unix)]
509unsafe_impl_fd_asrefs!(io::PipeWriter, io::PipeReader);
510
511#[stable(feature = "asfd_ptrs", since = "1.64.0")]
512/// This impl allows implementing traits that require `AsFd` on Arc.
513/// ```
514/// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg {
515/// # #[cfg(target_os = "wasi")]
516/// # use std::os::wasi::io::AsFd;
517/// # #[cfg(unix)]
518/// # use std::os::unix::io::AsFd;
519/// use std::net::UdpSocket;
520/// use std::sync::Arc;
521///
522/// trait MyTrait: AsFd {}
523/// impl MyTrait for Arc<UdpSocket> {}
524/// impl MyTrait for Box<UdpSocket> {}
525/// # }
526/// ```
527impl<T: AsFd + ?Sized> AsFd for crate::sync::Arc<T> {
528    #[inline]
529    fn as_fd(&self) -> BorrowedFd<'_> {
530        (**self).as_fd()
531    }
532}
533
534#[stable(feature = "asfd_rc", since = "1.69.0")]
535impl<T: AsFd + ?Sized> AsFd for crate::rc::Rc<T> {
536    #[inline]
537    fn as_fd(&self) -> BorrowedFd<'_> {
538        (**self).as_fd()
539    }
540}
541
542#[unstable(feature = "unique_rc_arc", issue = "112566")]
543impl<T: AsFd + ?Sized> AsFd for crate::rc::UniqueRc<T> {
544    #[inline]
545    fn as_fd(&self) -> BorrowedFd<'_> {
546        (**self).as_fd()
547    }
548}
549
550#[stable(feature = "asfd_ptrs", since = "1.64.0")]
551impl<T: AsFd + ?Sized, A: Allocator> AsFd for Box<T, A> {
552    #[inline]
553    fn as_fd(&self) -> BorrowedFd<'_> {
554        (**self).as_fd()
555    }
556}
557
558#[stable(feature = "io_safety", since = "1.63.0")]
559impl AsFd for io::Stdin {
560    #[inline]
561    fn as_fd(&self) -> BorrowedFd<'_> {
562        unsafe { BorrowedFd::borrow_raw(0) }
563    }
564}
565
566#[stable(feature = "io_safety", since = "1.63.0")]
567impl<'a> AsFd for io::StdinLock<'a> {
568    #[inline]
569    fn as_fd(&self) -> BorrowedFd<'_> {
570        // SAFETY: user code should not close stdin out from under the standard library
571        unsafe { BorrowedFd::borrow_raw(0) }
572    }
573}
574
575#[stable(feature = "io_safety", since = "1.63.0")]
576impl AsFd for io::Stdout {
577    #[inline]
578    fn as_fd(&self) -> BorrowedFd<'_> {
579        unsafe { BorrowedFd::borrow_raw(1) }
580    }
581}
582
583#[stable(feature = "io_safety", since = "1.63.0")]
584impl<'a> AsFd for io::StdoutLock<'a> {
585    #[inline]
586    fn as_fd(&self) -> BorrowedFd<'_> {
587        // SAFETY: user code should not close stdout out from under the standard library
588        unsafe { BorrowedFd::borrow_raw(1) }
589    }
590}
591
592#[stable(feature = "io_safety", since = "1.63.0")]
593impl AsFd for io::Stderr {
594    #[inline]
595    fn as_fd(&self) -> BorrowedFd<'_> {
596        unsafe { BorrowedFd::borrow_raw(2) }
597    }
598}
599
600#[stable(feature = "io_safety", since = "1.63.0")]
601impl<'a> AsFd for io::StderrLock<'a> {
602    #[inline]
603    fn as_fd(&self) -> BorrowedFd<'_> {
604        // SAFETY: user code should not close stderr out from under the standard library
605        unsafe { BorrowedFd::borrow_raw(2) }
606    }
607}
608
609#[stable(feature = "anonymous_pipe", since = "1.87.0")]
610#[cfg(not(target_os = "trusty"))]
611impl AsFd for io::PipeReader {
612    fn as_fd(&self) -> BorrowedFd<'_> {
613        self.0.as_fd()
614    }
615}
616
617#[stable(feature = "anonymous_pipe", since = "1.87.0")]
618#[cfg(not(target_os = "trusty"))]
619impl From<io::PipeReader> for OwnedFd {
620    fn from(pipe: io::PipeReader) -> Self {
621        pipe.0.into_inner()
622    }
623}
624
625#[stable(feature = "anonymous_pipe", since = "1.87.0")]
626#[cfg(not(target_os = "trusty"))]
627impl AsFd for io::PipeWriter {
628    fn as_fd(&self) -> BorrowedFd<'_> {
629        self.0.as_fd()
630    }
631}
632
633#[stable(feature = "anonymous_pipe", since = "1.87.0")]
634#[cfg(not(target_os = "trusty"))]
635impl From<io::PipeWriter> for OwnedFd {
636    fn from(pipe: io::PipeWriter) -> Self {
637        pipe.0.into_inner()
638    }
639}
640
641#[stable(feature = "anonymous_pipe", since = "1.87.0")]
642#[cfg(not(target_os = "trusty"))]
643impl From<OwnedFd> for io::PipeReader {
644    fn from(owned_fd: OwnedFd) -> Self {
645        Self(FromInner::from_inner(owned_fd))
646    }
647}
648
649#[stable(feature = "anonymous_pipe", since = "1.87.0")]
650#[cfg(not(target_os = "trusty"))]
651impl From<OwnedFd> for io::PipeWriter {
652    fn from(owned_fd: OwnedFd) -> Self {
653        Self(FromInner::from_inner(owned_fd))
654    }
655}