4
4
5
5
use crate :: sealed:: Sealed ;
6
6
use crate :: sys_common:: AsInner ;
7
+ #[ cfg( target_os = "linux" ) ]
8
+ use crate :: time:: Duration ;
7
9
use crate :: { io, net} ;
8
10
9
11
/// Os-specific extensions for [`TcpStream`]
@@ -59,11 +61,13 @@ pub trait TcpStreamExt: Sealed {
59
61
60
62
/// A socket listener will be awakened solely when data arrives.
61
63
///
62
- /// The `accept` argument set the delay in seconds until the
64
+ /// The `accept` argument set the maximum delay until the
63
65
/// data is available to read, reducing the number of short lived
64
66
/// connections without data to process.
65
67
/// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
66
68
/// no necessity to set it after the `listen` call.
69
+ /// Note that the delay is expressed as Duration from user's perspective
70
+ /// the call rounds it down to the nearest second expressible as a `c_int`.
67
71
///
68
72
/// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
69
73
///
@@ -73,16 +77,17 @@ pub trait TcpStreamExt: Sealed {
73
77
/// #![feature(tcp_deferaccept)]
74
78
/// use std::net::TcpStream;
75
79
/// use std::os::linux::net::TcpStreamExt;
80
+ /// use std::time::Duration;
76
81
///
77
82
/// let stream = TcpStream::connect("127.0.0.1:8080")
78
83
/// .expect("Couldn't connect to the server...");
79
- /// stream.set_deferaccept(1 ).expect("set_deferaccept call failed");
84
+ /// stream.set_deferaccept(Duration::from_secs(1u64) ).expect("set_deferaccept call failed");
80
85
/// ```
81
86
#[ unstable( feature = "tcp_deferaccept" , issue = "119639" ) ]
82
87
#[ cfg( target_os = "linux" ) ]
83
- fn set_deferaccept ( & self , accept : u32 ) -> io:: Result < ( ) > ;
88
+ fn set_deferaccept ( & self , accept : Duration ) -> io:: Result < ( ) > ;
84
89
85
- /// Gets the accept delay value (in seconds) of the `TCP_DEFER_ACCEPT` option.
90
+ /// Gets the accept delay value of the `TCP_DEFER_ACCEPT` option.
86
91
///
87
92
/// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
88
93
///
@@ -92,15 +97,16 @@ pub trait TcpStreamExt: Sealed {
92
97
/// #![feature(tcp_deferaccept)]
93
98
/// use std::net::TcpStream;
94
99
/// use std::os::linux::net::TcpStreamExt;
100
+ /// use std::time::Duration;
95
101
///
96
102
/// let stream = TcpStream::connect("127.0.0.1:8080")
97
103
/// .expect("Couldn't connect to the server...");
98
- /// stream.set_deferaccept(1 ).expect("set_deferaccept call failed");
99
- /// assert_eq!(stream.deferaccept().unwrap_or(0 ), 1 );
104
+ /// stream.set_deferaccept(Duration::from_secs(1u64) ).expect("set_deferaccept call failed");
105
+ /// assert_eq!(stream.deferaccept().unwrap( ), Duration::from_secs(1u64) );
100
106
/// ```
101
107
#[ unstable( feature = "tcp_deferaccept" , issue = "119639" ) ]
102
108
#[ cfg( target_os = "linux" ) ]
103
- fn deferaccept ( & self ) -> io:: Result < u32 > ;
109
+ fn deferaccept ( & self ) -> io:: Result < Duration > ;
104
110
}
105
111
106
112
#[ stable( feature = "tcp_quickack" , since = "1.89.0" ) ]
@@ -117,12 +123,12 @@ impl TcpStreamExt for net::TcpStream {
117
123
}
118
124
119
125
#[ cfg( target_os = "linux" ) ]
120
- fn set_deferaccept ( & self , accept : u32 ) -> io:: Result < ( ) > {
126
+ fn set_deferaccept ( & self , accept : Duration ) -> io:: Result < ( ) > {
121
127
self . as_inner ( ) . as_inner ( ) . set_deferaccept ( accept)
122
128
}
123
129
124
130
#[ cfg( target_os = "linux" ) ]
125
- fn deferaccept ( & self ) -> io:: Result < u32 > {
131
+ fn deferaccept ( & self ) -> io:: Result < Duration > {
126
132
self . as_inner ( ) . as_inner ( ) . deferaccept ( )
127
133
}
128
134
}
0 commit comments