Skip to content

Commit

Permalink
Provide implementations for I/O safety traits unconditionally (#244)
Browse files Browse the repository at this point in the history
* Provide implementations for I/O safety traits unconditionally

These traits have been stabilized with Rust 1.63. Since that is now our MSRV, we can drop the
`build.rs` as well as the `autocfg` build dependency.
While at it, I've also added a few `use` statements to make the implemenations more readable.

* Make `AsFd`/`AsRawFd` implementations available on wasi

`std` provides these traits on wasi, so we should probably do so as well.
  • Loading branch information
LingMan authored Jul 5, 2023
1 parent 74f9fbc commit da36cd5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@ redox_syscall = "0.3"
[dev-dependencies]
doc-comment = "0.3"

[build-dependencies]
autocfg = "1"

[features]
nightly = []
10 changes: 0 additions & 10 deletions build.rs

This file was deleted.

37 changes: 17 additions & 20 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::mem;
use std::ops::Deref;
#[cfg(unix)]
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle, RawHandle};
use std::path::{Path, PathBuf};

use crate::error::IoResultExt;
Expand Down Expand Up @@ -1034,42 +1040,33 @@ impl Seek for &NamedTempFile<File> {
}
}

#[cfg(all(fd, unix))]
impl<F: std::os::unix::io::AsFd> std::os::unix::io::AsFd for NamedTempFile<F> {
fn as_fd(&self) -> std::os::unix::io::BorrowedFd<'_> {
#[cfg(any(unix, target_os = "wasi"))]
impl<F: AsFd> AsFd for NamedTempFile<F> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.as_file().as_fd()
}
}

#[cfg(unix)]
impl<F> std::os::unix::io::AsRawFd for NamedTempFile<F>
where
F: std::os::unix::io::AsRawFd,
{
#[cfg(any(unix, target_os = "wasi"))]
impl<F: AsRawFd> AsRawFd for NamedTempFile<F> {
#[inline]
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
fn as_raw_fd(&self) -> RawFd {
self.as_file().as_raw_fd()
}
}

#[cfg(all(fd, windows))]
impl<F> std::os::windows::io::AsHandle for NamedTempFile<F>
where
F: std::os::windows::io::AsHandle,
{
#[cfg(windows)]
impl<F: AsHandle> AsHandle for NamedTempFile<F> {
#[inline]
fn as_handle(&self) -> std::os::windows::io::BorrowedHandle<'_> {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.as_file().as_handle()
}
}

#[cfg(windows)]
impl<F> std::os::windows::io::AsRawHandle for NamedTempFile<F>
where
F: std::os::windows::io::AsRawHandle,
{
impl<F: AsRawHandle> AsRawHandle for NamedTempFile<F> {
#[inline]
fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
fn as_raw_handle(&self) -> RawHandle {
self.as_file().as_raw_handle()
}
}
Expand Down

0 comments on commit da36cd5

Please sign in to comment.