Skip to content

Commit 39133ef

Browse files
committed
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.
1 parent d16a5cd commit 39133ef

File tree

4 files changed

+208
-288
lines changed

4 files changed

+208
-288
lines changed

src/libstd/io/net/tcp.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,31 @@ impl TcpStream {
9797
/// the specified duration.
9898
///
9999
/// This is the same as the `connect` method, except that if the timeout
100-
/// specified (in milliseconds) elapses before a connection is made an error
101-
/// will be returned. The error's kind will be `TimedOut`.
100+
/// specified elapses before a connection is made an error will be
101+
/// returned. The error's kind will be `TimedOut`.
102102
///
103103
/// Note that the `addr` argument may one day be split into a separate host
104104
/// and port, similar to the API seen in `connect`.
105105
///
106106
/// If a `timeout` with zero or negative duration is specified then
107107
/// the function returns `Err`, with the error kind set to `TimedOut`.
108+
/// If the timeout is larger than 2^63 milliseconds, the function also
109+
/// returns `Err` with the error kind set to `TimedOut`.
108110
#[experimental = "the timeout argument may eventually change types"]
109111
pub fn connect_timeout(addr: SocketAddr,
110112
timeout: Duration) -> IoResult<TcpStream> {
111113
if timeout <= Duration::milliseconds(0) {
112114
return Err(standard_error(TimedOut));
113115
}
116+
let timeout_ms = timeout.num_milliseconds().map(|x| { x as u64 });
117+
if timeout_ms.is_none() {
118+
return Err(standard_error(TimedOut));
119+
}
114120

115121
let SocketAddr { ip, port } = addr;
116122
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
117123
LocalIo::maybe_raise(|io| {
118-
io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
124+
io.tcp_connect(addr, timeout_ms).map(TcpStream::new)
119125
}).map_err(IoError::from_rtio_error)
120126
}
121127

src/libstd/io/net/unix.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,26 @@ impl UnixStream {
6161
/// Connect to a pipe named by `path`, timing out if the specified number of
6262
/// milliseconds.
6363
///
64-
/// This function is similar to `connect`, except that if `timeout_ms`
64+
/// This function is similar to `connect`, except that if `timeout`
6565
/// elapses the function will return an error of kind `TimedOut`.
6666
///
6767
/// If a `timeout` with zero or negative duration is specified then
6868
/// the function returns `Err`, with the error kind set to `TimedOut`.
69+
/// If the timeout is larger than 2^63 milliseconds, the function also
70+
/// returns `Err` with the error kind set to `TimedOut`.
6971
#[experimental = "the timeout argument is likely to change types"]
7072
pub fn connect_timeout<P: ToCStr>(path: &P,
7173
timeout: Duration) -> IoResult<UnixStream> {
7274
if timeout <= Duration::milliseconds(0) {
7375
return Err(standard_error(TimedOut));
7476
}
77+
let timeout_ms = timeout.num_milliseconds().map(|x| { x as u64 });
78+
if timeout_ms.is_none() {
79+
return Err(standard_error(TimedOut));
80+
}
7581

7682
LocalIo::maybe_raise(|io| {
77-
let s = io.unix_connect(&path.to_c_str(), Some(timeout.num_milliseconds() as u64));
83+
let s = io.unix_connect(&path.to_c_str(), timeout_ms);
7884
s.map(|p| UnixStream { obj: p })
7985
}).map_err(IoError::from_rtio_error)
8086
}

src/libstd/io/timer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl Callback for TimerCallback {
225225
}
226226

227227
fn in_ms_u64(d: Duration) -> u64 {
228-
let ms = d.num_milliseconds();
228+
let ms = d.num_milliseconds().unwrap_or(0);
229229
if ms < 0 { return 0 };
230230
return ms as u64;
231231
}

0 commit comments

Comments
 (0)