diff --git a/library/std/src/os/fd/owned.rs b/library/std/src/os/fd/owned.rs index dd965ddc01eef..d661a13edc5e5 100644 --- a/library/std/src/os/fd/owned.rs +++ b/library/std/src/os/fd/owned.rs @@ -355,3 +355,34 @@ impl From for crate::net::UdpSocket { )))) } } + +#[stable(feature = "io_safety", since = "1.63.0")] +/// This impl allows implementing traits that require `AsFd` on Arc. +/// ``` +/// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg { +/// # #[cfg(target_os = "wasi")] +/// # use std::os::wasi::io::AsFd; +/// # #[cfg(unix)] +/// # use std::os::unix::io::AsFd; +/// use std::net::UdpSocket; +/// use std::sync::Arc; +/// +/// trait MyTrait: AsFd {} +/// impl MyTrait for Arc {} +/// impl MyTrait for Box {} +/// # } +/// ``` +impl AsFd for crate::sync::Arc { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + (**self).as_fd() + } +} + +#[stable(feature = "io_safety", since = "1.63.0")] +impl AsFd for Box { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + (**self).as_fd() + } +} diff --git a/library/std/src/os/fd/raw.rs b/library/std/src/os/fd/raw.rs index 47ee88d97fb93..8ba4984bd5ad5 100644 --- a/library/std/src/os/fd/raw.rs +++ b/library/std/src/os/fd/raw.rs @@ -222,3 +222,29 @@ impl<'a> AsRawFd for io::StderrLock<'a> { libc::STDERR_FILENO } } + +#[stable(feature = "asrawfd_ptrs", since = "1.63.0")] +/// This impl allows implementing traits that require `AsRawFd` on Arc. +/// ``` +/// use std::net::UdpSocket; +/// use std::sync::Arc; +/// use std::os::unix::prelude::AsRawFd; +/// trait MyTrait: AsRawFd { +/// } +/// impl MyTrait for Arc {} +/// impl MyTrait for Box {} +/// ``` +impl AsRawFd for crate::sync::Arc { + #[inline] + fn as_raw_fd(&self) -> RawFd { + (**self).as_raw_fd() + } +} + +#[stable(feature = "asrawfd_ptrs", since = "1.63.0")] +impl AsRawFd for Box { + #[inline] + fn as_raw_fd(&self) -> RawFd { + (**self).as_raw_fd() + } +}