11//! A module implementing an io type backed by the C runtime's file descriptors, i.e. what's
22//! returned from libc::open, even on windows.
33
4- use crate :: suppress_iph;
5- use std:: { cmp, ffi, fs, io, mem} ;
4+ use std:: { cmp, ffi, io} ;
65
76#[ cfg( windows) ]
87use libc:: commit as fsync;
@@ -24,7 +23,7 @@ pub type Offset = libc::c_longlong;
2423#[ inline]
2524fn cvt < T , I : num_traits:: PrimInt > ( ret : I , f : impl FnOnce ( I ) -> T ) -> io:: Result < T > {
2625 if ret < I :: zero ( ) {
27- Err ( crate :: stdlib :: os:: errno ( ) )
26+ Err ( crate :: os:: errno ( ) )
2827 } else {
2928 Ok ( f ( ret) )
3029 }
@@ -73,50 +72,16 @@ impl Fd {
7372 cvt ( unsafe { suppress_iph ! ( ftruncate( self . 0 , len) ) } , drop)
7473 }
7574
76- /// NOTE: it's not recommended to use ManuallyDrop::into_inner() to drop the file - it won't
77- /// work on all platforms, and will swallow any errors you might want to handle.
78- #[ allow( unused) ] // only used on windows atm
79- pub ( crate ) fn as_rust_file ( & self ) -> io:: Result < mem:: ManuallyDrop < fs:: File > > {
80- #[ cfg( windows) ]
81- let file = {
82- use std:: os:: windows:: io:: FromRawHandle ;
83- let handle = self . to_raw_handle ( ) ?;
84- unsafe { fs:: File :: from_raw_handle ( handle) }
85- } ;
86- #[ cfg( unix) ]
87- let file = {
88- let fd = self . 0 ;
89- use std:: os:: unix:: io:: FromRawFd ;
90- if fd < 0 {
91- return Err ( io:: Error :: from_raw_os_error ( libc:: EBADF ) ) ;
92- }
93- unsafe { fs:: File :: from_raw_fd ( fd) }
94- } ;
95- #[ cfg( target_os = "wasi" ) ]
96- let file = {
97- let fd = self . 0 ;
98- if fd < 0 {
99- return Err ( io:: Error :: from_raw_os_error ( libc:: EBADF ) ) ;
100- }
101- // SAFETY: as of now, File is a wrapper around WasiFd, which is a wrapper around
102- // wasi::Fd (u32). This isn't likely to change, and if it does change to a different
103- // sized integer, mem::transmute will fail.
104- unsafe { mem:: transmute :: < u32 , fs:: File > ( fd as u32 ) }
105- } ;
106- Ok ( mem:: ManuallyDrop :: new ( file) )
107- }
108-
10975 #[ cfg( windows) ]
110- pub ( crate ) fn to_raw_handle ( & self ) -> io:: Result < std:: os:: windows:: io:: RawHandle > {
111- use winapi:: um:: { handleapi:: INVALID_HANDLE_VALUE , winnt:: HANDLE } ;
76+ pub fn to_raw_handle ( & self ) -> io:: Result < std:: os:: windows:: io:: RawHandle > {
11277 extern "C" {
11378 fn _get_osfhandle ( fd : i32 ) -> libc:: intptr_t ;
11479 }
115- let handle = unsafe { suppress_iph ! ( _get_osfhandle( self . 0 ) ) } as HANDLE ;
116- if handle == INVALID_HANDLE_VALUE {
80+ let handle = unsafe { suppress_iph ! ( _get_osfhandle( self . 0 ) ) } ;
81+ if handle == - 1 {
11782 Err ( io:: Error :: last_os_error ( ) )
11883 } else {
119- Ok ( handle)
84+ Ok ( handle as _ )
12085 }
12186 }
12287}
0 commit comments