Skip to content

Commit 999bdec

Browse files
committed
Stabilize the Duration API
This commit stabilizes the `std::time` module and the `Duration` type. `Duration::span` remains unstable, and the `Display` implementation for `Duration` has been removed as it is still being reworked and all trait implementations for stable types are de facto stable. This is a [breaking-change] to those using `Duration`'s `Display` implementation.
1 parent af32c01 commit 999bdec

19 files changed

+51
-80
lines changed

src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#![feature(collections)]
3434
#![feature(const_fn)]
3535
#![feature(core)]
36-
#![feature(duration)]
3736
#![feature(duration_span)]
3837
#![feature(dynamic_lib)]
3938
#![feature(enumset)]

src/librustc/metadata/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ fn get_metadata_section(target: &Target, filename: &Path)
722722
let dur = Duration::span(|| {
723723
ret = Some(get_metadata_section_imp(target, filename));
724724
});
725-
info!("reading {:?} => {}", filename.file_name().unwrap(), dur);
725+
info!("reading {:?} => {:?}", filename.file_name().unwrap(), dur);
726726
return ret.unwrap();;
727727
}
728728

src/librustc/util/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ pub fn time<T, U, F>(do_it: bool, what: &str, u: U, f: F) -> T where
5757
// Hack up our own formatting for the duration to make it easier for scripts
5858
// to parse (always use the same number of decimal places and the same unit).
5959
const NANOS_PER_SEC: f64 = 1_000_000_000.0;
60-
let secs = dur.secs() as f64;
61-
let secs = secs + dur.extra_nanos() as f64 / NANOS_PER_SEC;
60+
let secs = dur.as_secs() as f64;
61+
let secs = secs + dur.subsec_nanos() as f64 / NANOS_PER_SEC;
6262

6363
let mem_string = match get_resident() {
6464
Some(n) => {

src/libstd/sys/unix/condvar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ impl Condvar {
6161
let r = ffi::gettimeofday(&mut sys_now, ptr::null_mut());
6262
debug_assert_eq!(r, 0);
6363

64-
let nsec = dur.extra_nanos() as libc::c_long +
64+
let nsec = dur.subsec_nanos() as libc::c_long +
6565
(sys_now.tv_usec * 1000) as libc::c_long;
6666
let extra = (nsec / 1_000_000_000) as libc::time_t;
6767
let nsec = nsec % 1_000_000_000;
68-
let seconds = dur.secs() as libc::time_t;
68+
let seconds = dur.as_secs() as libc::time_t;
6969

7070
let timeout = sys_now.tv_sec.checked_add(extra).and_then(|s| {
7171
s.checked_add(seconds)

src/libstd/sys/unix/net.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ impl Socket {
7979
pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
8080
let timeout = match dur {
8181
Some(dur) => {
82-
if dur.secs() == 0 && dur.extra_nanos() == 0 {
82+
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
8383
return Err(io::Error::new(io::ErrorKind::InvalidInput,
8484
"cannot set a 0 duration timeout"));
8585
}
8686

87-
let secs = if dur.secs() > libc::time_t::max_value() as u64 {
87+
let secs = if dur.as_secs() > libc::time_t::max_value() as u64 {
8888
libc::time_t::max_value()
8989
} else {
90-
dur.secs() as libc::time_t
90+
dur.as_secs() as libc::time_t
9191
};
9292
let mut timeout = libc::timeval {
9393
tv_sec: secs,
94-
tv_usec: (dur.extra_nanos() / 1000) as libc::suseconds_t,
94+
tv_usec: (dur.subsec_nanos() / 1000) as libc::suseconds_t,
9595
};
9696
if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
9797
timeout.tv_usec = 1;

src/libstd/sys/unix/thread.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ impl Thread {
131131

132132
pub fn sleep(dur: Duration) {
133133
let mut ts = libc::timespec {
134-
tv_sec: dur.secs() as libc::time_t,
135-
tv_nsec: dur.extra_nanos() as libc::c_long,
134+
tv_sec: dur.as_secs() as libc::time_t,
135+
tv_nsec: dur.subsec_nanos() as libc::c_long,
136136
};
137137

138138
// If we're awoken with a signal then the return value will be -1 and

src/libstd/sys/windows/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ fn dur2timeout(dur: Duration) -> libc::DWORD {
162162
// * Nanosecond precision is rounded up
163163
// * Greater than u32::MAX milliseconds (50 days) is rounded up to INFINITE
164164
// (never time out).
165-
dur.secs().checked_mul(1000).and_then(|ms| {
166-
ms.checked_add((dur.extra_nanos() as u64) / 1_000_000)
165+
dur.as_secs().checked_mul(1000).and_then(|ms| {
166+
ms.checked_add((dur.subsec_nanos() as u64) / 1_000_000)
167167
}).and_then(|ms| {
168-
ms.checked_add(if dur.extra_nanos() % 1_000_000 > 0 {1} else {0})
168+
ms.checked_add(if dur.subsec_nanos() % 1_000_000 > 0 {1} else {0})
169169
}).map(|ms| {
170170
if ms > <libc::DWORD>::max_value() as u64 {
171171
libc::INFINITE

src/libstd/time/duration.rs

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Temporal quantification
12-
13-
#![unstable(feature = "duration", reason = "recently added API per RFC 1040")]
14-
1511
#[cfg(stage0)]
1612
use prelude::v1::*;
1713

18-
use fmt;
1914
use ops::{Add, Sub, Mul, Div};
2015
use sys::time::SteadyTime;
2116

@@ -43,11 +38,12 @@ const MILLIS_PER_SEC: u64 = 1_000;
4338
/// let five_seconds = Duration::new(5, 0);
4439
/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
4540
///
46-
/// assert_eq!(five_seconds_and_five_nanos.secs(), 5);
47-
/// assert_eq!(five_seconds_and_five_nanos.extra_nanos(), 5);
41+
/// assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
42+
/// assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);
4843
///
4944
/// let ten_millis = Duration::from_millis(10);
5045
/// ```
46+
#[stable(feature = "duration", since = "1.3.0")]
5147
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
5248
pub struct Duration {
5349
secs: u64,
@@ -60,6 +56,7 @@ impl Duration {
6056
///
6157
/// If the nanoseconds is greater than 1 billion (the number of nanoseconds
6258
/// in a second), then it will carry over into the seconds provided.
59+
#[stable(feature = "duration", since = "1.3.0")]
6360
pub fn new(secs: u64, nanos: u32) -> Duration {
6461
let secs = secs + (nanos / NANOS_PER_SEC) as u64;
6562
let nanos = nanos % NANOS_PER_SEC;
@@ -79,11 +76,13 @@ impl Duration {
7976
}
8077

8178
/// Creates a new `Duration` from the specified number of seconds.
79+
#[stable(feature = "duration", since = "1.3.0")]
8280
pub fn from_secs(secs: u64) -> Duration {
8381
Duration { secs: secs, nanos: 0 }
8482
}
8583

8684
/// Creates a new `Duration` from the specified number of milliseconds.
85+
#[stable(feature = "duration", since = "1.3.0")]
8786
pub fn from_millis(millis: u64) -> Duration {
8887
let secs = millis / MILLIS_PER_SEC;
8988
let nanos = ((millis % MILLIS_PER_SEC) as u32) * NANOS_PER_MILLI;
@@ -94,14 +93,16 @@ impl Duration {
9493
///
9594
/// The extra precision represented by this duration is ignored (e.g. extra
9695
/// nanoseconds are not represented in the returned value).
97-
pub fn secs(&self) -> u64 { self.secs }
96+
#[stable(feature = "duration", since = "1.3.0")]
97+
pub fn as_secs(&self) -> u64 { self.secs }
9898

9999
/// Returns the nanosecond precision represented by this duration.
100100
///
101101
/// This method does **not** return the length of the duration when
102102
/// represented by nanoseconds. The returned number always represents a
103103
/// fractional portion of a second (e.g. it is less than one billion).
104-
pub fn extra_nanos(&self) -> u32 { self.nanos }
104+
#[stable(feature = "duration", since = "1.3.0")]
105+
pub fn subsec_nanos(&self) -> u32 { self.nanos }
105106
}
106107

107108
impl Add for Duration {
@@ -167,20 +168,6 @@ impl Div<u32> for Duration {
167168
}
168169
}
169170

170-
impl fmt::Display for Duration {
171-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
172-
match (self.secs, self.nanos) {
173-
(s, 0) => write!(f, "{}s", s),
174-
(0, n) if n % NANOS_PER_MILLI == 0 => write!(f, "{}ms",
175-
n / NANOS_PER_MILLI),
176-
(0, n) if n % 1_000 == 0 => write!(f, "{}µs", n / 1_000),
177-
(0, n) => write!(f, "{}ns", n),
178-
(s, n) => write!(f, "{}.{}s", s,
179-
format!("{:09}", n).trim_right_matches('0'))
180-
}
181-
}
182-
}
183-
184171
#[cfg(test)]
185172
mod tests {
186173
use prelude::v1::*;
@@ -198,20 +185,20 @@ mod tests {
198185

199186
#[test]
200187
fn secs() {
201-
assert_eq!(Duration::new(0, 0).secs(), 0);
202-
assert_eq!(Duration::from_secs(1).secs(), 1);
203-
assert_eq!(Duration::from_millis(999).secs(), 0);
204-
assert_eq!(Duration::from_millis(1001).secs(), 1);
188+
assert_eq!(Duration::new(0, 0).as_secs(), 0);
189+
assert_eq!(Duration::from_secs(1).as_secs(), 1);
190+
assert_eq!(Duration::from_millis(999).as_secs(), 0);
191+
assert_eq!(Duration::from_millis(1001).as_secs(), 1);
205192
}
206193

207194
#[test]
208195
fn nanos() {
209-
assert_eq!(Duration::new(0, 0).extra_nanos(), 0);
210-
assert_eq!(Duration::new(0, 5).extra_nanos(), 5);
211-
assert_eq!(Duration::new(0, 1_000_000_001).extra_nanos(), 1);
212-
assert_eq!(Duration::from_secs(1).extra_nanos(), 0);
213-
assert_eq!(Duration::from_millis(999).extra_nanos(), 999 * 1_000_000);
214-
assert_eq!(Duration::from_millis(1001).extra_nanos(), 1 * 1_000_000);
196+
assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
197+
assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
198+
assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
199+
assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
200+
assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
201+
assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
215202
}
216203

217204
#[test]
@@ -258,18 +245,4 @@ mod tests {
258245
assert_eq!(Duration::new(99, 999_999_000) / 100,
259246
Duration::new(0, 999_999_990));
260247
}
261-
262-
#[test]
263-
fn display() {
264-
assert_eq!(Duration::new(0, 2).to_string(), "2ns");
265-
assert_eq!(Duration::new(0, 2_000_000).to_string(), "2ms");
266-
assert_eq!(Duration::new(2, 0).to_string(), "2s");
267-
assert_eq!(Duration::new(2, 2).to_string(), "2.000000002s");
268-
assert_eq!(Duration::new(2, 2_000_000).to_string(),
269-
"2.002s");
270-
assert_eq!(Duration::new(0, 2_000_002).to_string(),
271-
"2000002ns");
272-
assert_eq!(Duration::new(2, 2_000_002).to_string(),
273-
"2.002000002s");
274-
}
275248
}

src/libstd/time/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Temporal quantification.
1212
13-
#![unstable(feature = "time")]
13+
#![stable(feature = "time", since = "1.3.0")]
1414

1515
pub use self::duration::Duration;
1616

src/libtest/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
#![feature(asm)]
3838
#![feature(box_syntax)]
39-
#![feature(duration)]
4039
#![feature(duration_span)]
4140
#![feature(fnbox)]
4241
#![feature(iter_cmp)]
@@ -1105,7 +1104,7 @@ impl Bencher {
11051104
}
11061105

11071106
pub fn ns_elapsed(&mut self) -> u64 {
1108-
self.dur.secs() * 1_000_000_000 + (self.dur.extra_nanos() as u64)
1107+
self.dur.as_secs() * 1_000_000_000 + (self.dur.subsec_nanos() as u64)
11091108
}
11101109

11111110
pub fn ns_per_iter(&mut self) -> u64 {

src/test/bench/core-map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::__rand::{Rng, thread_rng};
1616
use std::time::Duration;
1717

1818
fn timed<F>(label: &str, f: F) where F: FnMut() {
19-
println!(" {}: {}", label, Duration::span(f));
19+
println!(" {}: {:?}", label, Duration::span(f));
2020
}
2121

2222
trait MutableMap {

src/test/bench/core-set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn write_header(header: &str) {
153153
}
154154

155155
fn write_row(label: &str, value: Duration) {
156-
println!("{:30} {} s\n", label, value);
156+
println!("{:30} {:?} s\n", label, value);
157157
}
158158

159159
fn write_results(label: &str, results: &Results) {

src/test/bench/core-std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn maybe_run_test<F>(argv: &[String], name: String, test: F) where F: FnOnce() {
5151

5252
let dur = Duration::span(test);
5353

54-
println!("{}:\t\t{}", name, dur);
54+
println!("{}:\t\t{:?}", name, dur);
5555
}
5656

5757
fn shift_push() {

src/test/bench/msgsend-pipes-shared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ fn run(args: &[String]) {
8888
});
8989
let result = result.unwrap();
9090
print!("Count is {}\n", result);
91-
print!("Test took {}\n", dur);
92-
let thruput = ((size / workers * workers) as f64) / (dur.secs() as f64);
91+
print!("Test took {:?}\n", dur);
92+
let thruput = ((size / workers * workers) as f64) / (dur.as_secs() as f64);
9393
print!("Throughput={} per sec\n", thruput);
9494
assert_eq!(result, num_bytes * size);
9595
}

src/test/bench/msgsend-pipes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ fn run(args: &[String]) {
9595
});
9696
let result = result.unwrap();
9797
print!("Count is {}\n", result);
98-
print!("Test took {}\n", dur);
99-
let thruput = ((size / workers * workers) as f64) / (dur.secs() as f64);
98+
print!("Test took {:?}\n", dur);
99+
let thruput = ((size / workers * workers) as f64) / (dur.as_secs() as f64);
100100
print!("Throughput={} per sec\n", thruput);
101101
assert_eq!(result, num_bytes * size);
102102
}

src/test/bench/msgsend-ring-mutex-arcs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ fn main() {
107107

108108
// all done, report stats.
109109
let num_msgs = num_tasks * msg_per_task;
110-
let rate = (num_msgs as f64) / (dur.secs() as f64);
110+
let rate = (num_msgs as f64) / (dur.as_secs() as f64);
111111

112-
println!("Sent {} messages in {}", num_msgs, dur);
112+
println!("Sent {} messages in {:?}", num_msgs, dur);
113113
println!(" {} messages / second", rate);
114114
println!(" {} μs / message", 1000000. / rate);
115115
}

src/test/bench/shootout-pfib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn main() {
114114
let dur = Duration::span(|| fibn = Some(fib(n)));
115115
let fibn = fibn.unwrap();
116116

117-
println!("{}\t{}\t{}", n, fibn, dur);
117+
println!("{}\t{}\t{:?}", n, fibn, dur);
118118
}
119119
}
120120
}

src/test/bench/std-smallintmap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ fn main() {
5454

5555
let maxf = max as f64;
5656

57-
println!("insert(): {} seconds\n", checkf);
58-
println!(" : {} op/s\n", maxf / checkf.secs() as f64);
59-
println!("get() : {} seconds\n", appendf);
60-
println!(" : {} op/s\n", maxf / appendf.secs() as f64);
57+
println!("insert(): {:?} seconds\n", checkf);
58+
println!(" : {} op/s\n", maxf / checkf.as_secs() as f64);
59+
println!("get() : {:?} seconds\n", appendf);
60+
println!(" : {} op/s\n", maxf / appendf.as_secs() as f64);
6161
}

src/test/bench/task-perf-alloc-unwind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn run(repeat: isize, depth: isize) {
3636
recurse_or_panic(depth, None)
3737
}).join();
3838
});
39-
println!("iter: {}", dur);
39+
println!("iter: {:?}", dur);
4040
}
4141
}
4242

0 commit comments

Comments
 (0)