diff options
| author | Joshua Nelson <jnelson@cloudflare.com> | 2022-05-26 15:10:46 -0500 |
|---|---|---|
| committer | Joshua Nelson <jnelson@cloudflare.com> | 2022-06-21 23:03:55 -0500 |
| commit | ed1e3512dc5e0b25c693b95f39281c97c7bd3819 (patch) | |
| tree | e8f9df643aa4186387a055baf38edfac75bcfe04 | |
| parent | 3a8b0144c82197a70e919ad371d56f82c2282833 (diff) | |
`impl<T: AsRawFd> for {Arc,Box}<T>`
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<T: MyTrait> MyTrait for Arc<T> {
fn as_raw_fd(&self) -> RawFd {
(**self).as_raw_fd()
}
}
```
| -rw-r--r-- | library/std/src/os/fd/raw.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/library/std/src/os/fd/raw.rs b/library/std/src/os/fd/raw.rs index 47ee88d97fb..7b6d2402aa9 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<UdpSocket> {} +/// impl MyTrait for Box<UdpSocket> {} +/// # } +/// ``` +#[stable(feature = "asrawfd_ptrs", since = "1.63.0")] +impl<T: AsRawFd> AsRawFd for crate::sync::Arc<T> { + #[inline] + fn as_raw_fd(&self) -> RawFd { + (**self).as_raw_fd() + } +} + +#[stable(feature = "asraw_ptrs", since = "1.63.0")] +impl<T: AsRawFd> AsRawFd for Box<T> { + #[inline] + fn as_raw_fd(&self) -> RawFd { + (**self).as_raw_fd() + } +} |
