File tree 6 files changed +18
-25
lines changed
6 files changed +18
-25
lines changed Original file line number Diff line number Diff line change @@ -33,11 +33,9 @@ impl Instant {
33
33
Instant { t : 0 }
34
34
}
35
35
36
- pub fn sub_instant ( & self , other : & Instant ) -> Duration {
37
- let diff = self . t
38
- . checked_sub ( other. t )
39
- . expect ( "second instant is later than self" ) ;
40
- Duration :: new ( diff / NSEC_PER_SEC , ( diff % NSEC_PER_SEC ) as u32 )
36
+ pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
37
+ let diff = self . t . checked_sub ( other. t ) ?;
38
+ Some ( Duration :: new ( diff / NSEC_PER_SEC , ( diff % NSEC_PER_SEC ) as u32 ) )
41
39
}
42
40
43
41
pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
Original file line number Diff line number Diff line change @@ -137,10 +137,8 @@ impl Instant {
137
137
false
138
138
}
139
139
140
- pub fn sub_instant ( & self , other : & Instant ) -> Duration {
141
- self . t . sub_timespec ( & other. t ) . unwrap_or_else ( |_| {
142
- panic ! ( "specified instant was later than self" )
143
- } )
140
+ pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
141
+ self . t . sub_timespec ( & other. t ) . ok ( )
144
142
}
145
143
146
144
pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
Original file line number Diff line number Diff line change @@ -14,8 +14,8 @@ impl Instant {
14
14
Instant ( usercalls:: insecure_time ( ) )
15
15
}
16
16
17
- pub fn sub_instant ( & self , other : & Instant ) -> Duration {
18
- self . 0 - other. 0
17
+ pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
18
+ self . 0 . checked_sub ( other. 0 )
19
19
}
20
20
21
21
pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
Original file line number Diff line number Diff line change @@ -149,12 +149,11 @@ mod inner {
149
149
true
150
150
}
151
151
152
- pub fn sub_instant ( & self , other : & Instant ) -> Duration {
152
+ pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
153
+ let diff = self . t . checked_sub ( other. t ) ?;
153
154
let info = info ( ) ;
154
- let diff = self . t . checked_sub ( other. t )
155
- . expect ( "second instant is later than self" ) ;
156
155
let nanos = mul_div_u64 ( diff, info. numer as u64 , info. denom as u64 ) ;
157
- Duration :: new ( nanos / NSEC_PER_SEC , ( nanos % NSEC_PER_SEC ) as u32 )
156
+ Some ( Duration :: new ( nanos / NSEC_PER_SEC , ( nanos % NSEC_PER_SEC ) as u32 ) )
158
157
}
159
158
160
159
pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
@@ -285,10 +284,8 @@ mod inner {
285
284
false // last clause, used so `||` is always trailing above
286
285
}
287
286
288
- pub fn sub_instant ( & self , other : & Instant ) -> Duration {
289
- self . t . sub_timespec ( & other. t ) . unwrap_or_else ( |_| {
290
- panic ! ( "specified instant was later than self" )
291
- } )
287
+ pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
288
+ self . t . sub_timespec ( & other. t ) . ok ( )
292
289
}
293
290
294
291
pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
Original file line number Diff line number Diff line change @@ -22,8 +22,8 @@ impl Instant {
22
22
false
23
23
}
24
24
25
- pub fn sub_instant ( & self , other : & Instant ) -> Duration {
26
- self . 0 - other. 0
25
+ pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
26
+ self . 0 . checked_sub ( other. 0 )
27
27
}
28
28
29
29
pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
Original file line number Diff line number Diff line change @@ -49,17 +49,17 @@ impl Instant {
49
49
Instant { t : Duration :: from_secs ( 0 ) }
50
50
}
51
51
52
- pub fn sub_instant ( & self , other : & Instant ) -> Duration {
52
+ pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
53
53
// On windows there's a threshold below which we consider two timestamps
54
54
// equivalent due to measurement error. For more details + doc link,
55
55
// check the docs on epsilon.
56
56
let epsilon =
57
57
perf_counter:: PerformanceCounterInstant :: epsilon ( ) ;
58
58
if other. t > self . t && other. t - self . t <= epsilon {
59
- return Duration :: new ( 0 , 0 )
59
+ Some ( Duration :: new ( 0 , 0 ) )
60
+ } else {
61
+ self . t . checked_sub ( other. t )
60
62
}
61
- self . t . checked_sub ( other. t )
62
- . expect ( "specified instant was later than self" )
63
63
}
64
64
65
65
pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
You can’t perform that action at this time.
0 commit comments