@@ -5,7 +5,7 @@ use crate::{
5
5
io:: { self , BorrowedBuf , BorrowedCursor , ErrorKind , IoSlice , IoSliceMut } ,
6
6
mem,
7
7
net:: { Shutdown , SocketAddr } ,
8
- os:: solid:: io:: { AsRawFd , FromRawFd , IntoRawFd } ,
8
+ os:: solid:: io:: { AsRawFd , FromRawFd , IntoRawFd , OwnedFd } ,
9
9
ptr, str,
10
10
sys_common:: net:: { getsockopt, setsockopt, sockaddr_to_addr} ,
11
11
sys_common:: IntoInner ,
@@ -29,95 +29,6 @@ const fn max_iov() -> usize {
29
29
1024
30
30
}
31
31
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
-
121
32
#[ doc( hidden) ]
122
33
pub trait IsMinusOne {
123
34
fn is_minus_one ( & self ) -> bool ;
@@ -201,7 +112,7 @@ pub(super) fn decode_error_kind(er: abi::ER) -> ErrorKind {
201
112
202
113
pub fn init ( ) { }
203
114
204
- pub struct Socket ( FileDesc ) ;
115
+ pub struct Socket ( OwnedFd ) ;
205
116
206
117
impl Socket {
207
118
pub fn new ( addr : & SocketAddr , ty : c_int ) -> io:: Result < Socket > {
@@ -215,18 +126,15 @@ impl Socket {
215
126
pub fn new_raw ( fam : c_int , ty : c_int ) -> io:: Result < Socket > {
216
127
unsafe {
217
128
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) )
222
130
}
223
131
}
224
132
225
133
pub fn connect_timeout ( & self , addr : & SocketAddr , timeout : Duration ) -> io:: Result < ( ) > {
226
134
self . set_nonblocking ( true ) ?;
227
135
let r = unsafe {
228
136
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) )
230
138
} ;
231
139
self . set_nonblocking ( false ) ?;
232
140
@@ -250,14 +158,14 @@ impl Socket {
250
158
timeout. tv_usec = 1 ;
251
159
}
252
160
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 ( ) ] } ;
254
162
255
163
let mut writefds = fds;
256
164
let mut errorfds = fds;
257
165
258
166
let n = unsafe {
259
167
cvt ( netc:: select (
260
- self . 0 . raw ( ) + 1 ,
168
+ self . as_raw_fd ( ) + 1 ,
261
169
ptr:: null_mut ( ) ,
262
170
& mut writefds,
263
171
& mut errorfds,
@@ -280,18 +188,17 @@ impl Socket {
280
188
}
281
189
282
190
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) ) }
286
193
}
287
194
288
195
pub fn duplicate ( & self ) -> io:: Result < Socket > {
289
- self . 0 . duplicate ( ) . map ( Socket )
196
+ Ok ( Self ( self . 0 . try_clone ( ) ? ) )
290
197
}
291
198
292
199
fn recv_with_flags ( & self , mut buf : BorrowedCursor < ' _ > , flags : c_int ) -> io:: Result < ( ) > {
293
200
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)
295
202
} ) ?;
296
203
unsafe {
297
204
buf. advance ( ret as usize ) ;
@@ -316,12 +223,19 @@ impl Socket {
316
223
}
317
224
318
225
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 )
320
234
}
321
235
322
236
#[ inline]
323
237
pub fn is_read_vectored ( & self ) -> bool {
324
- self . 0 . is_read_vectored ( )
238
+ true
325
239
}
326
240
327
241
fn recv_from_with_flags (
@@ -334,7 +248,7 @@ impl Socket {
334
248
335
249
let n = cvt ( unsafe {
336
250
netc:: recvfrom (
337
- self . 0 . raw ( ) ,
251
+ self . as_raw_fd ( ) ,
338
252
buf. as_mut_ptr ( ) as * mut c_void ,
339
253
buf. len ( ) ,
340
254
flags,
@@ -354,16 +268,30 @@ impl Socket {
354
268
}
355
269
356
270
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 )
358
279
}
359
280
360
281
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 )
362
290
}
363
291
364
292
#[ inline]
365
293
pub fn is_write_vectored ( & self ) -> bool {
366
- self . 0 . is_write_vectored ( )
294
+ true
367
295
}
368
296
369
297
pub fn set_timeout ( & self , dur : Option < Duration > , kind : c_int ) -> io:: Result < ( ) > {
@@ -409,7 +337,7 @@ impl Socket {
409
337
Shutdown :: Read => netc:: SHUT_RD ,
410
338
Shutdown :: Both => netc:: SHUT_RDWR ,
411
339
} ;
412
- cvt ( unsafe { netc:: shutdown ( self . 0 . raw ( ) , how) } ) ?;
340
+ cvt ( unsafe { netc:: shutdown ( self . as_raw_fd ( ) , how) } ) ?;
413
341
Ok ( ( ) )
414
342
}
415
343
@@ -440,7 +368,7 @@ impl Socket {
440
368
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
441
369
let mut nonblocking = nonblocking as c_int ;
442
370
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 _ )
444
372
} )
445
373
. map ( drop)
446
374
}
@@ -452,27 +380,27 @@ impl Socket {
452
380
453
381
// This method is used by sys_common code to abstract over targets.
454
382
pub fn as_raw ( & self ) -> c_int {
455
- self . 0 . raw ( )
383
+ self . as_raw_fd ( )
456
384
}
457
385
}
458
386
459
387
impl AsRawFd for Socket {
460
388
#[ inline]
461
389
fn as_raw_fd ( & self ) -> c_int {
462
- self . 0 . fd
390
+ self . 0 . as_raw_fd ( )
463
391
}
464
392
}
465
393
466
394
impl FromRawFd for Socket {
467
395
#[ inline]
468
396
unsafe fn from_raw_fd ( fd : c_int ) -> Socket {
469
- Socket ( FileDesc :: new ( fd) )
397
+ unsafe { Self ( FromRawFd :: from_raw_fd ( fd) ) }
470
398
}
471
399
}
472
400
473
401
impl IntoRawFd for Socket {
474
402
#[ inline]
475
403
fn into_raw_fd ( self ) -> c_int {
476
- self . 0 . into_raw ( )
404
+ self . 0 . into_raw_fd ( )
477
405
}
478
406
}
0 commit comments