@@ -143,9 +143,18 @@ pub struct OwnedFd {
143
143
144
144
#[ cfg( any( unix, target_os = "wasi" ) ) ]
145
145
impl OwnedFd {
146
- /// Creates a new `OwnedFd` instance that shares the same underlying file handle
147
- /// as the existing `OwnedFd` instance.
146
+ /// Creates a new `OwnedFd` instance that shares the same underlying file
147
+ /// description as the existing `OwnedFd` instance.
148
148
pub fn try_clone ( & self ) -> std:: io:: Result < Self > {
149
+ crate :: AsFd :: as_fd ( self ) . try_clone_to_owned ( )
150
+ }
151
+ }
152
+
153
+ #[ cfg( any( unix, target_os = "wasi" ) ) ]
154
+ impl BorrowedFd < ' _ > {
155
+ /// Creates a new `OwnedFd` instance that shares the same underlying file
156
+ /// description as the existing `BorrowedFd` instance.
157
+ pub fn try_clone_to_owned ( & self ) -> std:: io:: Result < OwnedFd > {
149
158
#[ cfg( feature = "close" ) ]
150
159
{
151
160
#[ cfg( unix) ]
@@ -168,7 +177,7 @@ impl OwnedFd {
168
177
fd => fd,
169
178
} ;
170
179
171
- Ok ( unsafe { Self :: from_raw_fd ( fd) } )
180
+ Ok ( unsafe { OwnedFd :: from_raw_fd ( fd) } )
172
181
}
173
182
174
183
#[ cfg( target_os = "wasi" ) ]
@@ -215,9 +224,18 @@ pub struct OwnedHandle {
215
224
216
225
#[ cfg( windows) ]
217
226
impl OwnedHandle {
218
- /// Creates a new `OwnedHandle` instance that shares the same underlying file handle
219
- /// as the existing `OwnedHandle` instance.
220
- pub fn try_clone ( & self ) -> std:: io:: Result < OwnedHandle > {
227
+ /// Creates a new `OwnedHandle` instance that shares the same underlying
228
+ /// object as the existing `OwnedHandle` instance.
229
+ pub fn try_clone ( & self ) -> std:: io:: Result < Self > {
230
+ crate :: AsHandle :: as_handle ( self ) . try_clone_to_owned ( )
231
+ }
232
+ }
233
+
234
+ #[ cfg( windows) ]
235
+ impl BorrowedHandle < ' _ > {
236
+ /// Creates a new `OwnedHandle` instance that shares the same underlying
237
+ /// object as the existing `BorrowedHandle` instance.
238
+ pub fn try_clone_to_owned ( & self ) -> std:: io:: Result < OwnedHandle > {
221
239
#[ cfg( feature = "close" ) ]
222
240
{
223
241
self . duplicate ( 0 , false , DUPLICATE_SAME_ACCESS )
@@ -237,7 +255,7 @@ impl OwnedHandle {
237
255
access : u32 ,
238
256
inherit : bool ,
239
257
options : DUPLICATE_HANDLE_OPTIONS ,
240
- ) -> std:: io:: Result < Self > {
258
+ ) -> std:: io:: Result < OwnedHandle > {
241
259
let mut ret = 0 as HANDLE ;
242
260
match unsafe {
243
261
let cur_proc = GetCurrentProcess ( ) ;
@@ -254,7 +272,7 @@ impl OwnedHandle {
254
272
0 => return Err ( std:: io:: Error :: last_os_error ( ) ) ,
255
273
_ => ( ) ,
256
274
}
257
- unsafe { Ok ( Self :: from_raw_handle ( ret as RawHandle ) ) }
275
+ unsafe { Ok ( OwnedHandle :: from_raw_handle ( ret as RawHandle ) ) }
258
276
}
259
277
}
260
278
@@ -285,9 +303,38 @@ pub struct OwnedSocket {
285
303
286
304
#[ cfg( windows) ]
287
305
impl OwnedSocket {
288
- /// Creates a new `OwnedSocket` instance that shares the same underlying socket
289
- /// as the existing `OwnedSocket` instance.
306
+ /// Creates a new `OwnedSocket` instance that shares the same underlying
307
+ /// object as the existing `OwnedSocket` instance.
290
308
pub fn try_clone ( & self ) -> std:: io:: Result < Self > {
309
+ crate :: AsSocket :: as_socket ( self ) . try_clone_to_owned ( )
310
+ }
311
+
312
+ #[ cfg( feature = "close" ) ]
313
+ #[ cfg( not( target_vendor = "uwp" ) ) ]
314
+ fn set_no_inherit ( & self ) -> std:: io:: Result < ( ) > {
315
+ match unsafe {
316
+ SetHandleInformation ( self . as_raw_socket ( ) as HANDLE , HANDLE_FLAG_INHERIT , 0 )
317
+ } {
318
+ 0 => return Err ( std:: io:: Error :: last_os_error ( ) ) ,
319
+ _ => Ok ( ( ) ) ,
320
+ }
321
+ }
322
+
323
+ #[ cfg( feature = "close" ) ]
324
+ #[ cfg( target_vendor = "uwp" ) ]
325
+ fn set_no_inherit ( & self ) -> std:: io:: Result < ( ) > {
326
+ Err ( io:: Error :: new_const (
327
+ std:: io:: ErrorKind :: Unsupported ,
328
+ & "Unavailable on UWP" ,
329
+ ) )
330
+ }
331
+ }
332
+
333
+ #[ cfg( windows) ]
334
+ impl BorrowedSocket < ' _ > {
335
+ /// Creates a new `OwnedSocket` instance that shares the same underlying
336
+ /// object as the existing `BorrowedSocket` instance.
337
+ pub fn try_clone_to_owned ( & self ) -> std:: io:: Result < OwnedSocket > {
291
338
#[ cfg( feature = "close" ) ]
292
339
{
293
340
let mut info = unsafe { std:: mem:: zeroed :: < WSAPROTOCOL_INFOW > ( ) } ;
@@ -349,26 +396,6 @@ impl OwnedSocket {
349
396
unreachable ! ( "try_clone called without the \" close\" feature in io-lifetimes" ) ;
350
397
}
351
398
}
352
-
353
- #[ cfg( feature = "close" ) ]
354
- #[ cfg( not( target_vendor = "uwp" ) ) ]
355
- fn set_no_inherit ( & self ) -> std:: io:: Result < ( ) > {
356
- match unsafe {
357
- SetHandleInformation ( self . as_raw_socket ( ) as HANDLE , HANDLE_FLAG_INHERIT , 0 )
358
- } {
359
- 0 => return Err ( std:: io:: Error :: last_os_error ( ) ) ,
360
- _ => Ok ( ( ) ) ,
361
- }
362
- }
363
-
364
- #[ cfg( feature = "close" ) ]
365
- #[ cfg( target_vendor = "uwp" ) ]
366
- fn set_no_inherit ( & self ) -> std:: io:: Result < ( ) > {
367
- Err ( io:: Error :: new_const (
368
- std:: io:: ErrorKind :: Unsupported ,
369
- & "Unavailable on UWP" ,
370
- ) )
371
- }
372
399
}
373
400
374
401
/// FFI type for handles in return values or out parameters, where `INVALID_HANDLE_VALUE` is used
0 commit comments