Skip to content

Commit 0f566ec

Browse files
committed
Move Instant backing type to Duration
Per review comments, this commit switches out the backing type for Instant on windows to a Duration. Tests all pass, and the code's a lot simpler (plus it should be portable now, with the exception of the QueryPerformanceWhatever functions).
1 parent 55dea0e commit 0f566ec

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/libstd/sys/windows/time.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100;
1111

1212
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
1313
pub struct Instant {
14-
t: u64,
14+
t: Duration,
1515
}
1616

1717
#[derive(Copy, Clone)]
@@ -49,34 +49,25 @@ impl Instant {
4949
pub fn sub_instant(&self, other: &Instant) -> Duration {
5050
// On windows there's a threshold below which we consider two timestamps
5151
// equivalent due to measurement error. For more details + doc link,
52-
// check the docs on epsilon_nanos.
53-
let epsilon_ns =
54-
perf_counter::PerformanceCounterInstant::epsilon_nanos() as u64;
55-
if other.t > self.t && other.t - self.t <= epsilon_ns {
52+
// check the docs on epsilon.
53+
let epsilon =
54+
perf_counter::PerformanceCounterInstant::epsilon();
55+
if other.t > self.t && other.t - self.t <= epsilon {
5656
return Duration::new(0, 0)
5757
}
58-
let diff = (self.t).checked_sub(other.t)
59-
.expect("specified instant was later than self");
60-
Duration::new(diff / NANOS_PER_SEC, (diff % NANOS_PER_SEC) as u32)
58+
self.t.checked_sub(other.t)
59+
.expect("specified instant was later than self")
6160
}
6261

6362
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
64-
let sum = other.as_secs()
65-
.checked_mul(NANOS_PER_SEC)?
66-
.checked_add(other.subsec_nanos() as u64)?
67-
.checked_add(self.t as u64)?;
6863
Some(Instant {
69-
t: sum,
64+
t: self.t.checked_add(*other)?
7065
})
7166
}
7267

7368
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
74-
let other_ns = other.as_secs()
75-
.checked_mul(NANOS_PER_SEC)?
76-
.checked_add(other.subsec_nanos() as u64)?;
77-
let difference = self.t.checked_sub(other_ns)?;
7869
Some(Instant {
79-
t: difference,
70+
t: self.t.checked_sub(*other)?
8071
})
8172
}
8273
}
@@ -183,6 +174,7 @@ mod perf_counter {
183174
use sys_common::mul_div_u64;
184175
use sys::c;
185176
use sys::cvt;
177+
use time::Duration;
186178

187179
pub struct PerformanceCounterInstant {
188180
ts: c::LARGE_INTEGER
@@ -198,17 +190,17 @@ mod perf_counter {
198190
// using QueryPerformanceCounter is 1 "tick" -- defined as 1/frequency().
199191
// Reference: https://docs.microsoft.com/en-us/windows/desktop/SysInfo
200192
// /acquiring-high-resolution-time-stamps
201-
pub fn epsilon_nanos() -> u32 {
193+
pub fn epsilon() -> Duration {
202194
let epsilon = NANOS_PER_SEC / (frequency() as u64);
203-
// As noted elsewhere, subsecond nanos always fit in a u32
204-
epsilon as u32
195+
Duration::from_nanos(epsilon)
205196
}
206197
}
207198
impl From<PerformanceCounterInstant> for super::Instant {
208199
fn from(other: PerformanceCounterInstant) -> Self {
209200
let freq = frequency() as u64;
201+
let instant_nsec = mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq);
210202
Self {
211-
t: mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq)
203+
t: Duration::from_nanos(instant_nsec)
212204
}
213205
}
214206
}

0 commit comments

Comments
 (0)