diff --git a/src/net/tcp/listener.rs b/src/net/tcp/listener.rs index df51d57ae..80bc2afa1 100644 --- a/src/net/tcp/listener.rs +++ b/src/net/tcp/listener.rs @@ -193,6 +193,13 @@ impl FromRawFd for TcpListener { } } +#[cfg(unix)] +impl std::os::fd::AsFd for TcpListener { + fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> { + unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) } + } +} + #[cfg(windows)] impl IntoRawSocket for TcpListener { fn into_raw_socket(self) -> RawSocket { diff --git a/src/net/tcp/stream.rs b/src/net/tcp/stream.rs index 503f1b81e..623f46597 100644 --- a/src/net/tcp/stream.rs +++ b/src/net/tcp/stream.rs @@ -375,6 +375,13 @@ impl FromRawFd for TcpStream { } } +#[cfg(unix)] +impl std::os::fd::AsFd for TcpStream { + fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> { + unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) } + } +} + #[cfg(windows)] impl IntoRawSocket for TcpStream { fn into_raw_socket(self) -> RawSocket { diff --git a/src/net/uds/datagram.rs b/src/net/uds/datagram.rs index e963d6e2f..ba955b6bf 100644 --- a/src/net/uds/datagram.rs +++ b/src/net/uds/datagram.rs @@ -234,3 +234,9 @@ impl FromRawFd for UnixDatagram { UnixDatagram::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl std::os::fd::AsFd for UnixDatagram { + fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> { + unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) } + } +} diff --git a/src/net/uds/listener.rs b/src/net/uds/listener.rs index eeffe042e..9f36936a6 100644 --- a/src/net/uds/listener.rs +++ b/src/net/uds/listener.rs @@ -107,3 +107,9 @@ impl FromRawFd for UnixListener { UnixListener::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl std::os::fd::AsFd for UnixListener { + fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> { + unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) } + } +} diff --git a/src/net/uds/stream.rs b/src/net/uds/stream.rs index 1c17d84a1..6801ffa52 100644 --- a/src/net/uds/stream.rs +++ b/src/net/uds/stream.rs @@ -252,3 +252,9 @@ impl FromRawFd for UnixStream { UnixStream::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl std::os::fd::AsFd for UnixStream { + fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> { + unsafe { std::os::fd::BorrowedFd::borrow_raw(self.inner.as_raw_fd()) } + } +}