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

Implement I/O safety traits #1745

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 2 additions & 6 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)]
use std::os::unix::io::FromRawFd;
#[cfg(target_os = "wasi")]
use std::os::wasi::io::FromRawFd;
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
use std::os::fd::FromRawFd;

let stdlistener = unsafe { std::net::TcpListener::from_raw_fd(3) };
stdlistener.set_nonblocking(true).unwrap();
Expand Down
10 changes: 3 additions & 7 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)]
use std::os::unix::io::AsRawFd;
#[cfg(target_os = "wasi")]
use std::os::wasi::io::AsRawFd;
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
use std::os::fd::AsRawFd;
#[cfg(windows)]
use std::os::windows::io::AsRawSocket;
#[cfg(debug_assertions)]
Expand All @@ -23,7 +19,7 @@ use crate::{event, Interest, Registry, Token};
/// Mio supports registering any FD or socket that can be registered with the
/// underlying OS selector. `IoSource` provides the necessary bridge.
///
/// [`RawFd`]: std::os::unix::io::RawFd
/// [`RawFd`]: std::os::fd::RawFd
/// [`RawSocket`]: std::os::windows::io::RawSocket
///
/// # Notes
Expand Down
48 changes: 12 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)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[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};
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,6 +191,13 @@ impl FromRawFd for TcpListener {
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for TcpListener {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpListener {
fn into_raw_socket(self) -> RawSocket {
Expand Down Expand Up @@ -222,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
52 changes: 14 additions & 38 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)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[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};

Expand Down Expand Up @@ -232,8 +228,8 @@ impl TcpStream {
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use std::io;
/// #[cfg(unix)]
/// use std::os::unix::io::AsRawFd;
/// #[cfg(any(unix, target_os = "wasi"))]
/// use std::os::fd::AsRawFd;
/// #[cfg(windows)]
/// use std::os::windows::io::AsRawSocket;
/// use mio::net::TcpStream;
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,6 +373,13 @@ impl FromRawFd for TcpStream {
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for TcpStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

#[cfg(windows)]
impl IntoRawSocket for TcpStream {
fn into_raw_socket(self) -> RawSocket {
Expand Down Expand Up @@ -404,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
33 changes: 18 additions & 15 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)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[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,8 +570,8 @@ impl UdpSocket {
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use std::io;
/// #[cfg(unix)]
/// use std::os::unix::io::AsRawFd;
/// #[cfg(any(unix, target_os = "wasi"))]
/// use std::os::fd::AsRawFd;
/// #[cfg(windows)]
/// use std::os::windows::io::AsRawSocket;
/// use mio::net::UdpSocket;
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,6 +667,13 @@ impl FromRawFd for UdpSocket {
}
}

#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
impl AsFd for UdpSocket {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}

#[cfg(windows)]
impl IntoRawSocket for UdpSocket {
fn into_raw_socket(self) -> RawSocket {
Expand Down
16 changes: 11 additions & 5 deletions src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::io_source::IoSource;
use crate::{event, sys, Interest, Registry, Token};

use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;
use std::{fmt, io};

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

/// A Unix datagram socket.
pub struct UnixDatagram {
inner: IoSource<net::UnixDatagram>,
Expand Down Expand Up @@ -135,7 +135,7 @@ impl UnixDatagram {
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use std::io;
/// use std::os::unix::io::AsRawFd;
/// use std::os::fd::AsRawFd;
/// use mio::net::UnixDatagram;
///
/// let (dgram1, dgram2) = UnixDatagram::pair()?;
Expand Down Expand Up @@ -248,3 +248,9 @@ impl From<UnixDatagram> for net::UnixDatagram {
unsafe { net::UnixDatagram::from_raw_fd(datagram.into_raw_fd()) }
}
}

impl AsFd for UnixDatagram {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}
8 changes: 7 additions & 1 deletion src/net/uds/listener.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;
use std::{fmt, io};
Expand Down Expand Up @@ -117,3 +117,9 @@ impl From<UnixListener> for net::UnixListener {
unsafe { net::UnixListener::from_raw_fd(listener.into_raw_fd()) }
}
}

impl AsFd for UnixListener {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}
16 changes: 11 additions & 5 deletions src/net/uds/stream.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::io_source::IoSource;
use crate::{event, sys, Interest, Registry, Token};

use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net::Shutdown;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net::{self, SocketAddr};
use std::path::Path;

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

/// A non-blocking Unix stream socket.
pub struct UnixStream {
inner: IoSource<net::UnixStream>,
Expand Down Expand Up @@ -100,7 +100,7 @@ impl UnixStream {
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use std::io;
/// use std::os::unix::io::AsRawFd;
/// use std::os::fd::AsRawFd;
/// use mio::net::UnixStream;
///
/// let (stream1, stream2) = UnixStream::pair()?;
Expand Down Expand Up @@ -261,3 +261,9 @@ impl From<UnixStream> for net::UnixStream {
unsafe { net::UnixStream::from_raw_fd(stream.into_raw_fd()) }
}
}

impl AsFd for UnixStream {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner.as_fd()
}
}
2 changes: 1 addition & 1 deletion src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
target_os = "vita"
)),
))]
use std::os::unix::io::{AsRawFd, RawFd};
use std::os::fd::{AsRawFd, RawFd};
#[cfg(all(debug_assertions, not(target_os = "wasi")))]
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(all(debug_assertions, not(target_os = "wasi")))]
Expand Down
Loading