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:
Brian Anderson
2014-07-31 17:03:27 -07:00
parent 9fdcddb317
commit 4475e6a095
2 changed files with 27 additions and 9 deletions

View File

@@ -93,7 +93,7 @@ impl TcpStream {
}
/// 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
/// 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
/// 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"]
pub fn connect_timeout(addr: SocketAddr,
timeout: Duration) -> IoResult<TcpStream> {
assert!(timeout > Duration::milliseconds(0));
let SocketAddr { ip, port } = addr;
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
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)
}
@@ -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)]
#[allow(experimental)]
mod test {

View File

@@ -67,11 +67,17 @@ impl UnixStream {
///
/// This function is similar to `connect`, except that if `timeout_ms`
/// 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"]
pub fn connect_timeout<P: ToCStr>(path: &P,
timeout: Duration) -> IoResult<UnixStream> {
assert!(timeout > Duration::milliseconds(0));
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 })
}).map_err(IoError::from_rtio_error)
}
@@ -509,6 +515,18 @@ mod tests {
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() {
let addr = next_test_unix();
let a = UnixListener::bind(&addr).listen().unwrap();