Skip to content

Commit ed1e351

Browse files
committedJun 22, 2022
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() } } ```
1 parent 3a8b014 commit ed1e351

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
 

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.