@@ -415,6 +415,8 @@ impl From<std::fs::File> for File {
415415cfg_unix ! {
416416 use crate :: os:: unix:: io:: { AsRawFd , FromRawFd , IntoRawFd , RawFd } ;
417417
418+
419+
418420 impl AsRawFd for File {
419421 fn as_raw_fd( & self ) -> RawFd {
420422 self . file. as_raw_fd( )
@@ -432,10 +434,36 @@ cfg_unix! {
432434 let file = self . file. clone( ) ;
433435 drop( self ) ;
434436 Arc :: try_unwrap( file)
435- . expect( "cannot acquire ownership of the file handle after drop" )
437+ . expect( ARC_TRY_UNWRAP_EXPECT )
436438 . into_raw_fd( )
437439 }
438440 }
441+
442+ cfg_io_safety! {
443+ use crate :: os:: unix:: io:: { AsFd , BorrowedFd , OwnedFd } ;
444+
445+ impl AsFd for File {
446+ fn as_fd( & self ) -> BorrowedFd <' _> {
447+ self . file. as_fd( )
448+ }
449+ }
450+
451+ impl From <OwnedFd > for File {
452+ fn from( fd: OwnedFd ) -> Self {
453+ std:: fs:: File :: from( fd) . into( )
454+ }
455+ }
456+
457+ impl From <File > for OwnedFd {
458+ fn from( val: File ) -> OwnedFd {
459+ let file = val. file. clone( ) ;
460+ drop( val) ;
461+ Arc :: try_unwrap( file)
462+ . expect( ARC_TRY_UNWRAP_EXPECT )
463+ . into( )
464+ }
465+ }
466+ }
439467}
440468
441469cfg_windows ! {
@@ -458,10 +486,36 @@ cfg_windows! {
458486 let file = self . file. clone( ) ;
459487 drop( self ) ;
460488 Arc :: try_unwrap( file)
461- . expect( "cannot acquire ownership of the file handle after drop" )
489+ . expect( ARC_TRY_UNWRAP_EXPECT )
462490 . into_raw_handle( )
463491 }
464492 }
493+
494+ cfg_io_safety! {
495+ use crate :: os:: windows:: io:: { AsHandle , BorrowedHandle , OwnedHandle } ;
496+
497+ impl AsHandle for File {
498+ fn as_handle( & self ) -> BorrowedHandle <' _> {
499+ self . file. as_handle( )
500+ }
501+ }
502+
503+ impl From <OwnedHandle > for File {
504+ fn from( handle: OwnedHandle ) -> Self {
505+ std:: fs:: File :: from( handle) . into( )
506+ }
507+ }
508+
509+ impl From <File > for OwnedHandle {
510+ fn from( val: File ) -> OwnedHandle {
511+ let file = val. file. clone( ) ;
512+ drop( val) ;
513+ Arc :: try_unwrap( file)
514+ . expect( ARC_TRY_UNWRAP_EXPECT )
515+ . into( )
516+ }
517+ }
518+ }
465519}
466520
467521/// An async mutex with non-borrowing lock guards.
@@ -974,3 +1028,5 @@ mod tests {
9741028 } )
9751029 }
9761030}
1031+
1032+ const ARC_TRY_UNWRAP_EXPECT : & str = "cannot acquire ownership of the file handle after drop" ;
0 commit comments