std: connect_timeout requires a positive Duration
This is only breaking if you were previously specifying a duration of zero for some mysterious reason. [breaking-change]
This commit is contained in:
@@ -93,7 +93,7 @@ impl TcpStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a TCP connection to a remote socket address, timing out after
|
/// Creates a TCP connection to a remote socket address, timing out after
|
||||||
/// the specified number of milliseconds.
|
/// the specified duration.
|
||||||
///
|
///
|
||||||
/// This is the same as the `connect` method, except that if the timeout
|
/// This is the same as the `connect` method, except that if the timeout
|
||||||
/// specified (in milliseconds) elapses before a connection is made an error
|
/// specified (in milliseconds) elapses before a connection is made an error
|
||||||
@@ -101,13 +101,19 @@ impl TcpStream {
|
|||||||
///
|
///
|
||||||
/// Note that the `addr` argument may one day be split into a separate host
|
/// Note that the `addr` argument may one day be split into a separate host
|
||||||
/// and port, similar to the API seen in `connect`.
|
/// and port, similar to the API seen in `connect`.
|
||||||
|
///
|
||||||
|
/// # Failure
|
||||||
|
///
|
||||||
|
/// Fails on a `timeout` of zero or negative duration.
|
||||||
#[experimental = "the timeout argument may eventually change types"]
|
#[experimental = "the timeout argument may eventually change types"]
|
||||||
pub fn connect_timeout(addr: SocketAddr,
|
pub fn connect_timeout(addr: SocketAddr,
|
||||||
timeout: Duration) -> IoResult<TcpStream> {
|
timeout: Duration) -> IoResult<TcpStream> {
|
||||||
|
assert!(timeout > Duration::milliseconds(0));
|
||||||
|
|
||||||
let SocketAddr { ip, port } = addr;
|
let SocketAddr { ip, port } = addr;
|
||||||
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
|
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
|
||||||
LocalIo::maybe_raise(|io| {
|
LocalIo::maybe_raise(|io| {
|
||||||
io.tcp_connect(addr, Some(in_ms_u64(timeout))).map(TcpStream::new)
|
io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
|
||||||
}).map_err(IoError::from_rtio_error)
|
}).map_err(IoError::from_rtio_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -444,12 +450,6 @@ impl Acceptor<TcpStream> for TcpAcceptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn in_ms_u64(d: Duration) -> u64 {
|
|
||||||
let ms = d.num_milliseconds();
|
|
||||||
if ms < 0 { return 0 };
|
|
||||||
return ms as u64;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[allow(experimental)]
|
#[allow(experimental)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|||||||
@@ -67,11 +67,17 @@ impl UnixStream {
|
|||||||
///
|
///
|
||||||
/// This function is similar to `connect`, except that if `timeout_ms`
|
/// This function is similar to `connect`, except that if `timeout_ms`
|
||||||
/// elapses the function will return an error of kind `TimedOut`.
|
/// elapses the function will return an error of kind `TimedOut`.
|
||||||
|
///
|
||||||
|
/// # Failure
|
||||||
|
///
|
||||||
|
/// Fails on a `timeout` of zero or negative duration.
|
||||||
#[experimental = "the timeout argument is likely to change types"]
|
#[experimental = "the timeout argument is likely to change types"]
|
||||||
pub fn connect_timeout<P: ToCStr>(path: &P,
|
pub fn connect_timeout<P: ToCStr>(path: &P,
|
||||||
timeout: Duration) -> IoResult<UnixStream> {
|
timeout: Duration) -> IoResult<UnixStream> {
|
||||||
|
assert!(timeout > Duration::milliseconds(0));
|
||||||
|
|
||||||
LocalIo::maybe_raise(|io| {
|
LocalIo::maybe_raise(|io| {
|
||||||
let s = io.unix_connect(&path.to_c_str(), Some(in_ms_u64(timeout)));
|
let s = io.unix_connect(&path.to_c_str(), Some(timeout.num_milliseconds() as u64));
|
||||||
s.map(|p| UnixStream { obj: p })
|
s.map(|p| UnixStream { obj: p })
|
||||||
}).map_err(IoError::from_rtio_error)
|
}).map_err(IoError::from_rtio_error)
|
||||||
}
|
}
|
||||||
@@ -509,6 +515,18 @@ mod tests {
|
|||||||
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok());
|
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok());
|
||||||
})
|
})
|
||||||
|
|
||||||
|
iotest!(fn connect_timeout_zero() {
|
||||||
|
let addr = next_test_unix();
|
||||||
|
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
|
||||||
|
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(0)).is_ok());
|
||||||
|
} #[should_fail])
|
||||||
|
|
||||||
|
iotest!(fn connect_timeout_negative() {
|
||||||
|
let addr = next_test_unix();
|
||||||
|
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
|
||||||
|
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(-1)).is_ok());
|
||||||
|
} #[should_fail])
|
||||||
|
|
||||||
iotest!(fn close_readwrite_smoke() {
|
iotest!(fn close_readwrite_smoke() {
|
||||||
let addr = next_test_unix();
|
let addr = next_test_unix();
|
||||||
let a = UnixListener::bind(&addr).listen().unwrap();
|
let a = UnixListener::bind(&addr).listen().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user