Skip to content

Commit 46450ec

Browse files
authored
Rollup merge of rust-lang#97437 - jyn514:impl-asrawfd-arc, r=dtolnay
`impl<T: AsRawFd> 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() } } ``` In particular, this leads to lots of "multiple applicable items in scope" errors because you have to disambiguate `MyTrait::as_raw_fd` from `AsRawFd::as_raw_fd` at each call site. In generic contexts, when passing the type to a function that takes `impl AsRawFd` it's also sometimes required to use `T: MyTrait + AsRawFd`, which wouldn't be necessary if I could write `MyTrait: AsRawFd`. After this PR, the code can be simpler: ```rust trait MyTrait: AsRawFd {} impl<T: MyTrait> MyTrait for Arc<T> {} ```
2 parents ec21d7e + 86f979b commit 46450ec

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

library/std/src/os/fd/owned.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,34 @@ impl From<OwnedFd> for crate::net::UdpSocket {
355355
))))
356356
}
357357
}
358+
359+
#[stable(feature = "io_safety", since = "1.63.0")]
360+
/// This impl allows implementing traits that require `AsFd` on Arc.
361+
/// ```
362+
/// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg {
363+
/// # #[cfg(target_os = "wasi")]
364+
/// # use std::os::wasi::io::AsFd;
365+
/// # #[cfg(unix)]
366+
/// # use std::os::unix::io::AsFd;
367+
/// use std::net::UdpSocket;
368+
/// use std::sync::Arc;
369+
///
370+
/// trait MyTrait: AsFd {}
371+
/// impl MyTrait for Arc<UdpSocket> {}
372+
/// impl MyTrait for Box<UdpSocket> {}
373+
/// # }
374+
/// ```
375+
impl<T: AsFd> AsFd for crate::sync::Arc<T> {
376+
#[inline]
377+
fn as_fd(&self) -> BorrowedFd<'_> {
378+
(**self).as_fd()
379+
}
380+
}
381+
382+
#[stable(feature = "io_safety", since = "1.63.0")]
383+
impl<T: AsFd> AsFd for Box<T> {
384+
#[inline]
385+
fn as_fd(&self) -> BorrowedFd<'_> {
386+
(**self).as_fd()
387+
}
388+
}

library/std/src/os/fd/raw.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,29 @@ impl<'a> AsRawFd for io::StderrLock<'a> {
222222
libc::STDERR_FILENO
223223
}
224224
}
225+
226+
#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
227+
/// This impl allows implementing traits that require `AsRawFd` on Arc.
228+
/// ```
229+
/// use std::net::UdpSocket;
230+
/// use std::sync::Arc;
231+
/// use std::os::unix::prelude::AsRawFd;
232+
/// trait MyTrait: AsRawFd {
233+
/// }
234+
/// impl MyTrait for Arc<UdpSocket> {}
235+
/// impl MyTrait for Box<UdpSocket> {}
236+
/// ```
237+
impl<T: AsRawFd> AsRawFd for crate::sync::Arc<T> {
238+
#[inline]
239+
fn as_raw_fd(&self) -> RawFd {
240+
(**self).as_raw_fd()
241+
}
242+
}
243+
244+
#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
245+
impl<T: AsRawFd> AsRawFd for Box<T> {
246+
#[inline]
247+
fn as_raw_fd(&self) -> RawFd {
248+
(**self).as_raw_fd()
249+
}
250+
}

0 commit comments

Comments
 (0)