diff --git a/src/net/tcp/listener.rs b/src/net/tcp/listener.rs index 962af2603..2ed7ec85e 100644 --- a/src/net/tcp/listener.rs +++ b/src/net/tcp/listener.rs @@ -248,3 +248,21 @@ impl FromRawFd for TcpListener { TcpListener::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl From for net::TcpListener { + fn from(listener: TcpListener) -> Self { + // Safety: This is safe since we are extracting the raw fd from a well-constructed + // mio::net::TcpListener which ensures that we actually pass in a valid file + // descriptor/socket + unsafe { + #[cfg(any(unix, target_os = "hermit", target_os = "wasi"))] + { + net::TcpListener::from_raw_fd(listener.into_raw_fd()) + } + #[cfg(windows)] + { + net::TcpListener::from_raw_socket(listener.into_raw_socket()) + } + } + } +} diff --git a/src/net/tcp/stream.rs b/src/net/tcp/stream.rs index 608f95e99..8c2fba61d 100644 --- a/src/net/tcp/stream.rs +++ b/src/net/tcp/stream.rs @@ -430,3 +430,21 @@ impl FromRawFd for TcpStream { TcpStream::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl From for net::TcpStream { + fn from(stream: TcpStream) -> Self { + // Safety: This is safe since we are extracting the raw fd from a well-constructed + // mio::net::TcpStream which ensures that we actually pass in a valid file + // descriptor/socket + unsafe { + #[cfg(any(unix, target_os = "hermit", target_os = "wasi"))] + { + net::TcpStream::from_raw_fd(stream.into_raw_fd()) + } + #[cfg(windows)] + { + net::TcpStream::from_raw_socket(stream.into_raw_socket()) + } + } + } +} diff --git a/src/net/udp.rs b/src/net/udp.rs index afde3f884..4fa65cf83 100644 --- a/src/net/udp.rs +++ b/src/net/udp.rs @@ -697,3 +697,21 @@ impl FromRawSocket for UdpSocket { UdpSocket::from_std(FromRawSocket::from_raw_socket(socket)) } } + +impl From for net::UdpSocket { + fn from(socket: UdpSocket) -> Self { + // Safety: This is safe since we are extracting the raw fd from a well-constructed + // mio::net::UdpSocket which ensures that we actually pass in a valid file + // descriptor/socket + unsafe { + #[cfg(any(unix, target_os = "hermit", target_os = "wasi"))] + { + net::UdpSocket::from_raw_fd(socket.into_raw_fd()) + } + #[cfg(windows)] + { + net::UdpSocket::from_raw_socket(socket.into_raw_socket()) + } + } + } +} diff --git a/src/net/uds/datagram.rs b/src/net/uds/datagram.rs index e963d6e2f..4d8ff82f3 100644 --- a/src/net/uds/datagram.rs +++ b/src/net/uds/datagram.rs @@ -234,3 +234,12 @@ impl FromRawFd for UnixDatagram { UnixDatagram::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl From for net::UnixDatagram { + fn from(datagram: UnixDatagram) -> Self { + // Safety: This is safe since we are extracting the raw fd from a well-constructed + // mio::net::uds::UnixListener which ensures that we actually pass in a valid file + // descriptor/socket + unsafe { net::UnixDatagram::from_raw_fd(datagram.into_raw_fd()) } + } +} diff --git a/src/net/uds/listener.rs b/src/net/uds/listener.rs index eeffe042e..da1605375 100644 --- a/src/net/uds/listener.rs +++ b/src/net/uds/listener.rs @@ -107,3 +107,12 @@ impl FromRawFd for UnixListener { UnixListener::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl From for net::UnixListener { + fn from(listener: UnixListener) -> Self { + // Safety: This is safe since we are extracting the raw fd from a well-constructed + // mio::net::uds::UnixListener which ensures that we actually pass in a valid file + // descriptor/socket + unsafe { net::UnixListener::from_raw_fd(listener.into_raw_fd()) } + } +} diff --git a/src/net/uds/stream.rs b/src/net/uds/stream.rs index 1c17d84a1..70b5085b3 100644 --- a/src/net/uds/stream.rs +++ b/src/net/uds/stream.rs @@ -252,3 +252,12 @@ impl FromRawFd for UnixStream { UnixStream::from_std(FromRawFd::from_raw_fd(fd)) } } + +impl From for net::UnixStream { + fn from(stream: UnixStream) -> Self { + // Safety: This is safe since we are extracting the raw fd from a well-constructed + // mio::net::uds::UnixStream which ensures that we actually pass in a valid file + // descriptor/socket + unsafe { net::UnixStream::from_raw_fd(stream.into_raw_fd()) } + } +}