diff --git a/Cargo.toml b/Cargo.toml index 2dda7ca..676656b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,9 +36,6 @@ slab = "0.4.2" socket2 = { version = "0.5.3", features = ["all"] } waker-fn = "1.1.0" -[build-dependencies] -autocfg = "1" - [dev-dependencies] async-channel = "1" async-net = "1" @@ -50,7 +47,7 @@ tempfile = "3" [target.'cfg(target_os = "linux")'.dev-dependencies] inotify = { version = "0.10.1", default-features = false } -timerfd = "1" +timerfd = "1.5" [target.'cfg(windows)'.dev-dependencies] uds_windows = "1" diff --git a/build.rs b/build.rs deleted file mode 100644 index 8292525..0000000 --- a/build.rs +++ /dev/null @@ -1,16 +0,0 @@ -fn main() { - let cfg = match autocfg::AutoCfg::new() { - Ok(cfg) => cfg, - Err(e) => { - println!( - "cargo:warning=async-io: failed to detect compiler features: {}", - e - ); - return; - } - }; - - if !cfg.probe_rustc_version(1, 63) { - autocfg::emit("async_io_no_io_safety"); - } -} diff --git a/examples/unix-signal.rs b/examples/unix-signal.rs index e712893..7f262e8 100644 --- a/examples/unix-signal.rs +++ b/examples/unix-signal.rs @@ -8,7 +8,10 @@ #[cfg(unix)] fn main() -> std::io::Result<()> { - use std::os::unix::{io::AsRawFd, net::UnixStream}; + use std::os::unix::{ + io::{AsFd, AsRawFd}, + net::UnixStream, + }; use async_io::Async; use futures_lite::{future, prelude::*}; @@ -16,7 +19,10 @@ fn main() -> std::io::Result<()> { future::block_on(async { // Create a Unix stream that receives a byte on each signal occurrence. let (a, mut b) = Async::::pair()?; - signal_hook::low_level::pipe::register_raw(signal_hook::consts::SIGINT, a.as_raw_fd())?; + signal_hook::low_level::pipe::register_raw( + signal_hook::consts::SIGINT, + a.as_fd().as_raw_fd(), + )?; println!("Waiting for Ctrl-C..."); // Receive a byte that indicates the Ctrl-C signal occurred. diff --git a/src/lib.rs b/src/lib.rs index 44ec6e4..7e8debb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,19 +64,15 @@ use std::sync::Arc; use std::task::{Context, Poll, Waker}; use std::time::{Duration, Instant}; -#[cfg(all(not(async_io_no_io_safety), unix))] -use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; #[cfg(unix)] use std::{ - os::unix::io::{AsRawFd, RawFd}, + os::unix::io::{AsFd, AsRawFd, BorrowedFd, OwnedFd}, os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream}, path::Path, }; #[cfg(windows)] -use std::os::windows::io::{AsRawSocket, RawSocket}; -#[cfg(all(not(async_io_no_io_safety), windows))] -use std::os::windows::io::{AsSocket, BorrowedSocket, OwnedSocket}; +use std::os::windows::io::{AsRawSocket, AsSocket, BorrowedSocket, OwnedSocket}; use futures_io::{AsyncRead, AsyncWrite}; use futures_lite::stream::{self, Stream}; @@ -619,14 +615,14 @@ pub struct Async { impl Unpin for Async {} #[cfg(unix)] -impl Async { +impl Async { /// Creates an async I/O handle. /// /// This method will put the handle in non-blocking mode and register it in /// [epoll]/[kqueue]/[event ports]/[IOCP]. /// - /// On Unix systems, the handle must implement `AsRawFd`, while on Windows it must implement - /// `AsRawSocket`. + /// On Unix systems, the handle must implement `AsFd`, while on Windows it must implement + /// `AsSocket`. /// /// [epoll]: https://en.wikipedia.org/wiki/Epoll /// [kqueue]: https://en.wikipedia.org/wiki/Kqueue @@ -645,14 +641,9 @@ impl Async { /// # std::io::Result::Ok(()) }); /// ``` pub fn new(io: T) -> io::Result> { - let raw = io.as_raw_fd(); + let fd = io.as_fd(); // Put the file descriptor in non-blocking mode. - // - // Safety: We assume `as_raw_fd()` returns a valid fd. When we can - // depend on Rust >= 1.63, where `AsFd` is stabilized, and when - // `TimerFd` implements it, we can remove this unsafe and simplify this. - let fd = unsafe { rustix::fd::BorrowedFd::borrow_raw(raw) }; cfg_if::cfg_if! { // ioctl(FIONBIO) sets the flag atomically, but we use this only on Linux // for now, as with the standard library, because it seems to behave @@ -672,28 +663,21 @@ impl Async { } Ok(Async { - source: Reactor::get().insert_io(raw)?, + source: Reactor::get().insert_io(fd.as_raw_fd())?, io: Some(io), }) } } #[cfg(unix)] -impl AsRawFd for Async { - fn as_raw_fd(&self) -> RawFd { - self.get_ref().as_raw_fd() - } -} - -#[cfg(all(not(async_io_no_io_safety), unix))] impl AsFd for Async { fn as_fd(&self) -> BorrowedFd<'_> { self.get_ref().as_fd() } } -#[cfg(all(not(async_io_no_io_safety), unix))] -impl> TryFrom for Async { +#[cfg(unix)] +impl> TryFrom for Async { type Error = io::Error; fn try_from(value: OwnedFd) -> Result { @@ -701,7 +685,7 @@ impl> TryFrom for Async { } } -#[cfg(all(not(async_io_no_io_safety), unix))] +#[cfg(unix)] impl> TryFrom> for OwnedFd { type Error = io::Error; @@ -711,14 +695,14 @@ impl> TryFrom> for OwnedFd { } #[cfg(windows)] -impl Async { +impl Async { /// Creates an async I/O handle. /// /// This method will put the handle in non-blocking mode and register it in /// [epoll]/[kqueue]/[event ports]/[IOCP]. /// - /// On Unix systems, the handle must implement `AsRawFd`, while on Windows it must implement - /// `AsRawSocket`. + /// On Unix systems, the handle must implement `AsFd`, while on Windows it must implement + /// `AsSocket`. /// /// [epoll]: https://en.wikipedia.org/wiki/Epoll /// [kqueue]: https://en.wikipedia.org/wiki/Kqueue @@ -737,8 +721,7 @@ impl Async { /// # std::io::Result::Ok(()) }); /// ``` pub fn new(io: T) -> io::Result> { - let sock = io.as_raw_socket(); - let borrowed = unsafe { rustix::fd::BorrowedFd::borrow_raw(sock) }; + let borrowed = io.as_socket(); // Put the socket in non-blocking mode. // @@ -748,28 +731,21 @@ impl Async { rustix::io::ioctl_fionbio(borrowed, true)?; Ok(Async { - source: Reactor::get().insert_io(sock)?, + source: Reactor::get().insert_io(borrowed.as_raw_socket())?, io: Some(io), }) } } #[cfg(windows)] -impl AsRawSocket for Async { - fn as_raw_socket(&self) -> RawSocket { - self.get_ref().as_raw_socket() - } -} - -#[cfg(all(not(async_io_no_io_safety), windows))] impl AsSocket for Async { fn as_socket(&self) -> BorrowedSocket<'_> { self.get_ref().as_socket() } } -#[cfg(all(not(async_io_no_io_safety), windows))] -impl> TryFrom for Async { +#[cfg(windows)] +impl> TryFrom for Async { type Error = io::Error; fn try_from(value: OwnedSocket) -> Result { @@ -777,7 +753,7 @@ impl> TryFrom for Async { } } -#[cfg(all(not(async_io_no_io_safety), windows))] +#[cfg(windows)] impl> TryFrom> for OwnedSocket { type Error = io::Error;