Skip to content

Commit 28b5e45

Browse files
committed
auto merge of #15934 : brson/rust/dur, r=aturon
Currently, the Timer methods take an integer number of ms. This is considered a bug because a) types, b) some timers have ns precision. This plucks the `Duration` type from [rust-chrono](https://github.com/lifthrasiir/rust-chrono), plops it into `std::time`, and replaces the arguments to `sleep`, `oneshot`, and `periodic` timers with it. It leaves the old methods intact as `sleep_ms`, `oneshot_ms`, and `periodic_ms`, for convenience. Closes #11189. cc @lifthrasiir @aturon @kballard @alexcrichton
2 parents 0f09f51 + 075256a commit 28b5e45

19 files changed

+867
-79
lines changed

src/compiletest/runtest.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::os;
3030
use std::str;
3131
use std::string::String;
3232
use std::task;
33+
use std::time::Duration;
3334
use test::MetricMap;
3435

3536
pub fn run(config: Config, testfile: String) {
@@ -400,7 +401,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
400401
.expect(format!("failed to exec `{}`", config.adb_path).as_slice());
401402
loop {
402403
//waiting 1 second for gdbserver start
403-
timer::sleep(1000);
404+
timer::sleep(Duration::milliseconds(1000));
404405
let result = task::try(proc() {
405406
tcp::TcpStream::connect("127.0.0.1", 5039).unwrap();
406407
});

src/libgreen/sched.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,7 @@ mod test {
10291029
use std::rt::task::TaskOpts;
10301030
use std::rt::task::Task;
10311031
use std::rt::local::Local;
1032+
use std::time::Duration;
10321033

10331034
use {TaskState, PoolConfig, SchedPool};
10341035
use basic;
@@ -1291,7 +1292,7 @@ mod test {
12911292
// doesn't exit before emptying the work queue
12921293
pool.spawn(TaskOpts::new(), proc() {
12931294
spawn(proc() {
1294-
timer::sleep(10);
1295+
timer::sleep(Duration::milliseconds(10));
12951296
});
12961297
});
12971298

src/libstd/io/net/tcp.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ use io::net::addrinfo::get_host_addresses;
2727
use io::net::ip::SocketAddr;
2828
use io::{IoError, ConnectionFailed, InvalidInput};
2929
use io::{Reader, Writer, Listener, Acceptor};
30+
use io::{standard_error, TimedOut};
3031
use from_str::FromStr;
3132
use kinds::Send;
3233
use option::{None, Some, Option};
3334
use boxed::Box;
3435
use rt::rtio::{IoFactory, LocalIo, RtioSocket, RtioTcpListener};
3536
use rt::rtio::{RtioTcpAcceptor, RtioTcpStream};
3637
use rt::rtio;
38+
use time::Duration;
3739

3840
/// A structure which represents a TCP stream between a local socket and a
3941
/// remote socket.
@@ -92,21 +94,28 @@ impl TcpStream {
9294
}
9395

9496
/// Creates a TCP connection to a remote socket address, timing out after
95-
/// the specified number of milliseconds.
97+
/// the specified duration.
9698
///
9799
/// This is the same as the `connect` method, except that if the timeout
98100
/// specified (in milliseconds) elapses before a connection is made an error
99101
/// will be returned. The error's kind will be `TimedOut`.
100102
///
101103
/// Note that the `addr` argument may one day be split into a separate host
102104
/// and port, similar to the API seen in `connect`.
105+
///
106+
/// If a `timeout` with zero or negative duration is specified then
107+
/// the function returns `Err`, with the error kind set to `TimedOut`.
103108
#[experimental = "the timeout argument may eventually change types"]
104109
pub fn connect_timeout(addr: SocketAddr,
105-
timeout_ms: u64) -> IoResult<TcpStream> {
110+
timeout: Duration) -> IoResult<TcpStream> {
111+
if timeout <= Duration::milliseconds(0) {
112+
return Err(standard_error(TimedOut));
113+
}
114+
106115
let SocketAddr { ip, port } = addr;
107116
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
108117
LocalIo::maybe_raise(|io| {
109-
io.tcp_connect(addr, Some(timeout_ms)).map(TcpStream::new)
118+
io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
110119
}).map_err(IoError::from_rtio_error)
111120
}
112121

@@ -164,13 +173,14 @@ impl TcpStream {
164173
/// # #![allow(unused_must_use)]
165174
/// use std::io::timer;
166175
/// use std::io::TcpStream;
176+
/// use std::time::Duration;
167177
///
168178
/// let mut stream = TcpStream::connect("127.0.0.1", 34254).unwrap();
169179
/// let stream2 = stream.clone();
170180
///
171181
/// spawn(proc() {
172182
/// // close this stream after one second
173-
/// timer::sleep(1000);
183+
/// timer::sleep(Duration::seconds(1));
174184
/// let mut stream = stream2;
175185
/// stream.close_read();
176186
/// });

src/libstd/io/net/unix.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ use prelude::*;
2929
use c_str::ToCStr;
3030
use clone::Clone;
3131
use io::{Listener, Acceptor, Reader, Writer, IoResult, IoError};
32+
use io::{standard_error, TimedOut};
3233
use kinds::Send;
3334
use boxed::Box;
3435
use rt::rtio::{IoFactory, LocalIo, RtioUnixListener};
3536
use rt::rtio::{RtioUnixAcceptor, RtioPipe};
37+
use time::Duration;
3638

3739
/// A stream which communicates over a named pipe.
3840
pub struct UnixStream {
@@ -66,11 +68,18 @@ impl UnixStream {
6668
///
6769
/// This function is similar to `connect`, except that if `timeout_ms`
6870
/// elapses the function will return an error of kind `TimedOut`.
71+
///
72+
/// If a `timeout` with zero or negative duration is specified then
73+
/// the function returns `Err`, with the error kind set to `TimedOut`.
6974
#[experimental = "the timeout argument is likely to change types"]
7075
pub fn connect_timeout<P: ToCStr>(path: &P,
71-
timeout_ms: u64) -> IoResult<UnixStream> {
76+
timeout: Duration) -> IoResult<UnixStream> {
77+
if timeout <= Duration::milliseconds(0) {
78+
return Err(standard_error(TimedOut));
79+
}
80+
7281
LocalIo::maybe_raise(|io| {
73-
let s = io.unix_connect(&path.to_c_str(), Some(timeout_ms));
82+
let s = io.unix_connect(&path.to_c_str(), Some(timeout.num_milliseconds() as u64));
7483
s.map(|p| UnixStream { obj: p })
7584
}).map_err(IoError::from_rtio_error)
7685
}
@@ -499,13 +508,25 @@ mod tests {
499508

500509
iotest!(fn connect_timeout_error() {
501510
let addr = next_test_unix();
502-
assert!(UnixStream::connect_timeout(&addr, 100).is_err());
511+
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_err());
503512
})
504513

505514
iotest!(fn connect_timeout_success() {
506515
let addr = next_test_unix();
507516
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
508-
assert!(UnixStream::connect_timeout(&addr, 100).is_ok());
517+
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok());
518+
})
519+
520+
iotest!(fn connect_timeout_zero() {
521+
let addr = next_test_unix();
522+
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
523+
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(0)).is_err());
524+
})
525+
526+
iotest!(fn connect_timeout_negative() {
527+
let addr = next_test_unix();
528+
let _a = UnixListener::bind(&addr).unwrap().listen().unwrap();
529+
assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(-1)).is_err());
509530
})
510531

511532
iotest!(fn close_readwrite_smoke() {

src/libstd/io/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ mod tests {
976976
assert!(!p.wait().unwrap().success());
977977
return
978978
}
979-
timer::sleep(100);
979+
timer::sleep(Duration::milliseconds(100));
980980
}
981981
fail!("never saw the child go away");
982982
})

src/libstd/io/signal.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ mod test_unix {
167167
use comm::Empty;
168168
use io::timer;
169169
use super::{Listener, Interrupt};
170+
use time::Duration;
170171

171172
fn sigint() {
172173
unsafe {
@@ -179,7 +180,7 @@ mod test_unix {
179180
let mut signal = Listener::new();
180181
signal.register(Interrupt).unwrap();
181182
sigint();
182-
timer::sleep(10);
183+
timer::sleep(Duration::milliseconds(10));
183184
match signal.rx.recv() {
184185
Interrupt => (),
185186
s => fail!("Expected Interrupt, got {:?}", s),
@@ -193,7 +194,7 @@ mod test_unix {
193194
s1.register(Interrupt).unwrap();
194195
s2.register(Interrupt).unwrap();
195196
sigint();
196-
timer::sleep(10);
197+
timer::sleep(Duration::milliseconds(10));
197198
match s1.rx.recv() {
198199
Interrupt => (),
199200
s => fail!("Expected Interrupt, got {:?}", s),
@@ -212,7 +213,7 @@ mod test_unix {
212213
s2.register(Interrupt).unwrap();
213214
s2.unregister(Interrupt);
214215
sigint();
215-
timer::sleep(10);
216+
timer::sleep(Duration::milliseconds(10));
216217
assert_eq!(s2.rx.try_recv(), Err(Empty));
217218
}
218219
}

src/libstd/io/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ macro_rules! iotest (
3939
use io::process::*;
4040
use rt::running_on_valgrind;
4141
use str;
42+
use time::Duration;
4243

4344
fn f() $b
4445

0 commit comments

Comments
 (0)