diff options
| author | Brian Anderson <banderson@mozilla.com> | 2014-07-31 15:58:39 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2014-08-13 11:31:48 -0700 |
| commit | 9fdcddb3178d4705db4aee5ee12c05796203658c (patch) | |
| tree | bf89747f277fc55b3f4c85f92a21008a248c0fbe /src/libstd | |
| parent | 63cd4acf535f8df177459559fe2d7e6ab390c6d8 (diff) | |
| download | rust-9fdcddb3178d4705db4aee5ee12c05796203658c.tar.gz rust-9fdcddb3178d4705db4aee5ee12c05796203658c.zip | |
std: Make the TCP/UDP connect_timeout methods take Duration
[breaking-change]
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/net/tcp.rs | 11 | ||||
| -rw-r--r-- | src/libstd/io/net/unix.rs | 9 | ||||
| -rw-r--r-- | src/libstd/io/timer.rs | 20 |
3 files changed, 24 insertions, 16 deletions
diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs index 0b0c22ed887..84065bc936e 100644 --- a/src/libstd/io/net/tcp.rs +++ b/src/libstd/io/net/tcp.rs @@ -34,6 +34,7 @@ use boxed::Box; use rt::rtio::{IoFactory, LocalIo, RtioSocket, RtioTcpListener}; use rt::rtio::{RtioTcpAcceptor, RtioTcpStream}; use rt::rtio; +use time::Duration; /// A structure which represents a TCP stream between a local socket and a /// remote socket. @@ -102,11 +103,11 @@ impl TcpStream { /// and port, similar to the API seen in `connect`. #[experimental = "the timeout argument may eventually change types"] pub fn connect_timeout(addr: SocketAddr, - timeout_ms: u64) -> IoResult<TcpStream> { + timeout: Duration) -> IoResult<TcpStream> { 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(timeout_ms)).map(TcpStream::new) + io.tcp_connect(addr, Some(in_ms_u64(timeout))).map(TcpStream::new) }).map_err(IoError::from_rtio_error) } @@ -443,6 +444,12 @@ 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 5e7c4214977..8847b4c9214 100644 --- a/src/libstd/io/net/unix.rs +++ b/src/libstd/io/net/unix.rs @@ -33,6 +33,7 @@ use kinds::Send; use boxed::Box; use rt::rtio::{IoFactory, LocalIo, RtioUnixListener}; use rt::rtio::{RtioUnixAcceptor, RtioPipe}; +use time::Duration; /// A stream which communicates over a named pipe. pub struct UnixStream { @@ -68,9 +69,9 @@ impl UnixStream { /// elapses the function will return an error of kind `TimedOut`. #[experimental = "the timeout argument is likely to change types"] pub fn connect_timeout<P: ToCStr>(path: &P, - timeout_ms: u64) -> IoResult<UnixStream> { + timeout: Duration) -> IoResult<UnixStream> { LocalIo::maybe_raise(|io| { - let s = io.unix_connect(&path.to_c_str(), Some(timeout_ms)); + let s = io.unix_connect(&path.to_c_str(), Some(in_ms_u64(timeout))); s.map(|p| UnixStream { obj: p }) }).map_err(IoError::from_rtio_error) } @@ -499,13 +500,13 @@ mod tests { iotest!(fn connect_timeout_error() { let addr = next_test_unix(); - assert!(UnixStream::connect_timeout(&addr, 100).is_err()); + assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_err()); }) iotest!(fn connect_timeout_success() { let addr = next_test_unix(); let _a = UnixListener::bind(&addr).unwrap().listen().unwrap(); - assert!(UnixStream::connect_timeout(&addr, 100).is_ok()); + assert!(UnixStream::connect_timeout(&addr, Duration::milliseconds(100)).is_ok()); }) iotest!(fn close_readwrite_smoke() { diff --git a/src/libstd/io/timer.rs b/src/libstd/io/timer.rs index 0d3f932f429..3045871ce7a 100644 --- a/src/libstd/io/timer.rs +++ b/src/libstd/io/timer.rs @@ -72,12 +72,6 @@ pub struct Timer { struct TimerCallback { tx: Sender<()> } -fn in_ms(d: Duration) -> u64 { - let ms = d.num_milliseconds(); - if ms < 0 { return 0 }; - return ms as u64; -} - /// Sleep the current task for the specified duration. /// /// When provided a zero or negative `duration`, the function will @@ -108,7 +102,7 @@ impl Timer { /// return immediately. pub fn sleep(&mut self, duration: Duration) { // Short-circuit the timer backend for 0 duration - let ms = in_ms(duration); + let ms = in_ms_u64(duration); if ms == 0 { return } self.obj.sleep(ms); } @@ -153,8 +147,8 @@ impl Timer { pub fn oneshot(&mut self, duration: Duration) -> Receiver<()> { let (tx, rx) = channel(); // Short-circuit the timer backend for 0 duration - if in_ms(duration) != 0 { - self.obj.oneshot(in_ms(duration), box TimerCallback { tx: tx }); + if in_ms_u64(duration) != 0 { + self.obj.oneshot(in_ms_u64(duration), box TimerCallback { tx: tx }); } else { tx.send(()); } @@ -207,7 +201,7 @@ impl Timer { /// When provided a zero or negative `duration`, the messages will /// be sent without delay. pub fn periodic(&mut self, duration: Duration) -> Receiver<()> { - let ms = in_ms(duration); + let ms = in_ms_u64(duration); // FIXME: The backend implementations don't ever send a message // if given a 0 ms duration. Temporarily using 1ms. It's // not clear what use a 0ms period is anyway... @@ -224,6 +218,12 @@ impl Callback for TimerCallback { } } +fn in_ms_u64(d: Duration) -> u64 { + let ms = d.num_milliseconds(); + if ms < 0 { return 0 }; + return ms as u64; +} + #[cfg(test)] mod test { iotest!(fn test_io_timer_sleep_simple() { |
