From ed1e3512dc5e0b25c693b95f39281c97c7bd3819 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 26 May 2022 15:10:46 -0500 Subject: [PATCH 1/2] `impl for {Arc,Box}` This allows implementing traits that require a raw FD on Arc and Box. Previously, you'd have to add the function to the trait itself: ```rust trait MyTrait { fn as_raw_fd(&self) -> RawFd; } impl MyTrait for Arc { fn as_raw_fd(&self) -> RawFd { (**self).as_raw_fd() } } ``` --- library/std/src/os/fd/raw.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/library/std/src/os/fd/raw.rs b/library/std/src/os/fd/raw.rs index 47ee88d97fb93..7b6d2402aa9db 100644 --- a/library/std/src/os/fd/raw.rs +++ b/library/std/src/os/fd/raw.rs @@ -222,3 +222,35 @@ impl<'a> AsRawFd for io::StderrLock<'a> { libc::STDERR_FILENO } } + +#[stable(feature = "asraw_ptrs", since = "1.63.0")] +/// This blanket impl allows implementing custom traits that require `AsRawFd` on Arc. +/// ``` +/// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg { +/// # #[cfg(target_os = "wasi")] +/// # use std::os::wasi::io::AsRawFd; +/// # #[cfg(unix)] +/// # use std::os::unix::io::AsRawFd; +/// use std::net::UdpSocket; +/// use std::sync::Arc; +/// trait MyTrait: AsRawFd { +/// } +/// impl MyTrait for Arc {} +/// impl MyTrait for Box {} +/// # } +/// ``` +#[stable(feature = "asrawfd_ptrs", since = "1.63.0")] +impl AsRawFd for crate::sync::Arc { + #[inline] + fn as_raw_fd(&self) -> RawFd { + (**self).as_raw_fd() + } +} + +#[stable(feature = "asraw_ptrs", since = "1.63.0")] +impl AsRawFd for Box { + #[inline] + fn as_raw_fd(&self) -> RawFd { + (**self).as_raw_fd() + } +} From cf483a130cdfa1e72678fd98c74b11fedb3ae8dd Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 27 May 2022 09:39:54 -0500 Subject: [PATCH 2/2] `impl AsFd for {Arc,Box}` --- library/std/src/os/fd/owned.rs | 31 +++++++++++++++++++++++++++++++ library/std/src/os/fd/raw.rs | 5 ++--- 2 files changed, 33 insertions(+), 3 deletions(-) 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 7b6d2402aa9db..345beb1824dfa 100644 --- a/library/std/src/os/fd/raw.rs +++ b/library/std/src/os/fd/raw.rs @@ -223,8 +223,7 @@ impl<'a> AsRawFd for io::StderrLock<'a> { } } -#[stable(feature = "asraw_ptrs", since = "1.63.0")] -/// This blanket impl allows implementing custom traits that require `AsRawFd` on Arc. +/// This impl allows implementing traits that require `AsRawFd` on Arc. /// ``` /// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg { /// # #[cfg(target_os = "wasi")] @@ -247,7 +246,7 @@ impl AsRawFd for crate::sync::Arc { } } -#[stable(feature = "asraw_ptrs", since = "1.63.0")] +#[stable(feature = "asrawfd_ptrs", since = "1.63.0")] impl AsRawFd for Box { #[inline] fn as_raw_fd(&self) -> RawFd {