11
11
#![ unstable( reason = "not public" , issue = "0" , feature = "fd" ) ]
12
12
13
13
use io:: { self , Read } ;
14
- use libc:: { self , c_int , c_void } ;
14
+ use libc;
15
15
use mem;
16
16
use sys:: cvt;
17
17
use sys_common:: AsInner ;
18
18
use sys_common:: io:: read_to_end_uninitialized;
19
19
20
20
pub struct FileDesc {
21
- fd : c_int ,
21
+ fd : usize ,
22
22
}
23
23
24
24
impl FileDesc {
25
- pub fn new ( fd : c_int ) -> FileDesc {
25
+ pub fn new ( fd : usize ) -> FileDesc {
26
26
FileDesc { fd : fd }
27
27
}
28
28
29
- pub fn raw ( & self ) -> c_int { self . fd }
29
+ pub fn raw ( & self ) -> usize { self . fd }
30
30
31
31
/// Extracts the actual filedescriptor without closing it.
32
- pub fn into_raw ( self ) -> c_int {
32
+ pub fn into_raw ( self ) -> usize {
33
33
let fd = self . fd ;
34
34
mem:: forget ( self ) ;
35
35
fd
36
36
}
37
37
38
38
pub fn read ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
39
- let ret = cvt ( unsafe {
40
- libc:: read ( self . fd ,
41
- buf. as_mut_ptr ( ) as * mut c_void ,
42
- buf. len ( ) )
43
- } ) ?;
44
- Ok ( ret as usize )
39
+ cvt ( libc:: read ( self . fd , buf) )
45
40
}
46
41
47
42
pub fn read_to_end ( & self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
@@ -50,12 +45,7 @@ impl FileDesc {
50
45
}
51
46
52
47
pub fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
53
- let ret = cvt ( unsafe {
54
- libc:: write ( self . fd ,
55
- buf. as_ptr ( ) as * const c_void ,
56
- buf. len ( ) )
57
- } ) ?;
58
- Ok ( ret as usize )
48
+ cvt ( libc:: write ( self . fd , buf) )
59
49
}
60
50
61
51
pub fn set_cloexec ( & self ) -> io:: Result < ( ) > {
@@ -86,7 +76,7 @@ impl FileDesc {
86
76
}
87
77
88
78
pub fn duplicate ( & self ) -> io:: Result < FileDesc > {
89
- let new_fd = cvt ( unsafe { libc:: dup ( self . fd ) } ) ?;
79
+ let new_fd = cvt ( libc:: dup ( self . fd , & [ ] ) ) ?;
90
80
Ok ( FileDesc :: new ( new_fd) )
91
81
}
92
82
}
@@ -101,8 +91,8 @@ impl<'a> Read for &'a FileDesc {
101
91
}
102
92
}
103
93
104
- impl AsInner < c_int > for FileDesc {
105
- fn as_inner ( & self ) -> & c_int { & self . fd }
94
+ impl AsInner < usize > for FileDesc {
95
+ fn as_inner ( & self ) -> & usize { & self . fd }
106
96
}
107
97
108
98
impl Drop for FileDesc {
@@ -112,6 +102,6 @@ impl Drop for FileDesc {
112
102
// the file descriptor was closed or not, and if we retried (for
113
103
// something like EINTR), we might close another valid file descriptor
114
104
// (opened after we closed ours.
115
- let _ = unsafe { libc:: close ( self . fd ) } ;
105
+ let _ = libc:: close ( self . fd ) ;
116
106
}
117
107
}
0 commit comments