]> git.lizzy.rs Git - rust.git/commitdiff
Move Instant backing type to Duration
authorAlex Berghage <bearcage@ludumipsum.com>
Mon, 21 Jan 2019 01:05:09 +0000 (18:05 -0700)
committerAlex Berghage <aberghage@gmail.com>
Wed, 23 Jan 2019 02:18:28 +0000 (19:18 -0700)
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).

src/libstd/sys/windows/time.rs

index b0cdd0a7f3b2c6424e40f9d3d89adf0c1ad32490..113affb737edd7cd50dbcb71881838c767dfb499 100644 (file)
@@ -11,7 +11,7 @@
 
 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
 pub struct Instant {
-    t: u64,
+    t: Duration,
 }
 
 #[derive(Copy, Clone)]
@@ -49,34 +49,25 @@ pub const fn zero() -> Instant {
     pub fn sub_instant(&self, other: &Instant) -> Duration {
         // On windows there's a threshold below which we consider two timestamps
         // equivalent due to measurement error. For more details + doc link,
-        // check the docs on epsilon_nanos.
-        let epsilon_ns =
-            perf_counter::PerformanceCounterInstant::epsilon_nanos() as u64;
-        if other.t > self.t && other.t - self.t <= epsilon_ns {
+        // check the docs on epsilon.
+        let epsilon =
+            perf_counter::PerformanceCounterInstant::epsilon();
+        if other.t > self.t && other.t - self.t <= epsilon {
             return Duration::new(0, 0)
         }
-        let diff = (self.t).checked_sub(other.t)
-                           .expect("specified instant was later than self");
-        Duration::new(diff / NANOS_PER_SEC, (diff % NANOS_PER_SEC) as u32)
+        self.t.checked_sub(other.t)
+              .expect("specified instant was later than self")
     }
 
     pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
-        let sum = other.as_secs()
-            .checked_mul(NANOS_PER_SEC)?
-            .checked_add(other.subsec_nanos() as u64)?
-            .checked_add(self.t as u64)?;
         Some(Instant {
-            t: sum,
+            t: self.t.checked_add(*other)?
         })
     }
 
     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
-        let other_ns = other.as_secs()
-            .checked_mul(NANOS_PER_SEC)?
-            .checked_add(other.subsec_nanos() as u64)?;
-        let difference = self.t.checked_sub(other_ns)?;
         Some(Instant {
-            t: difference,
+            t: self.t.checked_sub(*other)?
         })
     }
 }
@@ -183,6 +174,7 @@ mod perf_counter {
     use sys_common::mul_div_u64;
     use sys::c;
     use sys::cvt;
+    use time::Duration;
 
     pub struct PerformanceCounterInstant {
         ts: c::LARGE_INTEGER
@@ -198,17 +190,17 @@ pub fn now() -> Self {
         // using QueryPerformanceCounter is 1 "tick" -- defined as 1/frequency().
         // Reference: https://docs.microsoft.com/en-us/windows/desktop/SysInfo
         //                   /acquiring-high-resolution-time-stamps
-        pub fn epsilon_nanos() -> u32 {
+        pub fn epsilon() -> Duration {
             let epsilon = NANOS_PER_SEC / (frequency() as u64);
-            // As noted elsewhere, subsecond nanos always fit in a u32
-            epsilon as u32
+            Duration::from_nanos(epsilon)
         }
     }
     impl From<PerformanceCounterInstant> for super::Instant {
         fn from(other: PerformanceCounterInstant) -> Self {
             let freq = frequency() as u64;
+            let instant_nsec = mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq);
             Self {
-                t: mul_div_u64(other.ts as u64, NANOS_PER_SEC, freq)
+                t: Duration::from_nanos(instant_nsec)
             }
         }
     }