From 5f4ff2b1d048041c1f139ccfab321c7bfa80b6a3 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Thu, 11 Aug 2022 12:51:06 -0700 Subject: [PATCH 1/6] implement io-safe traits --- Cargo.toml | 3 +++ src/lib.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0496986..9459781 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,9 @@ libc = "0.2.77" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.9", features = ["winsock2"] } +[features] +io_safety = [] + [dev-dependencies] async-channel = "1" async-net = "1" diff --git a/src/lib.rs b/src/lib.rs index 29c3f5e..a79c11e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,9 +70,13 @@ use std::{ os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream}, path::Path, }; +#[cfg(all(feature = "io_safety", unix))] +use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; #[cfg(windows)] use std::os::windows::io::{AsRawSocket, RawSocket}; +#[cfg(all(feature = "io_safety", windows))] +use std::os::windows::io::{AsSocket, BorrowedSocket, OwnedSocket}; use futures_lite::io::{AsyncRead, AsyncWrite}; use futures_lite::stream::{self, Stream}; @@ -552,6 +556,31 @@ impl AsRawFd for Async { } } +#[cfg(all(feature = "io_safety", unix))] +impl AsFd for Async { + fn as_fd(&self) -> BorrowedFd<'_> { + self.get_ref().as_fd() + } +} + +#[cfg(all(feature = "io_safety", unix))] +impl> TryFrom for Async { + type Error = io::Error; + + fn try_from(value: OwnedFd) -> Result { + Async::new(value.into()) + } +} + +#[cfg(all(feature = "io_safety", unix))] +impl> TryFrom> for OwnedFd { + type Error = io::Error; + + fn try_from(value: Async) -> Result { + value.into_inner().map(Into::into) + } +} + #[cfg(windows)] impl Async { /// Creates an async I/O handle. @@ -612,6 +641,31 @@ impl AsRawSocket for Async { } } +#[cfg(all(feature = "io_safety", windows))] +impl AsSocket for Async { + fn as_socket(&self) -> BorrowedSocket<'_> { + self.get_ref().as_socket() + } +} + +#[cfg(all(feature = "io_safety", windows))] +impl> TryFrom for Async { + type Error = io::Error; + + fn try_from(value: OwnedSocket) -> Result { + Async::new(value.into()) + } +} + +#[cfg(all(feature = "io_safety", windows))] +impl> TryFrom> for OwnedSocket { + type Error = io::Error; + + fn try_from(value: Async) -> Result { + value.into_inner().map(Into::into) + } +} + impl Async { /// Gets a reference to the inner I/O handle. /// From e04819add35e99311c3d86773985324df44af1bc Mon Sep 17 00:00:00 2001 From: jtnunley Date: Thu, 11 Aug 2022 12:54:46 -0700 Subject: [PATCH 2/6] axe unneeded guards --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a79c11e..2b7a459 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -573,7 +573,7 @@ impl> TryFrom for Async { } #[cfg(all(feature = "io_safety", unix))] -impl> TryFrom> for OwnedFd { +impl> TryFrom> for OwnedFd { type Error = io::Error; fn try_from(value: Async) -> Result { @@ -658,7 +658,7 @@ impl> TryFrom for Async { } #[cfg(all(feature = "io_safety", windows))] -impl> TryFrom> for OwnedSocket { +impl> TryFrom> for OwnedSocket { type Error = io::Error; fn try_from(value: Async) -> Result { From b78097297a63125549aaedc52a037965981a462c Mon Sep 17 00:00:00 2001 From: jtnunley Date: Thu, 11 Aug 2022 13:01:19 -0700 Subject: [PATCH 3/6] fmt --- src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2b7a459..dbd853e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,14 +64,14 @@ use std::sync::Arc; use std::task::{Context, Poll, Waker}; use std::time::{Duration, Instant}; +#[cfg(all(feature = "io_safety", unix))] +use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; #[cfg(unix)] use std::{ os::unix::io::{AsRawFd, RawFd}, os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream}, path::Path, }; -#[cfg(all(feature = "io_safety", unix))] -use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; #[cfg(windows)] use std::os::windows::io::{AsRawSocket, RawSocket}; @@ -569,7 +569,7 @@ impl> TryFrom for Async { fn try_from(value: OwnedFd) -> Result { Async::new(value.into()) - } + } } #[cfg(all(feature = "io_safety", unix))] @@ -578,7 +578,7 @@ impl> TryFrom> for OwnedFd { fn try_from(value: Async) -> Result { value.into_inner().map(Into::into) - } + } } #[cfg(windows)] @@ -654,7 +654,7 @@ impl> TryFrom for Async { fn try_from(value: OwnedSocket) -> Result { Async::new(value.into()) - } + } } #[cfg(all(feature = "io_safety", windows))] @@ -663,7 +663,7 @@ impl> TryFrom> for OwnedSocket { fn try_from(value: Async) -> Result { value.into_inner().map(Into::into) - } + } } impl Async { From 1ece1535f629103e353cac0998be71820564e26a Mon Sep 17 00:00:00 2001 From: jtnunley Date: Sat, 13 Aug 2022 17:37:02 -0700 Subject: [PATCH 4/6] use autocfg --- Cargo.toml | 6 +++--- build.rs | 18 ++++++++++++++++++ src/lib.rs | 16 ++++++++-------- 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index 9459781..1eda3bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,15 +25,15 @@ slab = "0.4.2" socket2 = { version = "0.4.2", features = ["all"] } waker-fn = "1.1.0" +[build-dependencies] +autocfg = "1" + [target."cfg(unix)".dependencies] libc = "0.2.77" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.9", features = ["winsock2"] } -[features] -io_safety = [] - [dev-dependencies] async-channel = "1" async-net = "1" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..0d95e13 --- /dev/null +++ b/build.rs @@ -0,0 +1,18 @@ +const TEST_EXPR: &str = "{ +#[cfg(unix)] +fn _test(_: I) {} +#[cfg(unix)] +fn _test2(_: std::os::unix::io::OwnedFd) {} +#[cfg(windows)] +fn _test(_: I) {} +#[cfg(windows)] +fn _test2(_: std::os::windows::io::OwnedSocket) {} +}"; + +fn main() { + let cfg = autocfg::new(); + + if cfg.probe_expression(TEST_EXPR) { + autocfg::emit("has_io_safety"); + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index dbd853e..1abf742 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ use std::sync::Arc; use std::task::{Context, Poll, Waker}; use std::time::{Duration, Instant}; -#[cfg(all(feature = "io_safety", unix))] +#[cfg(all(has_io_safety, unix))] use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; #[cfg(unix)] use std::{ @@ -75,7 +75,7 @@ use std::{ #[cfg(windows)] use std::os::windows::io::{AsRawSocket, RawSocket}; -#[cfg(all(feature = "io_safety", windows))] +#[cfg(all(has_io_safety, windows))] use std::os::windows::io::{AsSocket, BorrowedSocket, OwnedSocket}; use futures_lite::io::{AsyncRead, AsyncWrite}; @@ -556,14 +556,14 @@ impl AsRawFd for Async { } } -#[cfg(all(feature = "io_safety", unix))] +#[cfg(all(has_io_safety, unix))] impl AsFd for Async { fn as_fd(&self) -> BorrowedFd<'_> { self.get_ref().as_fd() } } -#[cfg(all(feature = "io_safety", unix))] +#[cfg(all(has_io_safety, unix))] impl> TryFrom for Async { type Error = io::Error; @@ -572,7 +572,7 @@ impl> TryFrom for Async { } } -#[cfg(all(feature = "io_safety", unix))] +#[cfg(all(has_io_safety, unix))] impl> TryFrom> for OwnedFd { type Error = io::Error; @@ -641,14 +641,14 @@ impl AsRawSocket for Async { } } -#[cfg(all(feature = "io_safety", windows))] +#[cfg(all(has_io_safety, windows))] impl AsSocket for Async { fn as_socket(&self) -> BorrowedSocket<'_> { self.get_ref().as_socket() } } -#[cfg(all(feature = "io_safety", windows))] +#[cfg(all(has_io_safety, windows))] impl> TryFrom for Async { type Error = io::Error; @@ -657,7 +657,7 @@ impl> TryFrom for Async { } } -#[cfg(all(feature = "io_safety", windows))] +#[cfg(all(has_io_safety, windows))] impl> TryFrom> for OwnedSocket { type Error = io::Error; From 6be69a89466b0fb15a63eb30161c47fa2a9b8311 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Mon, 15 Aug 2022 06:28:08 -0700 Subject: [PATCH 5/6] fmt build file --- build.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 0d95e13..14664a3 100644 --- a/build.rs +++ b/build.rs @@ -11,8 +11,8 @@ fn _test2(_: std::os::windows::io::OwnedSocket) {} fn main() { let cfg = autocfg::new(); - + if cfg.probe_expression(TEST_EXPR) { autocfg::emit("has_io_safety"); } -} \ No newline at end of file +} From 0be4a7dfd552ca35c5424137f393647e65593350 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Wed, 17 Aug 2022 11:46:59 -0700 Subject: [PATCH 6/6] fix autocfg instances --- build.rs | 26 ++++++++++++-------------- src/lib.rs | 16 ++++++++-------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/build.rs b/build.rs index 14664a3..8292525 100644 --- a/build.rs +++ b/build.rs @@ -1,18 +1,16 @@ -const TEST_EXPR: &str = "{ -#[cfg(unix)] -fn _test(_: I) {} -#[cfg(unix)] -fn _test2(_: std::os::unix::io::OwnedFd) {} -#[cfg(windows)] -fn _test(_: I) {} -#[cfg(windows)] -fn _test2(_: std::os::windows::io::OwnedSocket) {} -}"; - fn main() { - let cfg = autocfg::new(); + 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_expression(TEST_EXPR) { - autocfg::emit("has_io_safety"); + if !cfg.probe_rustc_version(1, 63) { + autocfg::emit("async_io_no_io_safety"); } } diff --git a/src/lib.rs b/src/lib.rs index 1abf742..de6d526 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ use std::sync::Arc; use std::task::{Context, Poll, Waker}; use std::time::{Duration, Instant}; -#[cfg(all(has_io_safety, unix))] +#[cfg(all(not(async_io_no_io_safety), unix))] use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; #[cfg(unix)] use std::{ @@ -75,7 +75,7 @@ use std::{ #[cfg(windows)] use std::os::windows::io::{AsRawSocket, RawSocket}; -#[cfg(all(has_io_safety, windows))] +#[cfg(all(not(async_io_no_io_safety), windows))] use std::os::windows::io::{AsSocket, BorrowedSocket, OwnedSocket}; use futures_lite::io::{AsyncRead, AsyncWrite}; @@ -556,14 +556,14 @@ impl AsRawFd for Async { } } -#[cfg(all(has_io_safety, unix))] +#[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(has_io_safety, unix))] +#[cfg(all(not(async_io_no_io_safety), unix))] impl> TryFrom for Async { type Error = io::Error; @@ -572,7 +572,7 @@ impl> TryFrom for Async { } } -#[cfg(all(has_io_safety, unix))] +#[cfg(all(not(async_io_no_io_safety), unix))] impl> TryFrom> for OwnedFd { type Error = io::Error; @@ -641,14 +641,14 @@ impl AsRawSocket for Async { } } -#[cfg(all(has_io_safety, windows))] +#[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(has_io_safety, windows))] +#[cfg(all(not(async_io_no_io_safety), windows))] impl> TryFrom for Async { type Error = io::Error; @@ -657,7 +657,7 @@ impl> TryFrom for Async { } } -#[cfg(all(has_io_safety, windows))] +#[cfg(all(not(async_io_no_io_safety), windows))] impl> TryFrom> for OwnedSocket { type Error = io::Error;