Skip to content

Commit

Permalink
impl<T: AsFd> AsFd for {Arc,Box}<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Jun 22, 2022
1 parent ed1e351 commit cf483a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
31 changes: 31 additions & 0 deletions library/std/src/os/fd/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,34 @@ impl From<OwnedFd> for crate::net::UdpSocket {
))))
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
/// This impl allows implementing traits that require `AsFd` on Arc.
/// ```
/// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg {
/// # #[cfg(target_os = "wasi")]
/// # use std::os::wasi::io::AsFd;
/// # #[cfg(unix)]
/// # use std::os::unix::io::AsFd;
/// use std::net::UdpSocket;
/// use std::sync::Arc;
///
/// trait MyTrait: AsFd {}
/// impl MyTrait for Arc<UdpSocket> {}
/// impl MyTrait for Box<UdpSocket> {}
/// # }
/// ```
impl<T: AsFd> AsFd for crate::sync::Arc<T> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
(**self).as_fd()
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl<T: AsFd> AsFd for Box<T> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
(**self).as_fd()
}
}
5 changes: 2 additions & 3 deletions library/std/src/os/fd/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ impl<'a> AsRawFd for io::StderrLock<'a> {
}
}

#[stable(feature = "asraw_ptrs", since = "1.63.0")]
/// This blanket impl allows implementing custom traits that require `AsRawFd` on Arc.
/// This impl allows implementing traits that require `AsRawFd` on Arc.
/// ```
/// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg {
/// # #[cfg(target_os = "wasi")]
Expand All @@ -247,7 +246,7 @@ impl<T: AsRawFd> AsRawFd for crate::sync::Arc<T> {
}
}

#[stable(feature = "asraw_ptrs", since = "1.63.0")]
#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
impl<T: AsRawFd> AsRawFd for Box<T> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
Expand Down

0 comments on commit cf483a1

Please sign in to comment.