Skip to content

Commit

Permalink
Merge Fd trait implementations for Unix and WASI
Browse files Browse the repository at this point in the history
The AsFd, AsRawFd, FromRawFd and IntoRawFd traits.

For WASI this was not implemented for all public types, now it is
(matching Unix).
  • Loading branch information
Thomasdezeeuw committed Jun 9, 2024
1 parent 50a8a55 commit 8d30e76
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 111 deletions.
6 changes: 1 addition & 5 deletions examples/tcp_listenfd_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ const DATA: &[u8] = b"Hello world!\n";

#[cfg(not(windows))]
fn get_first_listen_fd_listener() -> Option<std::net::TcpListener> {
#[cfg(target_os = "hermit")]
use std::os::hermit::io::FromRawFd;
#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
use std::os::fd::FromRawFd;
#[cfg(target_os = "wasi")]
use std::os::wasi::io::FromRawFd;

let stdlistener = unsafe { std::net::TcpListener::from_raw_fd(3) };
stdlistener.set_nonblocking(true).unwrap();
Expand Down
6 changes: 1 addition & 5 deletions src/io_source.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::ops::{Deref, DerefMut};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::AsRawFd;
#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
use std::os::fd::AsRawFd;
#[cfg(target_os = "wasi")]
use std::os::wasi::io::AsRawFd;
#[cfg(windows)]
use std::os::windows::io::AsRawSocket;
#[cfg(debug_assertions)]
Expand Down
41 changes: 5 additions & 36 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::net::{self, SocketAddr};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::{fmt, io};
Expand Down Expand Up @@ -168,21 +164,21 @@ impl fmt::Debug for TcpListener {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl IntoRawFd for TcpListener {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsRawFd for TcpListener {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl FromRawFd for TcpListener {
/// Converts a `RawFd` to a `TcpListener`.
///
Expand All @@ -195,7 +191,7 @@ impl FromRawFd for TcpListener {
}
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for TcpListener {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
Expand Down Expand Up @@ -229,33 +225,6 @@ impl FromRawSocket for TcpListener {
}
}

#[cfg(target_os = "wasi")]
impl IntoRawFd for TcpListener {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(target_os = "wasi")]
impl AsRawFd for TcpListener {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(target_os = "wasi")]
impl FromRawFd for TcpListener {
/// Converts a `RawFd` to a `TcpListener`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
unsafe fn from_raw_fd(fd: RawFd) -> 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
Expand Down
43 changes: 6 additions & 37 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net::{self, Shutdown, SocketAddr};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};

Expand Down Expand Up @@ -232,7 +228,7 @@ impl TcpStream {
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use std::io;
/// #[cfg(unix)]
/// #[cfg(any(unix, target_os = "wasi"))]
/// use std::os::fd::AsRawFd;
/// #[cfg(windows)]
/// use std::os::windows::io::AsRawSocket;
Expand Down Expand Up @@ -350,21 +346,21 @@ impl fmt::Debug for TcpStream {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl IntoRawFd for TcpStream {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsRawFd for TcpStream {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl FromRawFd for TcpStream {
/// Converts a `RawFd` to a `TcpStream`.
///
Expand All @@ -377,7 +373,7 @@ impl FromRawFd for TcpStream {
}
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for TcpStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
Expand Down Expand Up @@ -411,33 +407,6 @@ impl FromRawSocket for TcpStream {
}
}

#[cfg(target_os = "wasi")]
impl IntoRawFd for TcpStream {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(target_os = "wasi")]
impl AsRawFd for TcpStream {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(target_os = "wasi")]
impl FromRawFd for TcpStream {
/// Converts a `RawFd` to a `TcpStream`.
///
/// # Notes
///
/// The caller is responsible for ensuring that the socket is in
/// non-blocking mode.
unsafe fn from_raw_fd(fd: RawFd) -> 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
Expand Down
24 changes: 10 additions & 14 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@
//!
//! [portability guidelines]: ../struct.Poll.html#portability
use crate::io_source::IoSource;
use crate::{event, sys, Interest, Registry, Token};

use std::fmt;
use std::io;
use std::net;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
use std::{fmt, io, net};

use crate::io_source::IoSource;
use crate::{event, sys, Interest, Registry, Token};

/// A User Datagram Protocol socket.
///
Expand Down Expand Up @@ -574,7 +570,7 @@ impl UdpSocket {
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use std::io;
/// #[cfg(unix)]
/// #[cfg(any(unix, target_os = "wasi"))]
/// use std::os::fd::AsRawFd;
/// #[cfg(windows)]
/// use std::os::windows::io::AsRawSocket;
Expand Down Expand Up @@ -644,21 +640,21 @@ impl fmt::Debug for UdpSocket {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl IntoRawFd for UdpSocket {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsRawFd for UdpSocket {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl FromRawFd for UdpSocket {
/// Converts a `RawFd` to a `UdpSocket`.
///
Expand All @@ -671,7 +667,7 @@ impl FromRawFd for UdpSocket {
}
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for UdpSocket {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
Expand Down
4 changes: 2 additions & 2 deletions src/sys/unix/sourcefd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{event, Interest, Registry, Token};

use std::io;
use std::os::fd::RawFd;

use crate::{event, Interest, Registry, Token};

/// Adapter for [`RawFd`] providing an [`event::Source`] implementation.
///
/// `SourceFd` enables registering any type with an FD with [`Poll`].
Expand Down
4 changes: 2 additions & 2 deletions src/sys/unix/udp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::sys::unix::net::{new_ip_socket, socket_addr};

use std::io;
use std::mem;
use std::net::{self, SocketAddr};
use std::os::fd::{AsRawFd, FromRawFd};

use crate::sys::unix::net::{new_ip_socket, socket_addr};

pub fn bind(addr: SocketAddr) -> io::Result<net::UdpSocket> {
let fd = new_ip_socket(addr, libc::SOCK_DGRAM)?;
let socket = unsafe { net::UdpSocket::from_raw_fd(fd) };
Expand Down
5 changes: 3 additions & 2 deletions src/sys/unix/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
)),
))]
mod fdbased {
use std::io;
use std::os::fd::AsRawFd;

#[cfg(all(
not(mio_unsupported_force_waker_pipe),
any(target_os = "linux", target_os = "android"),
Expand All @@ -35,8 +38,6 @@ mod fdbased {
use crate::sys::unix::waker::pipe::WakerInternal;
use crate::sys::Selector;
use crate::{Interest, Token};
use std::io;
use std::os::fd::AsRawFd;

#[derive(Debug)]
pub struct Waker {
Expand Down
15 changes: 7 additions & 8 deletions tests/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
))]
#![cfg(all(feature = "os-poll", feature = "net"))]

use mio::{event::Source, Events, Interest, Poll, Registry, Token};
use std::{
fs::File,
io, mem,
os::unix::io::{AsRawFd, RawFd},
pin::Pin,
ptr,
};
use std::fs::File;
use std::os::fd::{AsRawFd, RawFd};
use std::pin::Pin;
use std::{io, mem, ptr};

use mio::event::Source;
use mio::{Events, Interest, Poll, Registry, Token};

mod util;
use util::{expect_events, expect_no_events, init, temp_file, ExpectEvent};
Expand Down

0 comments on commit 8d30e76

Please sign in to comment.