@@ -143,9 +143,18 @@ pub struct OwnedFd {
143143
144144#[ cfg( any( unix, target_os = "wasi" ) ) ]
145145impl 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.
148148 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 > {
149158 #[ cfg( feature = "close" ) ]
150159 {
151160 #[ cfg( unix) ]
@@ -168,7 +177,7 @@ impl OwnedFd {
168177 fd => fd,
169178 } ;
170179
171- Ok ( unsafe { Self :: from_raw_fd ( fd) } )
180+ Ok ( unsafe { OwnedFd :: from_raw_fd ( fd) } )
172181 }
173182
174183 #[ cfg( target_os = "wasi" ) ]
@@ -215,9 +224,18 @@ pub struct OwnedHandle {
215224
216225#[ cfg( windows) ]
217226impl 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 > {
221239 #[ cfg( feature = "close" ) ]
222240 {
223241 self . duplicate ( 0 , false , DUPLICATE_SAME_ACCESS )
@@ -237,7 +255,7 @@ impl OwnedHandle {
237255 access : u32 ,
238256 inherit : bool ,
239257 options : DUPLICATE_HANDLE_OPTIONS ,
240- ) -> std:: io:: Result < Self > {
258+ ) -> std:: io:: Result < OwnedHandle > {
241259 let mut ret = 0 as HANDLE ;
242260 match unsafe {
243261 let cur_proc = GetCurrentProcess ( ) ;
@@ -254,7 +272,7 @@ impl OwnedHandle {
254272 0 => return Err ( std:: io:: Error :: last_os_error ( ) ) ,
255273 _ => ( ) ,
256274 }
257- unsafe { Ok ( Self :: from_raw_handle ( ret as RawHandle ) ) }
275+ unsafe { Ok ( OwnedHandle :: from_raw_handle ( ret as RawHandle ) ) }
258276 }
259277}
260278
@@ -285,9 +303,38 @@ pub struct OwnedSocket {
285303
286304#[ cfg( windows) ]
287305impl 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.
290308 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 > {
291338 #[ cfg( feature = "close" ) ]
292339 {
293340 let mut info = unsafe { std:: mem:: zeroed :: < WSAPROTOCOL_INFOW > ( ) } ;
@@ -349,26 +396,6 @@ impl OwnedSocket {
349396 unreachable ! ( "try_clone called without the \" close\" feature in io-lifetimes" ) ;
350397 }
351398 }
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- }
372399}
373400
374401/// FFI type for handles in return values or out parameters, where `INVALID_HANDLE_VALUE` is used
0 commit comments