Skip to content

Commit 8f23586

Browse files
committed
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 8f23586

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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 = "asraw_ptrs", since = "1.63.0")]
227+
/// This blanket impl allows implementing custom 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 = "asraw_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)