44
55use crate :: sealed:: Sealed ;
66use crate :: sys_common:: AsInner ;
7+ use crate :: time:: Duration ;
78use crate :: { io, net} ;
89
910/// Os-specific extensions for [`TcpStream`]
@@ -59,11 +60,13 @@ pub trait TcpStreamExt: Sealed {
5960
6061 /// A socket listener will be awakened solely when data arrives.
6162 ///
62- /// The `accept` argument set the delay in seconds until the
63+ /// The `accept` argument set the maximum delay until the
6364 /// data is available to read, reducing the number of short lived
6465 /// connections without data to process.
6566 /// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
6667 /// no necessity to set it after the `listen` call.
68+ /// Note that the delay is expressed as Duration from user's perspective
69+ /// the call rounds it down to the nearest second expressible as a `c_int`.
6770 ///
6871 /// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
6972 ///
@@ -73,16 +76,17 @@ pub trait TcpStreamExt: Sealed {
7376 /// #![feature(tcp_deferaccept)]
7477 /// use std::net::TcpStream;
7578 /// use std::os::linux::net::TcpStreamExt;
79+ /// use std::time::Duration;
7680 ///
7781 /// let stream = TcpStream::connect("127.0.0.1:8080")
7882 /// .expect("Couldn't connect to the server...");
79- /// stream.set_deferaccept(1 ).expect("set_deferaccept call failed");
83+ /// stream.set_deferaccept(Duration::from_secs(1u64) ).expect("set_deferaccept call failed");
8084 /// ```
8185 #[ unstable( feature = "tcp_deferaccept" , issue = "119639" ) ]
8286 #[ cfg( target_os = "linux" ) ]
83- fn set_deferaccept ( & self , accept : u32 ) -> io:: Result < ( ) > ;
87+ fn set_deferaccept ( & self , accept : Duration ) -> io:: Result < ( ) > ;
8488
85- /// Gets the accept delay value (in seconds) of the `TCP_DEFER_ACCEPT` option.
89+ /// Gets the accept delay value of the `TCP_DEFER_ACCEPT` option.
8690 ///
8791 /// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
8892 ///
@@ -92,15 +96,16 @@ pub trait TcpStreamExt: Sealed {
9296 /// #![feature(tcp_deferaccept)]
9397 /// use std::net::TcpStream;
9498 /// use std::os::linux::net::TcpStreamExt;
99+ /// use std::time::Duration;
95100 ///
96101 /// let stream = TcpStream::connect("127.0.0.1:8080")
97102 /// .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 );
103+ /// stream.set_deferaccept(Duration::from_secs(1u64) ).expect("set_deferaccept call failed");
104+ /// assert_eq!(stream.deferaccept().unwrap( ), Duration::from_secs(1u64) );
100105 /// ```
101106 #[ unstable( feature = "tcp_deferaccept" , issue = "119639" ) ]
102107 #[ cfg( target_os = "linux" ) ]
103- fn deferaccept ( & self ) -> io:: Result < u32 > ;
108+ fn deferaccept ( & self ) -> io:: Result < Duration > ;
104109}
105110
106111#[ stable( feature = "tcp_quickack" , since = "1.89.0" ) ]
@@ -117,12 +122,12 @@ impl TcpStreamExt for net::TcpStream {
117122 }
118123
119124 #[ cfg( target_os = "linux" ) ]
120- fn set_deferaccept ( & self , accept : u32 ) -> io:: Result < ( ) > {
125+ fn set_deferaccept ( & self , accept : Duration ) -> io:: Result < ( ) > {
121126 self . as_inner ( ) . as_inner ( ) . set_deferaccept ( accept)
122127 }
123128
124129 #[ cfg( target_os = "linux" ) ]
125- fn deferaccept ( & self ) -> io:: Result < u32 > {
130+ fn deferaccept ( & self ) -> io:: Result < Duration > {
126131 self . as_inner ( ) . as_inner ( ) . deferaccept ( )
127132 }
128133}
0 commit comments