11#![ unstable( reason = "not public" , issue = "none" , feature = "net_fd" ) ]
22
33use crate :: cmp;
4- use crate :: io:: { self , Read , Initializer , IoSlice , IoSliceMut } ;
4+ use crate :: io:: { self , Initializer , IoSlice , IoSliceMut , Read } ;
55use 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+ } ;
710use crate :: sys_common:: AsInner ;
811
912#[ derive( Debug ) ]
@@ -32,7 +35,9 @@ impl NetFileDesc {
3235 NetFileDesc { fd }
3336 }
3437
35- pub fn raw ( & self ) -> c_int { self . fd }
38+ pub fn raw ( & self ) -> c_int {
39+ self . fd
40+ }
3641
3742 /// Extracts the actual file descriptor without closing it.
3843 pub fn into_raw ( self ) -> c_int {
@@ -43,18 +48,18 @@ impl NetFileDesc {
4348
4449 pub fn read ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
4550 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 ( ) ) )
4952 } ) ?;
5053 Ok ( ret as usize )
5154 }
5255
5356 pub fn read_vectored ( & self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
5457 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+ )
5863 } ) ?;
5964 Ok ( ret as usize )
6065 }
@@ -66,51 +71,56 @@ impl NetFileDesc {
6671
6772 pub fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
6873 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 ( ) ) )
7275 } ) ?;
7376 Ok ( ret as usize )
7477 }
7578
7679 pub fn write_vectored ( & self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
7780 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+ )
8186 } ) ?;
8287 Ok ( ret as usize )
8388 }
8489
8590 #[ cfg( target_os = "linux" ) ]
8691 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+ ) ) ) ]
100105 pub fn set_cloexec ( & self ) -> io:: Result < ( ) > {
101106 unsafe {
102107 cvt ( netc:: ioctl ( self . fd , netc:: FIOCLEX ) ) ?;
103108 Ok ( ( ) )
104109 }
105110 }
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+ ) ) ]
114124 pub fn set_cloexec ( & self ) -> io:: Result < ( ) > {
115125 unsafe {
116126 let previous = cvt ( netc:: fcntl ( self . fd , netc:: F_GETFD ) ) ?;
@@ -149,16 +159,15 @@ impl NetFileDesc {
149159 // [1]: http://comments.gmane.org/gmane.linux.lib.musl.general/2963
150160 #[ cfg( any( target_os = "android" , target_os = "haiku" ) ) ]
151161 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" ) ) ) ]
153163 use netc:: F_DUPFD_CLOEXEC ;
154164
155165 let make_filedesc = |fd| {
156166 let fd = NetFileDesc :: new ( fd) ;
157167 fd. set_cloexec ( ) ?;
158168 Ok ( fd)
159169 } ;
160- static TRY_CLOEXEC : AtomicBool =
161- AtomicBool :: new ( !cfg ! ( target_os = "android" ) ) ;
170+ static TRY_CLOEXEC : AtomicBool = AtomicBool :: new ( !cfg ! ( target_os = "android" ) ) ;
162171 let fd = self . raw ( ) ;
163172 if TRY_CLOEXEC . load ( Ordering :: Relaxed ) {
164173 match cvt ( unsafe { netc:: fcntl ( fd, F_DUPFD_CLOEXEC , 0 ) } ) {
@@ -194,7 +203,9 @@ impl<'a> Read for &'a NetFileDesc {
194203}
195204
196205impl 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+ }
198209}
199210
200211impl Drop for NetFileDesc {
0 commit comments