44
55use crate :: sealed:: Sealed ;
66use crate :: sys_common:: AsInner ;
7+ #[ cfg( target_os = "linux" ) ]
8+ use crate :: time:: Duration ;
79use crate :: { io, net} ;
810
911/// Os-specific extensions for [`TcpStream`]
@@ -59,11 +61,13 @@ pub trait TcpStreamExt: Sealed {
5961
6062 /// A socket listener will be awakened solely when data arrives.
6163 ///
62- /// The `accept` argument set the delay in seconds until the
64+ /// The `accept` argument set the maximum delay until the
6365 /// data is available to read, reducing the number of short lived
6466 /// connections without data to process.
6567 /// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
6668 /// 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`.
6771 ///
6872 /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
6973 ///
@@ -73,16 +77,17 @@ pub trait TcpStreamExt: Sealed {
7377 /// #![feature(tcp_deferaccept)]
7478 /// use std::net::TcpStream;
7579 /// use std::os::linux::net::TcpStreamExt;
80+ /// use std::time::Duration;
7681 ///
7782 /// let stream = TcpStream::connect("127.0.0.1:8080")
7883 /// .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");
8085 /// ```
8186 #[ unstable( feature = "tcp_deferaccept" , issue = "119639" ) ]
8287 #[ cfg( target_os = "linux" ) ]
83- fn set_deferaccept ( & self , accept : u32 ) -> io:: Result < ( ) > ;
88+ fn set_deferaccept ( & self , accept : Duration ) -> io:: Result < ( ) > ;
8489
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.
8691 ///
8792 /// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
8893 ///
@@ -92,15 +97,16 @@ pub trait TcpStreamExt: Sealed {
9297 /// #![feature(tcp_deferaccept)]
9398 /// use std::net::TcpStream;
9499 /// use std::os::linux::net::TcpStreamExt;
100+ /// use std::time::Duration;
95101 ///
96102 /// let stream = TcpStream::connect("127.0.0.1:8080")
97103 /// .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) );
100106 /// ```
101107 #[ unstable( feature = "tcp_deferaccept" , issue = "119639" ) ]
102108 #[ cfg( target_os = "linux" ) ]
103- fn deferaccept ( & self ) -> io:: Result < u32 > ;
109+ fn deferaccept ( & self ) -> io:: Result < Duration > ;
104110}
105111
106112#[ stable( feature = "tcp_quickack" , since = "1.89.0" ) ]
@@ -117,12 +123,12 @@ impl TcpStreamExt for net::TcpStream {
117123 }
118124
119125 #[ cfg( target_os = "linux" ) ]
120- fn set_deferaccept ( & self , accept : u32 ) -> io:: Result < ( ) > {
126+ fn set_deferaccept ( & self , accept : Duration ) -> io:: Result < ( ) > {
121127 self . as_inner ( ) . as_inner ( ) . set_deferaccept ( accept)
122128 }
123129
124130 #[ cfg( target_os = "linux" ) ]
125- fn deferaccept ( & self ) -> io:: Result < u32 > {
131+ fn deferaccept ( & self ) -> io:: Result < Duration > {
126132 self . as_inner ( ) . as_inner ( ) . deferaccept ( )
127133 }
128134}
0 commit comments