1- use super :: raw:: RawFd ;
2-
1+ use super :: raw:: { AsRawFd , FromRawFd , IntoRawFd , RawFd } ;
2+ use crate :: fmt ;
33use crate :: marker:: PhantomData ;
44use crate :: mem:: forget;
5- use crate :: sys:: fd:: { AsRawFd , FromRawFd , IntoRawFd } ;
6- use crate :: sys:: hermit:: abi;
5+ use crate :: os:: hermit:: abi;
76use crate :: sys_common:: { AsInner , FromInner , IntoInner } ;
87
98/// A borrowed file descriptor.
@@ -49,7 +48,6 @@ pub struct BorrowedFd<'fd> {
4948#[ rustc_layout_scalar_valid_range_end( 0xFF_FF_FF_FE ) ]
5049#[ rustc_nonnull_optimization_guaranteed]
5150#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
52- #[ derive( Debug ) ]
5351pub struct OwnedFd {
5452 fd : RawFd ,
5553}
@@ -71,6 +69,35 @@ impl BorrowedFd<'_> {
7169 }
7270}
7371
72+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
73+ impl Drop for OwnedFd {
74+ #[ inline]
75+ fn drop ( & mut self ) {
76+ unsafe {
77+ // Note that errors are ignored when closing a file descriptor. The
78+ // reason for this is that if an error occurs we don't actually know if
79+ // the file descriptor was closed or not, and if we retried (for
80+ // something like EINTR), we might close another valid file descriptor
81+ // opened after we closed ours.
82+ let _ = abi:: close ( self . fd ) ;
83+ }
84+ }
85+ }
86+
87+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
88+ impl fmt:: Debug for BorrowedFd < ' _ > {
89+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
90+ f. debug_struct ( "BorrowedFd" ) . field ( "fd" , & self . fd ) . finish ( )
91+ }
92+ }
93+
94+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
95+ impl fmt:: Debug for OwnedFd {
96+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
97+ f. debug_struct ( "OwnedFd" ) . field ( "fd" , & self . fd ) . finish ( )
98+ }
99+ }
100+
74101#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
75102impl AsRawFd for BorrowedFd < ' _ > {
76103 #[ inline]
@@ -113,14 +140,6 @@ impl FromRawFd for OwnedFd {
113140 }
114141}
115142
116- #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
117- impl AsFd for crate :: net:: TcpStream {
118- #[ inline]
119- fn as_fd ( & self ) -> BorrowedFd < ' _ > {
120- self . as_inner ( ) . socket ( ) . as_fd ( )
121- }
122- }
123-
124143#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
125144impl From < crate :: net:: TcpStream > for OwnedFd {
126145 #[ inline]
@@ -139,14 +158,6 @@ impl From<OwnedFd> for crate::net::TcpStream {
139158 }
140159}
141160
142- #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
143- impl AsFd for crate :: net:: TcpListener {
144- #[ inline]
145- fn as_fd ( & self ) -> BorrowedFd < ' _ > {
146- self . as_inner ( ) . socket ( ) . as_fd ( )
147- }
148- }
149-
150161#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
151162impl From < crate :: net:: TcpListener > for OwnedFd {
152163 #[ inline]
@@ -165,14 +176,6 @@ impl From<OwnedFd> for crate::net::TcpListener {
165176 }
166177}
167178
168- #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
169- impl AsFd for crate :: net:: UdpSocket {
170- #[ inline]
171- fn as_fd ( & self ) -> BorrowedFd < ' _ > {
172- self . as_inner ( ) . socket ( ) . as_fd ( )
173- }
174- }
175-
176179#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
177180impl From < crate :: net:: UdpSocket > for OwnedFd {
178181 #[ inline]
@@ -191,21 +194,8 @@ impl From<OwnedFd> for crate::net::UdpSocket {
191194 }
192195}
193196
197+ /// A trait to borrow the file descriptor from an underlying object.
194198#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
195- impl Drop for OwnedFd {
196- #[ inline]
197- fn drop ( & mut self ) {
198- unsafe {
199- // Note that errors are ignored when closing a file descriptor. The
200- // reason for this is that if an error occurs we don't actually know if
201- // the file descriptor was closed or not, and if we retried (for
202- // something like EINTR), we might close another valid file descriptor
203- // opened after we closed ours.
204- let _ = abi:: close ( self . fd ) ;
205- }
206- }
207- }
208-
209199pub trait AsFd {
210200 /// Borrows the file descriptor.
211201 ///
@@ -226,6 +216,22 @@ pub trait AsFd {
226216 fn as_fd ( & self ) -> BorrowedFd < ' _ > ;
227217}
228218
219+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
220+ impl < T : AsFd > AsFd for & T {
221+ #[ inline]
222+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
223+ T :: as_fd ( self )
224+ }
225+ }
226+
227+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
228+ impl < T : AsFd > AsFd for & mut T {
229+ #[ inline]
230+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
231+ T :: as_fd ( self )
232+ }
233+ }
234+
229235#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
230236impl AsFd for OwnedFd {
231237 #[ inline]
@@ -236,3 +242,27 @@ impl AsFd for OwnedFd {
236242 unsafe { BorrowedFd :: borrow_raw ( self . as_raw_fd ( ) ) }
237243 }
238244}
245+
246+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
247+ impl AsFd for crate :: net:: UdpSocket {
248+ #[ inline]
249+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
250+ self . as_inner ( ) . socket ( ) . as_fd ( )
251+ }
252+ }
253+
254+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
255+ impl AsFd for crate :: net:: TcpListener {
256+ #[ inline]
257+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
258+ self . as_inner ( ) . socket ( ) . as_fd ( )
259+ }
260+ }
261+
262+ #[ stable( feature = "io_safety" , since = "1.63.0" ) ]
263+ impl AsFd for crate :: net:: TcpStream {
264+ #[ inline]
265+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
266+ self . as_inner ( ) . socket ( ) . as_fd ( )
267+ }
268+ }
0 commit comments