Skip to content

Commit f798960

Browse files
authored
Add a try_close function. (#1085)
The rustix developers do not intend the existence of this feature to imply that anyone should use it.
1 parent 5662b1f commit f798960

6 files changed

Lines changed: 36 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,7 @@ rustc-dep-of-std = [
245245

246246
# Obsolete and deprecated.
247247
cc = []
248+
249+
# Enable `rustix::io::try_close`. The rustix developers do not intend the
250+
# existence of this feature to imply that anyone should use it.
251+
try_close = []

src/backend/libc/io/syscalls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ pub(crate) unsafe fn close(raw_fd: RawFd) {
201201
let _ = c::close(raw_fd as c::c_int);
202202
}
203203

204+
#[cfg(feature = "try_close")]
205+
pub(crate) unsafe fn try_close(raw_fd: RawFd) -> io::Result<()> {
206+
ret(c::close(raw_fd as c::c_int))
207+
}
208+
204209
#[inline]
205210
pub(crate) unsafe fn ioctl(
206211
fd: BorrowedFd<'_>,

src/backend/linux_raw/io/syscalls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ pub(crate) unsafe fn close(fd: RawFd) {
241241
syscall_readonly!(__NR_close, raw_fd(fd)).decode_void();
242242
}
243243

244+
#[cfg(feature = "try_close")]
245+
#[inline]
246+
pub(crate) unsafe fn try_close(fd: RawFd) -> io::Result<()> {
247+
ret(syscall_readonly!(__NR_close, raw_fd(fd)))
248+
}
249+
244250
#[inline]
245251
pub(crate) unsafe fn ioctl(
246252
fd: BorrowedFd<'_>,

src/io/close.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@ use backend::fd::RawFd;
5353
pub unsafe fn close(raw_fd: RawFd) {
5454
backend::io::syscalls::close(raw_fd)
5555
}
56+
57+
/// `close(raw_fd)`—Closes a `RawFd` directly, and report any errors
58+
/// returned by the OS.
59+
///
60+
/// The rustix developers do not intend the existence of this feature to imply
61+
/// that anyone should use it.
62+
#[cfg(feature = "try_close")]
63+
pub unsafe fn try_close(raw_fd: RawFd) -> crate::io::Result<()> {
64+
backend::io::syscalls::try_close(raw_fd)
65+
}

src/io/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod is_read_write;
1818
#[cfg(not(windows))]
1919
mod read_write;
2020

21-
pub use close::close;
21+
pub use close::*;
2222
#[cfg(not(windows))]
2323
pub use dup::*;
2424
pub use errno::{retry_on_intr, Errno, Result};

tests/io/close.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ fn test_close_socket() {
1818
rustix::io::close(raw);
1919
}
2020
}
21+
22+
#[cfg(all(feature = "try_close", any(unix, target_os = "wasi")))]
23+
#[test]
24+
fn test_try_close() {
25+
let file = std::fs::File::open("Cargo.toml").unwrap();
26+
let raw = file.into_raw_fd();
27+
unsafe {
28+
rustix::io::try_close(raw).unwrap();
29+
}
30+
}

0 commit comments

Comments
 (0)