Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement I/O safety traits #39

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<crate::File> for OwnedFd {
fn from(file: crate::File) -> Self {
file.into_parts().0.into()
}
}
}
}

#[cfg(windows)]
Expand Down Expand Up @@ -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<crate::File> for OwnedHandle {
fn from(file: crate::File) -> Self {
file.into_parts().0.into()
}
}
}
}