Skip to content

Commit

Permalink
Merge pull request #39 from notgull/master
Browse files Browse the repository at this point in the history
Implement I/O safety traits
  • Loading branch information
andrewhickman authored Aug 17, 2022
2 parents cac1904 + 7b9e0f1 commit d3bbe4c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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()
}
}
}
}

0 comments on commit d3bbe4c

Please sign in to comment.