implement I/O safety traits (#13)

This commit is contained in:
John Nunley 2022-08-17 12:26:01 -07:00 committed by GitHub
parent 4ba3790808
commit edccb4d3ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 0 deletions

View File

@ -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"

16
build.rs Normal file
View File

@ -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");
}
}

View File

@ -1091,6 +1091,34 @@ impl std::os::windows::io::AsRawHandle for File {
}
}
#[cfg(all(not(async_fs_no_io_safety), unix))]
impl From<std::os::unix::io::OwnedFd> 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<std::os::windows::io::OwnedHandle> 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>,