Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the From trait for converting mio socket types into std #1767

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 {
/// Converts a `mio::net::TcpListener` into a `std::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.

let std_listener = net::TcpListener::from_raw_fd(listener.into_raw_fd());

#[cfg(windows)]
let std_listener = net::TcpListener::from_raw_socket(listener.into_raw_socket());

std_listener
}
}
}
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 {
/// Converts a `mio::net::TcpStream` into a `std::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"))]
let std_stream = net::TcpStream::from_raw_fd(stream.into_raw_fd());

#[cfg(windows)]
let std_stream = net::TcpStream::from_raw_socket(stream.into_raw_socket());

std_stream
}
}
}
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 {
/// Converts a `mio::net::UdpSocket` into a `std::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"))]
let std_sock = net::UdpSocket::from_raw_fd(socket.into_raw_fd());

#[cfg(windows)]
let std_sock = net::UdpSocket::from_raw_socket(socket.into_raw_socket());

std_sock
}
}
}