diff options
| author | Brian Anderson <banderson@mozilla.com> | 2014-07-31 17:03:27 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2014-08-13 11:31:48 -0700 |
| commit | 4475e6a095a8fe4459ac8b854ae2336e47aaafe5 (patch) | |
| tree | 5d31a17def0570cbf5ef3d8b9bb7d77dcdc6ab4d | |
| parent | 9fdcddb3178d4705db4aee5ee12c05796203658c (diff) | |
| download | rust-4475e6a095a8fe4459ac8b854ae2336e47aaafe5.tar.gz rust-4475e6a095a8fe4459ac8b854ae2336e47aaafe5.zip | |
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]
| -rw-r--r-- | src/libstd/io/net/tcp.rs | 16 | ||||
| -rw-r--r-- | src/libstd/io/net/unix.rs | 20 |
2 files changed, 27 insertions, 9 deletions
diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs index 84065bc936e..76368ac2821 100644 --- a/src/libstd/io/net/tcp.rs +++ b/src/libstd/io/net/tcp.rs @@ -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 { diff --git a/src/libstd/io/net/unix.rs b/src/libstd/io/net/unix.rs index 8847b4c9214..0b647e67a2d 100644 --- a/src/libstd/io/net/unix.rs +++ b/src/libstd/io/net/unix.rs @@ -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(); |
