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 #25

Merged
merged 1 commit into from
Aug 21, 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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ jobs:
profile: minimal
override: true
- run: cargo test
# notgull: check io_safety feature with CI
- run: cargo check --features io_safety
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ libc = "0.2.62"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.5", features = ["handleapi", "namedpipeapi", "processthreadsapi", "winnt"] }

[features]
# Uses I/O safety features introduced in Rust 1.63
io_safety = []
42 changes: 42 additions & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,45 @@ impl FromRawFd for PipeWriter {
PipeWriter(File::from_raw_fd(fd))
}
}

#[cfg(feature = "io_safety")]
impl From<PipeReader> for OwnedFd {
fn from(pr: PipeReader) -> Self {
pr.0.into()
}
}

#[cfg(feature = "io_safety")]
impl AsFd for PipeReader {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

#[cfg(feature = "io_safety")]
impl From<OwnedFd> for PipeReader {
fn from(fd: OwnedFd) -> Self {
PipeReader(fd.into())
}
}

#[cfg(feature = "io_safety")]
impl From<PipeWriter> for OwnedFd {
fn from(pw: PipeWriter) -> Self {
pw.0.into()
}
}

#[cfg(feature = "io_safety")]
impl AsFd for PipeWriter {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

#[cfg(feature = "io_safety")]
impl From<OwnedFd> for PipeWriter {
fn from(fd: OwnedFd) -> Self {
PipeWriter(fd.into())
}
}
42 changes: 42 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,45 @@ impl FromRawHandle for PipeWriter {
PipeWriter(File::from_raw_handle(handle))
}
}

#[cfg(feature = "io_safety")]
impl From<PipeReader> for OwnedHandle {
fn from(reader: PipeReader) -> Self {
reader.0.into()
}
}

#[cfg(feature = "io_safety")]
impl AsHandle for PipeReader {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.0.as_handle()
}
}

#[cfg(feature = "io_safety")]
impl From<OwnedHandle> for PipeReader {
fn from(handle: OwnedHandle) -> Self {
PipeReader(handle.into())
}
}

#[cfg(feature = "io_safety")]
impl From<PipeWriter> for OwnedHandle {
fn from(writer: PipeWriter) -> Self {
writer.0.into()
}
}

#[cfg(feature = "io_safety")]
impl AsHandle for PipeWriter {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.0.as_handle()
}
}

#[cfg(feature = "io_safety")]
impl From<OwnedHandle> for PipeWriter {
fn from(handle: OwnedHandle) -> Self {
PipeWriter(handle.into())
}
}