@@ -5,7 +5,7 @@ use crate::{
55 io:: { self , BorrowedBuf , BorrowedCursor , ErrorKind , IoSlice , IoSliceMut } ,
66 mem,
77 net:: { Shutdown , SocketAddr } ,
8- os:: solid:: io:: { AsRawFd , FromRawFd , IntoRawFd } ,
8+ os:: solid:: io:: { AsRawFd , FromRawFd , IntoRawFd , OwnedFd } ,
99 ptr, str,
1010 sys_common:: net:: { getsockopt, setsockopt, sockaddr_to_addr} ,
1111 sys_common:: IntoInner ,
@@ -29,95 +29,6 @@ const fn max_iov() -> usize {
2929 1024
3030}
3131
32- /// A file descriptor.
33- #[ rustc_layout_scalar_valid_range_start( 0 ) ]
34- // libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
35- // 32-bit c_int. Below is -2, in two's complement, but that only works out
36- // because c_int is 32 bits.
37- #[ rustc_layout_scalar_valid_range_end( 0xFF_FF_FF_FE ) ]
38- struct FileDesc {
39- fd : c_int ,
40- }
41-
42- impl FileDesc {
43- #[ inline]
44- fn new ( fd : c_int ) -> FileDesc {
45- assert_ne ! ( fd, -1i32 ) ;
46- // Safety: we just asserted that the value is in the valid range and
47- // isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
48- unsafe { FileDesc { fd } }
49- }
50-
51- #[ inline]
52- fn raw ( & self ) -> c_int {
53- self . fd
54- }
55-
56- /// Extracts the actual file descriptor without closing it.
57- #[ inline]
58- fn into_raw ( self ) -> c_int {
59- let fd = self . fd ;
60- mem:: forget ( self ) ;
61- fd
62- }
63-
64- fn read ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
65- let ret = cvt ( unsafe {
66- netc:: read ( self . fd , buf. as_mut_ptr ( ) as * mut c_void , cmp:: min ( buf. len ( ) , READ_LIMIT ) )
67- } ) ?;
68- Ok ( ret as usize )
69- }
70-
71- fn read_vectored ( & self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
72- let ret = cvt ( unsafe {
73- netc:: readv (
74- self . fd ,
75- bufs. as_ptr ( ) as * const netc:: iovec ,
76- cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
77- )
78- } ) ?;
79- Ok ( ret as usize )
80- }
81-
82- #[ inline]
83- fn is_read_vectored ( & self ) -> bool {
84- true
85- }
86-
87- fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
88- let ret = cvt ( unsafe {
89- netc:: write ( self . fd , buf. as_ptr ( ) as * const c_void , cmp:: min ( buf. len ( ) , READ_LIMIT ) )
90- } ) ?;
91- Ok ( ret as usize )
92- }
93-
94- fn write_vectored ( & self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
95- let ret = cvt ( unsafe {
96- netc:: writev (
97- self . fd ,
98- bufs. as_ptr ( ) as * const netc:: iovec ,
99- cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
100- )
101- } ) ?;
102- Ok ( ret as usize )
103- }
104-
105- #[ inline]
106- fn is_write_vectored ( & self ) -> bool {
107- true
108- }
109-
110- fn duplicate ( & self ) -> io:: Result < FileDesc > {
111- cvt ( unsafe { netc:: dup ( self . fd ) } ) . map ( Self :: new)
112- }
113- }
114-
115- impl Drop for FileDesc {
116- fn drop ( & mut self ) {
117- unsafe { netc:: close ( self . fd ) } ;
118- }
119- }
120-
12132#[ doc( hidden) ]
12233pub trait IsMinusOne {
12334 fn is_minus_one ( & self ) -> bool ;
@@ -201,7 +112,7 @@ pub(super) fn decode_error_kind(er: abi::ER) -> ErrorKind {
201112
202113pub fn init ( ) { }
203114
204- pub struct Socket ( FileDesc ) ;
115+ pub struct Socket ( OwnedFd ) ;
205116
206117impl Socket {
207118 pub fn new ( addr : & SocketAddr , ty : c_int ) -> io:: Result < Socket > {
@@ -215,18 +126,15 @@ impl Socket {
215126 pub fn new_raw ( fam : c_int , ty : c_int ) -> io:: Result < Socket > {
216127 unsafe {
217128 let fd = cvt ( netc:: socket ( fam, ty, 0 ) ) ?;
218- let fd = FileDesc :: new ( fd) ;
219- let socket = Socket ( fd) ;
220-
221- Ok ( socket)
129+ Ok ( Self :: from_raw_fd ( fd) )
222130 }
223131 }
224132
225133 pub fn connect_timeout ( & self , addr : & SocketAddr , timeout : Duration ) -> io:: Result < ( ) > {
226134 self . set_nonblocking ( true ) ?;
227135 let r = unsafe {
228136 let ( addr, len) = addr. into_inner ( ) ;
229- cvt ( netc:: connect ( self . 0 . raw ( ) , addr. as_ptr ( ) , len) )
137+ cvt ( netc:: connect ( self . as_raw_fd ( ) , addr. as_ptr ( ) , len) )
230138 } ;
231139 self . set_nonblocking ( false ) ?;
232140
@@ -250,14 +158,14 @@ impl Socket {
250158 timeout. tv_usec = 1 ;
251159 }
252160
253- let fds = netc:: fd_set { num_fds : 1 , fds : [ self . 0 . raw ( ) ] } ;
161+ let fds = netc:: fd_set { num_fds : 1 , fds : [ self . as_raw_fd ( ) ] } ;
254162
255163 let mut writefds = fds;
256164 let mut errorfds = fds;
257165
258166 let n = unsafe {
259167 cvt ( netc:: select (
260- self . 0 . raw ( ) + 1 ,
168+ self . as_raw_fd ( ) + 1 ,
261169 ptr:: null_mut ( ) ,
262170 & mut writefds,
263171 & mut errorfds,
@@ -280,18 +188,17 @@ impl Socket {
280188 }
281189
282190 pub fn accept ( & self , storage : * mut sockaddr , len : * mut socklen_t ) -> io:: Result < Socket > {
283- let fd = cvt_r ( || unsafe { netc:: accept ( self . 0 . raw ( ) , storage, len) } ) ?;
284- let fd = FileDesc :: new ( fd) ;
285- Ok ( Socket ( fd) )
191+ let fd = cvt_r ( || unsafe { netc:: accept ( self . as_raw_fd ( ) , storage, len) } ) ?;
192+ unsafe { Ok ( Self :: from_raw_fd ( fd) ) }
286193 }
287194
288195 pub fn duplicate ( & self ) -> io:: Result < Socket > {
289- self . 0 . duplicate ( ) . map ( Socket )
196+ Ok ( Self ( self . 0 . try_clone ( ) ? ) )
290197 }
291198
292199 fn recv_with_flags ( & self , mut buf : BorrowedCursor < ' _ > , flags : c_int ) -> io:: Result < ( ) > {
293200 let ret = cvt ( unsafe {
294- netc:: recv ( self . 0 . raw ( ) , buf. as_mut ( ) . as_mut_ptr ( ) . cast ( ) , buf. capacity ( ) , flags)
201+ netc:: recv ( self . as_raw_fd ( ) , buf. as_mut ( ) . as_mut_ptr ( ) . cast ( ) , buf. capacity ( ) , flags)
295202 } ) ?;
296203 unsafe {
297204 buf. advance ( ret as usize ) ;
@@ -316,12 +223,19 @@ impl Socket {
316223 }
317224
318225 pub fn read_vectored ( & self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
319- self . 0 . read_vectored ( bufs)
226+ let ret = cvt ( unsafe {
227+ netc:: readv (
228+ self . as_raw_fd ( ) ,
229+ bufs. as_ptr ( ) as * const netc:: iovec ,
230+ cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
231+ )
232+ } ) ?;
233+ Ok ( ret as usize )
320234 }
321235
322236 #[ inline]
323237 pub fn is_read_vectored ( & self ) -> bool {
324- self . 0 . is_read_vectored ( )
238+ true
325239 }
326240
327241 fn recv_from_with_flags (
@@ -334,7 +248,7 @@ impl Socket {
334248
335249 let n = cvt ( unsafe {
336250 netc:: recvfrom (
337- self . 0 . raw ( ) ,
251+ self . as_raw_fd ( ) ,
338252 buf. as_mut_ptr ( ) as * mut c_void ,
339253 buf. len ( ) ,
340254 flags,
@@ -354,16 +268,30 @@ impl Socket {
354268 }
355269
356270 pub fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
357- self . 0 . write ( buf)
271+ let ret = cvt ( unsafe {
272+ netc:: write (
273+ self . as_raw_fd ( ) ,
274+ buf. as_ptr ( ) as * const c_void ,
275+ cmp:: min ( buf. len ( ) , READ_LIMIT ) ,
276+ )
277+ } ) ?;
278+ Ok ( ret as usize )
358279 }
359280
360281 pub fn write_vectored ( & self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
361- self . 0 . write_vectored ( bufs)
282+ let ret = cvt ( unsafe {
283+ netc:: writev (
284+ self . as_raw_fd ( ) ,
285+ bufs. as_ptr ( ) as * const netc:: iovec ,
286+ cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
287+ )
288+ } ) ?;
289+ Ok ( ret as usize )
362290 }
363291
364292 #[ inline]
365293 pub fn is_write_vectored ( & self ) -> bool {
366- self . 0 . is_write_vectored ( )
294+ true
367295 }
368296
369297 pub fn set_timeout ( & self , dur : Option < Duration > , kind : c_int ) -> io:: Result < ( ) > {
@@ -409,7 +337,7 @@ impl Socket {
409337 Shutdown :: Read => netc:: SHUT_RD ,
410338 Shutdown :: Both => netc:: SHUT_RDWR ,
411339 } ;
412- cvt ( unsafe { netc:: shutdown ( self . 0 . raw ( ) , how) } ) ?;
340+ cvt ( unsafe { netc:: shutdown ( self . as_raw_fd ( ) , how) } ) ?;
413341 Ok ( ( ) )
414342 }
415343
@@ -440,7 +368,7 @@ impl Socket {
440368 pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
441369 let mut nonblocking = nonblocking as c_int ;
442370 cvt ( unsafe {
443- netc:: ioctl ( self . 0 . raw ( ) , netc:: FIONBIO , ( & mut nonblocking) as * mut c_int as _ )
371+ netc:: ioctl ( self . as_raw_fd ( ) , netc:: FIONBIO , ( & mut nonblocking) as * mut c_int as _ )
444372 } )
445373 . map ( drop)
446374 }
@@ -452,27 +380,27 @@ impl Socket {
452380
453381 // This method is used by sys_common code to abstract over targets.
454382 pub fn as_raw ( & self ) -> c_int {
455- self . 0 . raw ( )
383+ self . as_raw_fd ( )
456384 }
457385}
458386
459387impl AsRawFd for Socket {
460388 #[ inline]
461389 fn as_raw_fd ( & self ) -> c_int {
462- self . 0 . fd
390+ self . 0 . as_raw_fd ( )
463391 }
464392}
465393
466394impl FromRawFd for Socket {
467395 #[ inline]
468396 unsafe fn from_raw_fd ( fd : c_int ) -> Socket {
469- Socket ( FileDesc :: new ( fd) )
397+ unsafe { Self ( FromRawFd :: from_raw_fd ( fd) ) }
470398 }
471399}
472400
473401impl IntoRawFd for Socket {
474402 #[ inline]
475403 fn into_raw_fd ( self ) -> c_int {
476- self . 0 . into_raw ( )
404+ self . 0 . into_raw_fd ( )
477405 }
478406}
0 commit comments