Skip to content

Commit

Permalink
Implemented the From trait for converting mio socket types into std (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tglane authored Mar 29, 2024
1 parent cc0b4fe commit a09e036
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
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", target_os = "wasi"))]
{
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", target_os = "wasi"))]
{
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", target_os = "wasi"))]
{
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()) }
}
}

0 comments on commit a09e036

Please sign in to comment.