1
1
#![ unstable( reason = "not public" , issue = "none" , feature = "net_fd" ) ]
2
2
3
3
use crate :: cmp;
4
- use crate :: io:: { self , Read , Initializer , IoSlice , IoSliceMut } ;
4
+ use crate :: io:: { self , Initializer , IoSlice , IoSliceMut , Read } ;
5
5
use crate :: mem;
6
- use crate :: sys:: { cvt, net:: netc:: { self , c_int, c_void, ssize_t} } ;
6
+ use crate :: sys:: {
7
+ cvt,
8
+ net:: netc:: { self , c_int, c_void, ssize_t} ,
9
+ } ;
7
10
use crate :: sys_common:: AsInner ;
8
11
9
12
#[ derive( Debug ) ]
@@ -32,7 +35,9 @@ impl NetFileDesc {
32
35
NetFileDesc { fd }
33
36
}
34
37
35
- pub fn raw ( & self ) -> c_int { self . fd }
38
+ pub fn raw ( & self ) -> c_int {
39
+ self . fd
40
+ }
36
41
37
42
/// Extracts the actual file descriptor without closing it.
38
43
pub fn into_raw ( self ) -> c_int {
@@ -43,18 +48,18 @@ impl NetFileDesc {
43
48
44
49
pub fn read ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
45
50
let ret = cvt ( unsafe {
46
- netc:: read ( self . fd ,
47
- buf. as_mut_ptr ( ) as * mut c_void ,
48
- cmp:: min ( buf. len ( ) , max_len ( ) ) )
51
+ netc:: read ( self . fd , buf. as_mut_ptr ( ) as * mut c_void , cmp:: min ( buf. len ( ) , max_len ( ) ) )
49
52
} ) ?;
50
53
Ok ( ret as usize )
51
54
}
52
55
53
56
pub fn read_vectored ( & self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
54
57
let ret = cvt ( unsafe {
55
- netc:: readv ( self . fd ,
56
- bufs. as_ptr ( ) as * const netc:: iovec ,
57
- cmp:: min ( bufs. len ( ) , c_int:: max_value ( ) as usize ) as c_int )
58
+ netc:: readv (
59
+ self . fd ,
60
+ bufs. as_ptr ( ) as * const netc:: iovec ,
61
+ cmp:: min ( bufs. len ( ) , c_int:: max_value ( ) as usize ) as c_int ,
62
+ )
58
63
} ) ?;
59
64
Ok ( ret as usize )
60
65
}
@@ -66,51 +71,56 @@ impl NetFileDesc {
66
71
67
72
pub fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
68
73
let ret = cvt ( unsafe {
69
- netc:: write ( self . fd ,
70
- buf. as_ptr ( ) as * const c_void ,
71
- cmp:: min ( buf. len ( ) , max_len ( ) ) )
74
+ netc:: write ( self . fd , buf. as_ptr ( ) as * const c_void , cmp:: min ( buf. len ( ) , max_len ( ) ) )
72
75
} ) ?;
73
76
Ok ( ret as usize )
74
77
}
75
78
76
79
pub fn write_vectored ( & self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
77
80
let ret = cvt ( unsafe {
78
- netc:: writev ( self . fd ,
79
- bufs. as_ptr ( ) as * const netc:: iovec ,
80
- cmp:: min ( bufs. len ( ) , c_int:: max_value ( ) as usize ) as c_int )
81
+ netc:: writev (
82
+ self . fd ,
83
+ bufs. as_ptr ( ) as * const netc:: iovec ,
84
+ cmp:: min ( bufs. len ( ) , c_int:: max_value ( ) as usize ) as c_int ,
85
+ )
81
86
} ) ?;
82
87
Ok ( ret as usize )
83
88
}
84
89
85
90
#[ cfg( target_os = "linux" ) ]
86
91
pub fn get_cloexec ( & self ) -> io:: Result < bool > {
87
- unsafe {
88
- Ok ( ( cvt ( netc :: fcntl ( self . fd , netc :: F_GETFD ) ) ? & netc :: FD_CLOEXEC ) != 0 )
89
- }
90
- }
91
-
92
- # [ cfg ( not ( any ( target_env = "newlib " ,
93
- target_os = "solaris " ,
94
- target_os = "emscripten " ,
95
- target_os = "fuchsia " ,
96
- target_os = "l4re " ,
97
- target_os = "linux " ,
98
- target_os = "haiku" ,
99
- target_os = "redox" ) ) ) ]
92
+ unsafe { Ok ( ( cvt ( netc :: fcntl ( self . fd , netc :: F_GETFD ) ) ? & netc :: FD_CLOEXEC ) != 0 ) }
93
+ }
94
+
95
+ # [ cfg ( not ( any (
96
+ target_env = "newlib" ,
97
+ target_os = "solaris " ,
98
+ target_os = "emscripten " ,
99
+ target_os = "fuchsia " ,
100
+ target_os = "l4re " ,
101
+ target_os = "linux " ,
102
+ target_os = "haiku " ,
103
+ target_os = "redox"
104
+ ) ) ) ]
100
105
pub fn set_cloexec ( & self ) -> io:: Result < ( ) > {
101
106
unsafe {
102
107
cvt ( netc:: ioctl ( self . fd , netc:: FIOCLEX ) ) ?;
103
108
Ok ( ( ) )
104
109
}
105
110
}
106
- #[ cfg( all( any( target_env = "newlib" ,
107
- target_os = "solaris" ,
108
- target_os = "emscripten" ,
109
- target_os = "fuchsia" ,
110
- target_os = "l4re" ,
111
- target_os = "linux" ,
112
- target_os = "haiku" ,
113
- target_os = "redox" ) , not( target_os = "freertos" ) ) ) ]
111
+ #[ cfg( all(
112
+ any(
113
+ target_env = "newlib" ,
114
+ target_os = "solaris" ,
115
+ target_os = "emscripten" ,
116
+ target_os = "fuchsia" ,
117
+ target_os = "l4re" ,
118
+ target_os = "linux" ,
119
+ target_os = "haiku" ,
120
+ target_os = "redox"
121
+ ) ,
122
+ not( target_os = "freertos" )
123
+ ) ) ]
114
124
pub fn set_cloexec ( & self ) -> io:: Result < ( ) > {
115
125
unsafe {
116
126
let previous = cvt ( netc:: fcntl ( self . fd , netc:: F_GETFD ) ) ?;
@@ -149,16 +159,15 @@ impl NetFileDesc {
149
159
// [1]: http://comments.gmane.org/gmane.linux.lib.musl.general/2963
150
160
#[ cfg( any( target_os = "android" , target_os = "haiku" ) ) ]
151
161
use netc:: F_DUPFD as F_DUPFD_CLOEXEC ;
152
- #[ cfg( not( any( target_os = "android" , target_os= "haiku" ) ) ) ]
162
+ #[ cfg( not( any( target_os = "android" , target_os = "haiku" ) ) ) ]
153
163
use netc:: F_DUPFD_CLOEXEC ;
154
164
155
165
let make_filedesc = |fd| {
156
166
let fd = NetFileDesc :: new ( fd) ;
157
167
fd. set_cloexec ( ) ?;
158
168
Ok ( fd)
159
169
} ;
160
- static TRY_CLOEXEC : AtomicBool =
161
- AtomicBool :: new ( !cfg ! ( target_os = "android" ) ) ;
170
+ static TRY_CLOEXEC : AtomicBool = AtomicBool :: new ( !cfg ! ( target_os = "android" ) ) ;
162
171
let fd = self . raw ( ) ;
163
172
if TRY_CLOEXEC . load ( Ordering :: Relaxed ) {
164
173
match cvt ( unsafe { netc:: fcntl ( fd, F_DUPFD_CLOEXEC , 0 ) } ) {
@@ -194,7 +203,9 @@ impl<'a> Read for &'a NetFileDesc {
194
203
}
195
204
196
205
impl AsInner < c_int > for NetFileDesc {
197
- fn as_inner ( & self ) -> & c_int { & self . fd }
206
+ fn as_inner ( & self ) -> & c_int {
207
+ & self . fd
208
+ }
198
209
}
199
210
200
211
impl Drop for NetFileDesc {
0 commit comments