From e5d8b31f6cb9c2266f7c5acdc71b885c607b208b Mon Sep 17 00:00:00 2001 From: jtnunley Date: Wed, 17 Aug 2022 12:15:42 -0700 Subject: [PATCH] implement I/O safety traits --- Cargo.toml | 3 +++ build.rs | 16 ++++++++++++++++ src/lib.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index c3fedd6..8a00ad3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,9 @@ async-lock = "2.3.0" blocking = "1.0.0" futures-lite = "1.2.0" +[build-dependencies] +autocfg = "1" + [target.'cfg(unix)'.dev-dependencies] libc = "0.2.78" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..10a25b1 --- /dev/null +++ b/build.rs @@ -0,0 +1,16 @@ +fn main() { + let cfg = match autocfg::AutoCfg::new() { + Ok(cfg) => cfg, + Err(e) => { + println!( + "cargo:warning=async-fs: failed to detect compiler features: {}", + e + ); + return; + } + }; + + if !cfg.probe_rustc_version(1, 63) { + autocfg::emit("async_fs_no_io_safety"); + } +} diff --git a/src/lib.rs b/src/lib.rs index 047759c..c524789 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1091,6 +1091,34 @@ impl std::os::windows::io::AsRawHandle for File { } } +#[cfg(all(not(async_fs_no_io_safety), unix))] +impl From for File { + fn from(fd: std::os::unix::io::OwnedFd) -> Self { + File::from(std::fs::File::from(fd)) + } +} + +#[cfg(all(not(async_fs_no_io_safety), windows))] +impl From for File { + fn from(fd: std::os::windows::io::OwnedHandle) -> Self { + File::from(std::fs::File::from(fd)) + } +} + +#[cfg(all(not(async_fs_no_io_safety), unix))] +impl std::os::unix::io::AsFd for File { + fn as_fd(&self) -> std::os::unix::io::BorrowedFd<'_> { + self.file.as_fd() + } +} + +#[cfg(all(not(async_fs_no_io_safety), windows))] +impl std::os::windows::io::AsHandle for File { + fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> { + self.file.as_handle() + } +} + impl AsyncRead for File { fn poll_read( mut self: Pin<&mut Self>,