about summary refs log tree commit diff
path: root/src/libstd/io
diff options
context:
space:
mode:
authorRuud van Asseldonk <dev@veniogames.com>2014-08-19 14:32:20 +0200
committerRuud van Asseldonk <dev@veniogames.com>2014-08-20 13:55:02 +0200
commit39133efebf49823002977f0ad0ac12edf27b4d06 (patch)
tree9cab7ff0b07902691631a230c9395979936f52ef /src/libstd/io
parentd16a5cd7c4d37c947faf4661b22e994409197809 (diff)
downloadrust-39133efebf49823002977f0ad0ac12edf27b4d06.tar.gz
rust-39133efebf49823002977f0ad0ac12edf27b4d06.zip
libstd: Refactor Duration.
This changes the internal representation of `Duration` from

    days: i32,
    secs: i32,
    nanos: u32

to

    secs: i64,
    nanos: i32

This resolves #16466. Some methods now take `i64` instead of `i32` due
to the increased range. Some methods, like `num_milliseconds`, now
return an `Option<i64>` instead of `i64`, because the range of
`Duration` is now larger than e.g. 2^63 milliseconds.
Diffstat (limited to 'src/libstd/io')
-rw-r--r--src/libstd/io/net/tcp.rs12
-rw-r--r--src/libstd/io/net/unix.rs10
-rw-r--r--src/libstd/io/timer.rs2
3 files changed, 18 insertions, 6 deletions
diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs
index 7055b9d7a47..52d3a04432a 100644
--- a/src/libstd/io/net/tcp.rs
+++ b/src/libstd/io/net/tcp.rs
@@ -97,25 +97,31 @@ impl TcpStream {
     /// 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
-    /// will be returned. The error's kind will be `TimedOut`.
+    /// specified elapses before a connection is made an error will be
+    /// returned. The error's kind will be `TimedOut`.
     ///
     /// Note that the `addr` argument may one day be split into a separate host
     /// and port, similar to the API seen in `connect`.
     ///
     /// If a `timeout` with zero or negative duration is specified then
     /// the function returns `Err`, with the error kind set to `TimedOut`.
+    /// If the timeout is larger than 2^63 milliseconds, the function also
+    /// returns `Err` with the error kind set to `TimedOut`.
     #[experimental = "the timeout argument may eventually change types"]
     pub fn connect_timeout(addr: SocketAddr,
                            timeout: Duration) -> IoResult<TcpStream> {
         if timeout <= Duration::milliseconds(0) {
             return Err(standard_error(TimedOut));
         }
+        let timeout_ms = timeout.num_milliseconds().map(|x| { x as u64 });
+        if timeout_ms.is_none() {
+            return Err(standard_error(TimedOut));
+        }
 
         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.num_milliseconds() as u64)).map(TcpStream::new)
+            io.tcp_connect(addr, timeout_ms).map(TcpStream::new)
         }).map_err(IoError::from_rtio_error)
     }
 
diff --git a/src/libstd/io/net/unix.rs b/src/libstd/io/net/unix.rs
index eb251075418..179855003f9 100644
--- a/src/libstd/io/net/unix.rs
+++ b/src/libstd/io/net/unix.rs
@@ -61,20 +61,26 @@ impl UnixStream {
     /// Connect to a pipe named by `path`, timing out if the specified number of
     /// milliseconds.
     ///
-    /// This function is similar to `connect`, except that if `timeout_ms`
+    /// This function is similar to `connect`, except that if `timeout`
     /// elapses the function will return an error of kind `TimedOut`.
     ///
     /// If a `timeout` with zero or negative duration is specified then
     /// the function returns `Err`, with the error kind set to `TimedOut`.
+    /// If the timeout is larger than 2^63 milliseconds, the function also
+    /// returns `Err` with the error kind set to `TimedOut`.
     #[experimental = "the timeout argument is likely to change types"]
     pub fn connect_timeout<P: ToCStr>(path: &P,
                                       timeout: Duration) -> IoResult<UnixStream> {
         if timeout <= Duration::milliseconds(0) {
             return Err(standard_error(TimedOut));
         }
+        let timeout_ms = timeout.num_milliseconds().map(|x| { x as u64 });
+        if timeout_ms.is_none() {
+            return Err(standard_error(TimedOut));
+        }
 
         LocalIo::maybe_raise(|io| {
-            let s = io.unix_connect(&path.to_c_str(), Some(timeout.num_milliseconds() as u64));
+            let s = io.unix_connect(&path.to_c_str(), timeout_ms);
             s.map(|p| UnixStream { obj: p })
         }).map_err(IoError::from_rtio_error)
     }
diff --git a/src/libstd/io/timer.rs b/src/libstd/io/timer.rs
index 39c6c74e45e..205132aca1d 100644
--- a/src/libstd/io/timer.rs
+++ b/src/libstd/io/timer.rs
@@ -225,7 +225,7 @@ impl Callback for TimerCallback {
 }
 
 fn in_ms_u64(d: Duration) -> u64 {
-    let ms = d.num_milliseconds();
+    let ms = d.num_milliseconds().unwrap_or(0);
     if ms < 0 { return 0 };
     return ms as u64;
 }