Skip to content

Commit b04bfb4

Browse files
committed
Auto merge of #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 f99f9e4 + cf483a1 commit b04bfb4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

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

+31
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

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

0 commit comments

Comments
 (0)