Skip to content

Commit

Permalink
feat: implement AsFd/AsHandle to mirror the AsRaw* variants (#230)
Browse files Browse the repository at this point in the history
We don't implement the "into" variants as that'll drop and delete the
temporary file.
  • Loading branch information
Stebalien authored Apr 8, 2023
1 parent ae4f4c8 commit c76b783
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ redox_syscall = "0.3"
[dev-dependencies]
doc-comment = "0.3"

[build-dependencies]
autocfg = "1"

[features]
nightly = []
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
let ac = autocfg::new();

#[cfg(unix)]
ac.emit_trait_cfg("std::os::fd::AsFd", "fd");
#[cfg(windows)]
ac.emit_trait_cfg("std::os::windows::io::AsHandle", "fd");

autocfg::rerun_path("build.rs");
}
18 changes: 18 additions & 0 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,13 @@ 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<'_> {
self.as_file().as_fd()
}
}

#[cfg(unix)]
impl<F> std::os::unix::io::AsRawFd for NamedTempFile<F>
where
Expand All @@ -1045,6 +1052,17 @@ where
}
}

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

#[cfg(windows)]
impl<F> std::os::windows::io::AsRawHandle for NamedTempFile<F>
where
Expand Down

0 comments on commit c76b783

Please sign in to comment.