]> git.lizzy.rs Git - rust.git/commitdiff
std: Make the TCP/UDP connect_timeout methods take Duration
authorBrian Anderson <banderson@mozilla.com>
Thu, 31 Jul 2014 22:58:39 +0000 (15:58 -0700)
committerBrian Anderson <banderson@mozilla.com>
Wed, 13 Aug 2014 18:31:48 +0000 (11:31 -0700)
[breaking-change]

src/libstd/io/net/tcp.rs
src/libstd/io/net/unix.rs
src/libstd/io/timer.rs

index 0b0c22ed88799f3efccb362e12793d42ed6a8d59..84065bc936ecd891b7857320afa764b390d28f95 100644 (file)
@@ -34,6 +34,7 @@
 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 @@ pub fn connect(host: &str, port: u16) -> IoResult<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 @@ fn accept(&mut self) -> IoResult<TcpStream> {
     }
 }
 
+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 {
index 5e7c421497772faa7f7244326a6a238c3ee10b4e..8847b4c9214ad732a5cab3c09f5420e6da471323 100644 (file)
@@ -33,6 +33,7 @@
 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 @@ pub fn connect<P: ToCStr>(path: &P) -> IoResult<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 @@ pub fn smalltest(server: proc(UnixStream):Send, client: proc(UnixStream):Send) {
 
     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() {
index 0d3f932f4292de03c07b82a0f2882a8ceb515ee4..3045871ce7ac47db6a8b33508035c9ecf3be9ada 100644 (file)
@@ -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 @@ pub fn new() -> IoResult<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 @@ pub fn sleep(&mut self, duration: Duration) {
     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 @@ pub fn oneshot(&mut self, duration: Duration) -> Receiver<()> {
     /// 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 @@ fn call(&mut self) {
     }
 }
 
+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() {