1
- use super :: raw:: RawFd ;
2
-
1
+ use super :: raw:: { AsRawFd , FromRawFd , IntoRawFd , RawFd } ;
2
+ use crate :: fmt ;
3
3
use crate :: marker:: PhantomData ;
4
4
use crate :: mem:: forget;
5
- use crate :: sys:: fd:: { AsRawFd , FromRawFd , IntoRawFd } ;
6
- use crate :: sys:: hermit:: abi;
5
+ use crate :: os:: hermit:: abi;
7
6
use crate :: sys_common:: { AsInner , FromInner , IntoInner } ;
8
7
9
8
/// A borrowed file descriptor.
@@ -49,7 +48,6 @@ pub struct BorrowedFd<'fd> {
49
48
#[ rustc_layout_scalar_valid_range_end( 0xFF_FF_FF_FE ) ]
50
49
#[ rustc_nonnull_optimization_guaranteed]
51
50
#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
52
- #[ derive( Debug ) ]
53
51
pub struct OwnedFd {
54
52
fd : RawFd ,
55
53
}
@@ -71,6 +69,35 @@ impl BorrowedFd<'_> {
71
69
}
72
70
}
73
71
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
+
74
101
#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
75
102
impl AsRawFd for BorrowedFd < ' _ > {
76
103
#[ inline]
@@ -113,14 +140,6 @@ impl FromRawFd for OwnedFd {
113
140
}
114
141
}
115
142
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
-
124
143
#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
125
144
impl From < crate :: net:: TcpStream > for OwnedFd {
126
145
#[ inline]
@@ -139,14 +158,6 @@ impl From<OwnedFd> for crate::net::TcpStream {
139
158
}
140
159
}
141
160
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
-
150
161
#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
151
162
impl From < crate :: net:: TcpListener > for OwnedFd {
152
163
#[ inline]
@@ -165,14 +176,6 @@ impl From<OwnedFd> for crate::net::TcpListener {
165
176
}
166
177
}
167
178
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
-
176
179
#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
177
180
impl From < crate :: net:: UdpSocket > for OwnedFd {
178
181
#[ inline]
@@ -191,21 +194,8 @@ impl From<OwnedFd> for crate::net::UdpSocket {
191
194
}
192
195
}
193
196
197
+ /// A trait to borrow the file descriptor from an underlying object.
194
198
#[ 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
-
209
199
pub trait AsFd {
210
200
/// Borrows the file descriptor.
211
201
///
@@ -226,6 +216,22 @@ pub trait AsFd {
226
216
fn as_fd ( & self ) -> BorrowedFd < ' _ > ;
227
217
}
228
218
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
+
229
235
#[ stable( feature = "io_safety" , since = "1.63.0" ) ]
230
236
impl AsFd for OwnedFd {
231
237
#[ inline]
@@ -236,3 +242,27 @@ impl AsFd for OwnedFd {
236
242
unsafe { BorrowedFd :: borrow_raw ( self . as_raw_fd ( ) ) }
237
243
}
238
244
}
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