From 7b9e0f1c64b4157a92947f52142cd5d7a9460982 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Wed, 17 Aug 2022 12:07:36 -0700 Subject: [PATCH] implement I/O safety traits --- .github/workflows/ci.yml | 7 +++++++ Cargo.toml | 4 ++++ src/file.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b782162..eab4981 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,3 +53,10 @@ jobs: command: clippy args: --tests -- -D warnings if: matrix.rust_version == 'stable' + + - name: cargo check --features io_safety + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --features io_safety + if: matrix.rust_version == 'stable' diff --git a/Cargo.toml b/Cargo.toml index 371096a..74f8e33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,10 @@ exclude = [".github", ".gitignore", "README.tpl"] [dev-dependencies] serde_json = "1.0.64" +[features] +# Adds I/O safety traits introduced in Rust 1.63 +io_safety = [] + [package.metadata.release] tag-name = "{{version}}" sign-tag = true diff --git a/src/file.rs b/src/file.rs index b97729c..098e7dd 100644 --- a/src/file.rs +++ b/src/file.rs @@ -284,6 +284,23 @@ mod unix { .map_err(|err| self.error(err, ErrorKind::WriteAt)) } } + + #[cfg(feature = "io_safety")] + mod io_safety { + use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; + + impl AsFd for crate::File { + fn as_fd(&self) -> BorrowedFd<'_> { + self.file().as_fd() + } + } + + impl From for OwnedFd { + fn from(file: crate::File) -> Self { + file.into_parts().0.into() + } + } + } } #[cfg(windows)] @@ -325,4 +342,21 @@ mod windows { self.file.into_raw_handle() } } + + #[cfg(feature = "io_safety")] + mod io_safety { + use std::os::windows::io::{AsHandle, BorrowedHandle, OwnedHandle}; + + impl AsHandle for crate::File { + fn as_handle(&self) -> BorrowedHandle<'_> { + self.file().as_handle() + } + } + + impl From for OwnedHandle { + fn from(file: crate::File) -> Self { + file.into_parts().0.into() + } + } + } }