11//! The following is derived from Rust's
22//! library/std/src/os/fd/owned.rs at revision
3- //! fa68e73e9947be8ffc5b3b46d899e4953a44e7e9 .
3+ //! 334a54cd83191f38ad8046ed94c45de735c86c65 .
44//!
55//! All code in this file is licensed MIT or Apache 2.0 at your option.
66//!
@@ -18,8 +18,9 @@ use core::mem::forget;
1818
1919/// A borrowed file descriptor.
2020///
21- /// This has a lifetime parameter to tie it to the lifetime of something that
22- /// owns the file descriptor.
21+ /// This has a lifetime parameter to tie it to the lifetime of something that owns the file
22+ /// descriptor. For the duration of that lifetime, it is guaranteed that nobody will close the file
23+ /// descriptor.
2324///
2425/// This uses `repr(transparent)` and has the representation of a host file
2526/// descriptor, so it can be used in FFI in places where a file descriptor is
@@ -36,16 +37,17 @@ use core::mem::forget;
3637// 32-bit c_int. Below is -2, in two's complement, but that only works out
3738// because c_int is 32 bits.
3839#[ cfg_attr( rustc_attrs, rustc_layout_scalar_valid_range_end( 0xFF_FF_FF_FE ) ) ]
39- #[ cfg_attr( staged_api, unstable( feature = "io_safety" , issue = "87074" ) ) ]
4040#[ cfg_attr( rustc_attrs, rustc_nonnull_optimization_guaranteed) ]
41+ #[ cfg_attr( staged_api, stable( feature = "io_safety" , since = "1.63.0" ) ) ]
4142pub struct BorrowedFd < ' fd > {
4243 fd : RawFd ,
4344 _phantom : PhantomData < & ' fd OwnedFd > ,
4445}
4546
4647/// An owned file descriptor.
4748///
48- /// This closes the file descriptor on drop.
49+ /// This closes the file descriptor on drop. It is guaranteed that nobody else will close the file
50+ /// descriptor.
4951///
5052/// This uses `repr(transparent)` and has the representation of a host file
5153/// descriptor, so it can be used in FFI in places where a file descriptor is
@@ -71,7 +73,11 @@ impl BorrowedFd<'_> {
7173 /// The resource pointed to by `fd` must remain open for the duration of
7274 /// the returned `BorrowedFd`, and it must not have the value `-1`.
7375 #[ inline]
74- #[ cfg_attr( staged_api, unstable( feature = "io_safety" , issue = "87074" ) ) ]
76+ #[ cfg_attr(
77+ staged_api,
78+ rustc_const_stable( feature = "io_safety" , since = "1.63.0" )
79+ ) ]
80+ #[ cfg_attr( staged_api, stable( feature = "io_safety" , since = "1.63.0" ) ) ]
7581 pub const unsafe fn borrow_raw ( fd : RawFd ) -> Self {
7682 assert ! ( fd != u32 :: MAX as RawFd ) ;
7783 // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
@@ -184,7 +190,9 @@ impl FromRawFd for OwnedFd {
184190 /// # Safety
185191 ///
186192 /// The resource pointed to by `fd` must be open and suitable for assuming
187- /// ownership. The resource must not require any cleanup other than `close`.
193+ /// [ownership][io-safety]. The resource must not require any cleanup other than `close`.
194+ ///
195+ /// [io-safety]: io#io-safety
188196 #[ inline]
189197 unsafe fn from_raw_fd ( fd : RawFd ) -> Self {
190198 assert_ne ! ( fd, u32 :: MAX as RawFd ) ;
0 commit comments