File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -245,3 +245,7 @@ rustc-dep-of-std = [
245245
246246# Obsolete and deprecated.
247247cc = []
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 = []
Original file line number Diff line number Diff 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]
205210pub ( crate ) unsafe fn ioctl (
206211 fd : BorrowedFd < ' _ > ,
Original file line number Diff line number Diff 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]
245251pub ( crate ) unsafe fn ioctl (
246252 fd : BorrowedFd < ' _ > ,
Original file line number Diff line number Diff line change @@ -53,3 +53,13 @@ use backend::fd::RawFd;
5353pub 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+ }
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ mod is_read_write;
1818#[ cfg( not( windows) ) ]
1919mod read_write;
2020
21- pub use close:: close ;
21+ pub use close:: * ;
2222#[ cfg( not( windows) ) ]
2323pub use dup:: * ;
2424pub use errno:: { retry_on_intr, Errno , Result } ;
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments