Skip to content

Commit ac249c1

Browse files
committed
Move Duration to libcore.
1 parent 53492ec commit ac249c1

File tree

5 files changed

+91
-81
lines changed

5 files changed

+91
-81
lines changed

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ pub mod iter;
140140
pub mod option;
141141
pub mod raw;
142142
pub mod result;
143+
pub mod time;
143144

144145
pub mod slice;
145146
pub mod str;

src/libstd/time/duration.rs renamed to src/libcore/time.rs

+4-78
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Temporal quantification.
12+
13+
#![stable(feature = "time", since = "1.3.0")]
14+
1115
use ops::{Add, Sub, Mul, Div};
1216

1317
const NANOS_PER_SEC: u32 = 1_000_000_000;
@@ -154,81 +158,3 @@ impl Div<u32> for Duration {
154158
Duration { secs: secs, nanos: nanos }
155159
}
156160
}
157-
158-
#[cfg(test)]
159-
mod tests {
160-
use super::Duration;
161-
162-
#[test]
163-
fn creation() {
164-
assert!(Duration::from_secs(1) != Duration::from_secs(0));
165-
assert_eq!(Duration::from_secs(1) + Duration::from_secs(2),
166-
Duration::from_secs(3));
167-
assert_eq!(Duration::from_millis(10) + Duration::from_secs(4),
168-
Duration::new(4, 10 * 1_000_000));
169-
assert_eq!(Duration::from_millis(4000), Duration::new(4, 0));
170-
}
171-
172-
#[test]
173-
fn secs() {
174-
assert_eq!(Duration::new(0, 0).as_secs(), 0);
175-
assert_eq!(Duration::from_secs(1).as_secs(), 1);
176-
assert_eq!(Duration::from_millis(999).as_secs(), 0);
177-
assert_eq!(Duration::from_millis(1001).as_secs(), 1);
178-
}
179-
180-
#[test]
181-
fn nanos() {
182-
assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
183-
assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
184-
assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
185-
assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
186-
assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
187-
assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
188-
}
189-
190-
#[test]
191-
fn add() {
192-
assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
193-
Duration::new(0, 1));
194-
assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
195-
Duration::new(1, 1));
196-
}
197-
198-
#[test]
199-
fn sub() {
200-
assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
201-
Duration::new(0, 1));
202-
assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
203-
Duration::new(0, 1));
204-
assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
205-
Duration::new(0, 999_999_999));
206-
}
207-
208-
#[test] #[should_panic]
209-
fn sub_bad1() {
210-
Duration::new(0, 0) - Duration::new(0, 1);
211-
}
212-
213-
#[test] #[should_panic]
214-
fn sub_bad2() {
215-
Duration::new(0, 0) - Duration::new(1, 0);
216-
}
217-
218-
#[test]
219-
fn mul() {
220-
assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
221-
assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
222-
assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
223-
assert_eq!(Duration::new(0, 500_000_001) * 4000,
224-
Duration::new(2000, 4000));
225-
}
226-
227-
#[test]
228-
fn div() {
229-
assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
230-
assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
231-
assert_eq!(Duration::new(99, 999_999_000) / 100,
232-
Duration::new(0, 999_999_990));
233-
}
234-
}

src/libcoretest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@ mod ptr;
7171
mod result;
7272
mod slice;
7373
mod str;
74+
mod time;
7475
mod tuple;

src/libcoretest/time.rs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use core::time::Duration;
12+
13+
#[test]
14+
fn creation() {
15+
assert!(Duration::from_secs(1) != Duration::from_secs(0));
16+
assert_eq!(Duration::from_secs(1) + Duration::from_secs(2),
17+
Duration::from_secs(3));
18+
assert_eq!(Duration::from_millis(10) + Duration::from_secs(4),
19+
Duration::new(4, 10 * 1_000_000));
20+
assert_eq!(Duration::from_millis(4000), Duration::new(4, 0));
21+
}
22+
23+
#[test]
24+
fn secs() {
25+
assert_eq!(Duration::new(0, 0).as_secs(), 0);
26+
assert_eq!(Duration::from_secs(1).as_secs(), 1);
27+
assert_eq!(Duration::from_millis(999).as_secs(), 0);
28+
assert_eq!(Duration::from_millis(1001).as_secs(), 1);
29+
}
30+
31+
#[test]
32+
fn nanos() {
33+
assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
34+
assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
35+
assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
36+
assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
37+
assert_eq!(Duration::from_millis(999).subsec_nanos(), 999 * 1_000_000);
38+
assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1 * 1_000_000);
39+
}
40+
41+
#[test]
42+
fn add() {
43+
assert_eq!(Duration::new(0, 0) + Duration::new(0, 1),
44+
Duration::new(0, 1));
45+
assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001),
46+
Duration::new(1, 1));
47+
}
48+
49+
#[test]
50+
fn sub() {
51+
assert_eq!(Duration::new(0, 1) - Duration::new(0, 0),
52+
Duration::new(0, 1));
53+
assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000),
54+
Duration::new(0, 1));
55+
assert_eq!(Duration::new(1, 0) - Duration::new(0, 1),
56+
Duration::new(0, 999_999_999));
57+
}
58+
59+
#[test] #[should_panic]
60+
fn sub_bad1() {
61+
Duration::new(0, 0) - Duration::new(0, 1);
62+
}
63+
64+
#[test] #[should_panic]
65+
fn sub_bad2() {
66+
Duration::new(0, 0) - Duration::new(1, 0);
67+
}
68+
69+
#[test]
70+
fn mul() {
71+
assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
72+
assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
73+
assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
74+
assert_eq!(Duration::new(0, 500_000_001) * 4000,
75+
Duration::new(2000, 4000));
76+
}
77+
78+
#[test]
79+
fn div() {
80+
assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
81+
assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
82+
assert_eq!(Duration::new(99, 999_999_000) / 100,
83+
Duration::new(0, 999_999_990));
84+
}

src/libstd/time/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use ops::{Add, Sub};
1818
use sys::time;
1919

2020
#[stable(feature = "time", since = "1.3.0")]
21-
pub use self::duration::Duration;
22-
23-
mod duration;
21+
pub use core::time::Duration;
2422

2523
/// A measurement of a monotonically increasing clock.
2624
///

0 commit comments

Comments
 (0)