1
- use crate :: net:: { TcpStream , TcpListener } ;
2
- use crate :: sys;
3
-
4
1
use std:: io;
5
2
use std:: mem;
6
3
use std:: net:: SocketAddr ;
7
- use std:: time:: Duration ;
8
4
#[ cfg( unix) ]
9
- use std:: os:: unix:: io:: { AsRawFd , RawFd , FromRawFd } ;
5
+ use std:: os:: unix:: io:: { AsRawFd , FromRawFd , IntoRawFd , RawFd } ;
10
6
#[ cfg( windows) ]
11
7
use std:: os:: windows:: io:: { AsRawSocket , FromRawSocket , IntoRawSocket , RawSocket } ;
8
+ use std:: time:: Duration ;
9
+
10
+ use crate :: net:: { TcpListener , TcpStream } ;
11
+ use crate :: sys;
12
12
13
13
/// A non-blocking TCP socket used to configure a stream or listener.
14
14
///
@@ -50,18 +50,14 @@ impl TcpSocket {
50
50
///
51
51
/// This calls `socket(2)`.
52
52
pub fn new_v4 ( ) -> io:: Result < TcpSocket > {
53
- sys:: tcp:: new_v4_socket ( ) . map ( |sys| {
54
- TcpSocket { sys }
55
- } )
53
+ sys:: tcp:: new_v4_socket ( ) . map ( |sys| TcpSocket { sys } )
56
54
}
57
55
58
56
/// Create a new IPv6 TCP socket.
59
57
///
60
58
/// This calls `socket(2)`.
61
59
pub fn new_v6 ( ) -> io:: Result < TcpSocket > {
62
- sys:: tcp:: new_v6_socket ( ) . map ( |sys| {
63
- TcpSocket { sys }
64
- } )
60
+ sys:: tcp:: new_v6_socket ( ) . map ( |sys| TcpSocket { sys } )
65
61
}
66
62
67
63
pub ( crate ) fn new_for_addr ( addr : SocketAddr ) -> io:: Result < TcpSocket > {
@@ -230,7 +226,7 @@ impl TcpSocket {
230
226
/// let socket = TcpSocket::new_v6()?;
231
227
/// let keepalive = TcpKeepalive::default()
232
228
/// .with_time(Duration::from_secs(4));
233
- /// // Depending on the target operating system, we may also be able to
229
+ /// // Depending on the target operating system, we may also be able to
234
230
/// // configure the keepalive probe interval and/or the number of retries
235
231
/// // here as well.
236
232
///
@@ -274,13 +270,16 @@ impl TcpSocket {
274
270
///
275
271
/// Some platforms specify this value in seconds, so sub-second
276
272
/// specifications may be omitted.
277
- #[ cfg_attr( docsrs, doc( cfg( any(
278
- target_os = "linux" ,
279
- target_os = "macos" ,
280
- target_os = "ios" ,
281
- target_os = "freebsd" ,
282
- target_os = "netbsd" ,
283
- ) ) ) ) ]
273
+ #[ cfg_attr(
274
+ docsrs,
275
+ doc( cfg( any(
276
+ target_os = "linux" ,
277
+ target_os = "macos" ,
278
+ target_os = "ios" ,
279
+ target_os = "freebsd" ,
280
+ target_os = "netbsd" ,
281
+ ) ) )
282
+ ) ]
284
283
#[ cfg( any(
285
284
target_os = "linux" ,
286
285
target_os = "macos" ,
@@ -300,15 +299,18 @@ impl TcpSocket {
300
299
/// This returns the value of `TCP_KEEPCNT` on Unix operating systems that
301
300
/// support this option. On Windows, it is not possible to access the value
302
301
/// of TCP keepalive parameters after they have been set.
303
- #[ cfg_attr( docsrs, doc( cfg( any(
304
- target_os = "linux" ,
305
- target_os = "macos" ,
306
- target_os = "ios" ,
307
- target_os = "freebsd" ,
308
- target_os = "netbsd" ,
309
- ) ) ) ) ]
302
+ #[ cfg_attr(
303
+ docsrs,
304
+ doc( cfg( any(
305
+ target_os = "linux" ,
306
+ target_os = "macos" ,
307
+ target_os = "ios" ,
308
+ target_os = "freebsd" ,
309
+ target_os = "netbsd" ,
310
+ ) ) )
311
+ ) ]
310
312
#[ cfg( any(
311
- target_os = "linux" ,
313
+ target_os = "linux" ,
312
314
target_os = "macos" ,
313
315
target_os = "ios" ,
314
316
target_os = "freebsd" ,
@@ -332,6 +334,16 @@ impl Drop for TcpSocket {
332
334
}
333
335
}
334
336
337
+ #[ cfg( unix) ]
338
+ impl IntoRawFd for TcpSocket {
339
+ fn into_raw_fd ( self ) -> RawFd {
340
+ let ret = self . sys ;
341
+ // Avoid closing the socket
342
+ mem:: forget ( self ) ;
343
+ ret
344
+ }
345
+ }
346
+
335
347
#[ cfg( unix) ]
336
348
impl AsRawFd for TcpSocket {
337
349
fn as_raw_fd ( & self ) -> RawFd {
@@ -384,7 +396,9 @@ impl FromRawSocket for TcpSocket {
384
396
/// The caller is responsible for ensuring that the socket is in
385
397
/// non-blocking mode.
386
398
unsafe fn from_raw_socket ( socket : RawSocket ) -> TcpSocket {
387
- TcpSocket { sys : socket as sys:: tcp:: TcpSocket }
399
+ TcpSocket {
400
+ sys : socket as sys:: tcp:: TcpSocket ,
401
+ }
388
402
}
389
403
}
390
404
@@ -413,14 +427,17 @@ impl TcpKeepalive {
413
427
///
414
428
/// Some platforms specify this value in seconds, so sub-second
415
429
/// specifications may be omitted.
416
- #[ cfg_attr( docsrs, doc( cfg( any(
417
- target_os = "linux" ,
418
- target_os = "macos" ,
419
- target_os = "ios" ,
420
- target_os = "freebsd" ,
421
- target_os = "netbsd" ,
422
- target_os = "windows"
423
- ) ) ) ) ]
430
+ #[ cfg_attr(
431
+ docsrs,
432
+ doc( cfg( any(
433
+ target_os = "linux" ,
434
+ target_os = "macos" ,
435
+ target_os = "ios" ,
436
+ target_os = "freebsd" ,
437
+ target_os = "netbsd" ,
438
+ target_os = "windows"
439
+ ) ) )
440
+ ) ]
424
441
#[ cfg( any(
425
442
target_os = "linux" ,
426
443
target_os = "macos" ,
@@ -441,13 +458,16 @@ impl TcpKeepalive {
441
458
///
442
459
/// This will set the value of `TCP_KEEPCNT` on Unix operating systems that
443
460
/// support this option.
444
- #[ cfg_attr( docsrs, doc( cfg( any(
445
- target_os = "linux" ,
446
- target_os = "macos" ,
447
- target_os = "ios" ,
448
- target_os = "freebsd" ,
449
- target_os = "netbsd" ,
450
- ) ) ) ) ]
461
+ #[ cfg_attr(
462
+ docsrs,
463
+ doc( cfg( any(
464
+ target_os = "linux" ,
465
+ target_os = "macos" ,
466
+ target_os = "ios" ,
467
+ target_os = "freebsd" ,
468
+ target_os = "netbsd" ,
469
+ ) ) )
470
+ ) ]
451
471
#[ cfg( any(
452
472
target_os = "linux" ,
453
473
target_os = "macos" ,
@@ -466,4 +486,4 @@ impl TcpKeepalive {
466
486
pub fn new ( ) -> Self {
467
487
Self :: default ( )
468
488
}
469
- }
489
+ }
0 commit comments