Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,21 @@ impl FromRawFd for TcpListener {
TcpListener::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<TcpListener> 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"))]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently fails to build on WASI. Can you try the following, to see if that fixes it? You might need to add use std::os::wasi::io::{FromRawFd, IntoRawFd}; to some files.

Suggested change
#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]

Copy link
Contributor Author

@tglane tglane Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. With that addition the lib can be built for the target wasm32-wasi on my machine.

Edit: We still get failing CI tasks but they do not seem to be related to the changes made in this PR since the same tasks fail in other MR as well.

{
net::TcpListener::from_raw_fd(listener.into_raw_fd())
}
#[cfg(windows)]
{
net::TcpListener::from_raw_socket(listener.into_raw_socket())
}
}
}
}
18 changes: 18 additions & 0 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,21 @@ impl FromRawFd for TcpStream {
TcpStream::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<TcpStream> 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"))]
{
net::TcpStream::from_raw_fd(stream.into_raw_fd())
}
#[cfg(windows)]
{
net::TcpStream::from_raw_socket(stream.into_raw_socket())
}
}
}
}
18 changes: 18 additions & 0 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,21 @@ impl FromRawSocket for UdpSocket {
UdpSocket::from_std(FromRawSocket::from_raw_socket(socket))
}
}

impl From<UdpSocket> 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"))]
{
net::UdpSocket::from_raw_fd(socket.into_raw_fd())
}
#[cfg(windows)]
{
net::UdpSocket::from_raw_socket(socket.into_raw_socket())
}
}
}
}
9 changes: 9 additions & 0 deletions src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,12 @@ impl FromRawFd for UnixDatagram {
UnixDatagram::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<UnixDatagram> 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()) }
}
}
9 changes: 9 additions & 0 deletions src/net/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,12 @@ impl FromRawFd for UnixListener {
UnixListener::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<UnixListener> 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()) }
}
}
9 changes: 9 additions & 0 deletions src/net/uds/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,12 @@ impl FromRawFd for UnixStream {
UnixStream::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<UnixStream> 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()) }
}
}