@@ -11,7 +11,7 @@ const INTERVALS_PER_SEC: u64 = NANOS_PER_SEC / 100;
11
11
12
12
#[ derive( Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Debug , Hash ) ]
13
13
pub struct Instant {
14
- t : u64 ,
14
+ t : Duration ,
15
15
}
16
16
17
17
#[ derive( Copy , Clone ) ]
@@ -49,34 +49,25 @@ impl Instant {
49
49
pub fn sub_instant ( & self , other : & Instant ) -> Duration {
50
50
// On windows there's a threshold below which we consider two timestamps
51
51
// 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 {
56
56
return Duration :: new ( 0 , 0 )
57
57
}
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" )
61
60
}
62
61
63
62
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 ) ?;
68
63
Some ( Instant {
69
- t : sum ,
64
+ t : self . t . checked_add ( * other ) ?
70
65
} )
71
66
}
72
67
73
68
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) ?;
78
69
Some ( Instant {
79
- t : difference ,
70
+ t : self . t . checked_sub ( * other ) ?
80
71
} )
81
72
}
82
73
}
@@ -183,6 +174,7 @@ mod perf_counter {
183
174
use sys_common:: mul_div_u64;
184
175
use sys:: c;
185
176
use sys:: cvt;
177
+ use time:: Duration ;
186
178
187
179
pub struct PerformanceCounterInstant {
188
180
ts : c:: LARGE_INTEGER
@@ -198,17 +190,17 @@ mod perf_counter {
198
190
// using QueryPerformanceCounter is 1 "tick" -- defined as 1/frequency().
199
191
// Reference: https://docs.microsoft.com/en-us/windows/desktop/SysInfo
200
192
// /acquiring-high-resolution-time-stamps
201
- pub fn epsilon_nanos ( ) -> u32 {
193
+ pub fn epsilon ( ) -> Duration {
202
194
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)
205
196
}
206
197
}
207
198
impl From < PerformanceCounterInstant > for super :: Instant {
208
199
fn from ( other : PerformanceCounterInstant ) -> Self {
209
200
let freq = frequency ( ) as u64 ;
201
+ let instant_nsec = mul_div_u64 ( other. ts as u64 , NANOS_PER_SEC , freq) ;
210
202
Self {
211
- t : mul_div_u64 ( other . ts as u64 , NANOS_PER_SEC , freq )
203
+ t : Duration :: from_nanos ( instant_nsec )
212
204
}
213
205
}
214
206
}
0 commit comments