@@ -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 ;
@@ -206,7 +117,7 @@ pub(super) fn decode_error_kind(er: abi::ER) -> ErrorKind {
206
117
207
118
pub fn init ( ) { }
208
119
209
- pub struct Socket ( FileDesc ) ;
120
+ pub struct Socket ( OwnedFd ) ;
210
121
211
122
impl Socket {
212
123
pub fn new ( addr : & SocketAddr , ty : c_int ) -> io:: Result < Socket > {
@@ -220,16 +131,13 @@ impl Socket {
220
131
pub fn new_raw ( fam : c_int , ty : c_int ) -> io:: Result < Socket > {
221
132
unsafe {
222
133
let fd = cvt ( netc:: socket ( fam, ty, 0 ) ) ?;
223
- let fd = FileDesc :: new ( fd) ;
224
- let socket = Socket ( fd) ;
225
-
226
- Ok ( socket)
134
+ Ok ( Self :: from_raw_fd ( fd) )
227
135
}
228
136
}
229
137
230
138
pub fn connect ( & self , addr : & SocketAddr ) -> io:: Result < ( ) > {
231
139
let ( addr, len) = addr. into_inner ( ) ;
232
- cvt ( unsafe { netc:: connect ( self . 0 . raw ( ) , addr. as_ptr ( ) , len) } ) ?;
140
+ cvt ( unsafe { netc:: connect ( self . as_raw_fd ( ) , addr. as_ptr ( ) , len) } ) ?;
233
141
Ok ( ( ) )
234
142
}
235
143
@@ -258,14 +166,14 @@ impl Socket {
258
166
timeout. tv_usec = 1 ;
259
167
}
260
168
261
- let fds = netc:: fd_set { num_fds : 1 , fds : [ self . 0 . raw ( ) ] } ;
169
+ let fds = netc:: fd_set { num_fds : 1 , fds : [ self . as_raw_fd ( ) ] } ;
262
170
263
171
let mut writefds = fds;
264
172
let mut errorfds = fds;
265
173
266
174
let n = unsafe {
267
175
cvt ( netc:: select (
268
- self . 0 . raw ( ) + 1 ,
176
+ self . as_raw_fd ( ) + 1 ,
269
177
ptr:: null_mut ( ) ,
270
178
& mut writefds,
271
179
& mut errorfds,
@@ -288,18 +196,17 @@ impl Socket {
288
196
}
289
197
290
198
pub fn accept ( & self , storage : * mut sockaddr , len : * mut socklen_t ) -> io:: Result < Socket > {
291
- let fd = cvt_r ( || unsafe { netc:: accept ( self . 0 . raw ( ) , storage, len) } ) ?;
292
- let fd = FileDesc :: new ( fd) ;
293
- Ok ( Socket ( fd) )
199
+ let fd = cvt_r ( || unsafe { netc:: accept ( self . as_raw_fd ( ) , storage, len) } ) ?;
200
+ unsafe { Ok ( Self :: from_raw_fd ( fd) ) }
294
201
}
295
202
296
203
pub fn duplicate ( & self ) -> io:: Result < Socket > {
297
- self . 0 . duplicate ( ) . map ( Socket )
204
+ Ok ( Self ( self . 0 . try_clone ( ) ? ) )
298
205
}
299
206
300
207
fn recv_with_flags ( & self , mut buf : BorrowedCursor < ' _ > , flags : c_int ) -> io:: Result < ( ) > {
301
208
let ret = cvt ( unsafe {
302
- netc:: recv ( self . 0 . raw ( ) , buf. as_mut ( ) . as_mut_ptr ( ) . cast ( ) , buf. capacity ( ) , flags)
209
+ netc:: recv ( self . as_raw_fd ( ) , buf. as_mut ( ) . as_mut_ptr ( ) . cast ( ) , buf. capacity ( ) , flags)
303
210
} ) ?;
304
211
unsafe {
305
212
buf. advance ( ret as usize ) ;
@@ -324,12 +231,19 @@ impl Socket {
324
231
}
325
232
326
233
pub fn read_vectored ( & self , bufs : & mut [ IoSliceMut < ' _ > ] ) -> io:: Result < usize > {
327
- self . 0 . read_vectored ( bufs)
234
+ let ret = cvt ( unsafe {
235
+ netc:: readv (
236
+ self . as_raw_fd ( ) ,
237
+ bufs. as_ptr ( ) as * const netc:: iovec ,
238
+ cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
239
+ )
240
+ } ) ?;
241
+ Ok ( ret as usize )
328
242
}
329
243
330
244
#[ inline]
331
245
pub fn is_read_vectored ( & self ) -> bool {
332
- self . 0 . is_read_vectored ( )
246
+ true
333
247
}
334
248
335
249
fn recv_from_with_flags (
@@ -342,7 +256,7 @@ impl Socket {
342
256
343
257
let n = cvt ( unsafe {
344
258
netc:: recvfrom (
345
- self . 0 . raw ( ) ,
259
+ self . as_raw_fd ( ) ,
346
260
buf. as_mut_ptr ( ) as * mut c_void ,
347
261
buf. len ( ) ,
348
262
flags,
@@ -362,16 +276,30 @@ impl Socket {
362
276
}
363
277
364
278
pub fn write ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
365
- self . 0 . write ( buf)
279
+ let ret = cvt ( unsafe {
280
+ netc:: write (
281
+ self . as_raw_fd ( ) ,
282
+ buf. as_ptr ( ) as * const c_void ,
283
+ cmp:: min ( buf. len ( ) , READ_LIMIT ) ,
284
+ )
285
+ } ) ?;
286
+ Ok ( ret as usize )
366
287
}
367
288
368
289
pub fn write_vectored ( & self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
369
- self . 0 . write_vectored ( bufs)
290
+ let ret = cvt ( unsafe {
291
+ netc:: writev (
292
+ self . as_raw_fd ( ) ,
293
+ bufs. as_ptr ( ) as * const netc:: iovec ,
294
+ cmp:: min ( bufs. len ( ) , max_iov ( ) ) as c_int ,
295
+ )
296
+ } ) ?;
297
+ Ok ( ret as usize )
370
298
}
371
299
372
300
#[ inline]
373
301
pub fn is_write_vectored ( & self ) -> bool {
374
- self . 0 . is_write_vectored ( )
302
+ true
375
303
}
376
304
377
305
pub fn set_timeout ( & self , dur : Option < Duration > , kind : c_int ) -> io:: Result < ( ) > {
@@ -417,7 +345,7 @@ impl Socket {
417
345
Shutdown :: Read => netc:: SHUT_RD ,
418
346
Shutdown :: Both => netc:: SHUT_RDWR ,
419
347
} ;
420
- cvt ( unsafe { netc:: shutdown ( self . 0 . raw ( ) , how) } ) ?;
348
+ cvt ( unsafe { netc:: shutdown ( self . as_raw_fd ( ) , how) } ) ?;
421
349
Ok ( ( ) )
422
350
}
423
351
@@ -448,7 +376,7 @@ impl Socket {
448
376
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
449
377
let mut nonblocking = nonblocking as c_int ;
450
378
cvt ( unsafe {
451
- netc:: ioctl ( self . 0 . raw ( ) , netc:: FIONBIO , ( & mut nonblocking) as * mut c_int as _ )
379
+ netc:: ioctl ( self . as_raw_fd ( ) , netc:: FIONBIO , ( & mut nonblocking) as * mut c_int as _ )
452
380
} )
453
381
. map ( drop)
454
382
}
@@ -460,27 +388,27 @@ impl Socket {
460
388
461
389
// This method is used by sys_common code to abstract over targets.
462
390
pub fn as_raw ( & self ) -> c_int {
463
- self . 0 . raw ( )
391
+ self . as_raw_fd ( )
464
392
}
465
393
}
466
394
467
395
impl AsRawFd for Socket {
468
396
#[ inline]
469
397
fn as_raw_fd ( & self ) -> c_int {
470
- self . 0 . fd
398
+ self . 0 . as_raw_fd ( )
471
399
}
472
400
}
473
401
474
402
impl FromRawFd for Socket {
475
403
#[ inline]
476
404
unsafe fn from_raw_fd ( fd : c_int ) -> Socket {
477
- Socket ( FileDesc :: new ( fd) )
405
+ unsafe { Self ( FromRawFd :: from_raw_fd ( fd) ) }
478
406
}
479
407
}
480
408
481
409
impl IntoRawFd for Socket {
482
410
#[ inline]
483
411
fn into_raw_fd ( self ) -> c_int {
484
- self . 0 . into_raw ( )
412
+ self . 0 . into_raw_fd ( )
485
413
}
486
414
}
0 commit comments