File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -39,13 +39,14 @@ impl Instant {
39
39
InstantKind :: Virtual { nanoseconds } ,
40
40
InstantKind :: Virtual { nanoseconds : earlier } ,
41
41
) => {
42
- // If it exceeded u64::MAX nanosecond, we will just keep u64::MAX nanosecond,
43
- // Duration can't take in more than u64::MAX.
44
- let duration = match u64:: try_from ( nanoseconds. saturating_sub ( earlier) ) {
45
- Ok ( nanosecond) => Duration :: from_nanos ( nanosecond) ,
46
- Err ( _err) => Duration :: from_nanos ( u64:: MAX ) ,
47
- } ;
48
- Duration :: new ( duration. as_secs ( ) , duration. subsec_nanos ( ) )
42
+ // It is possible for second to overflow because u64::MAX < (u128::MAX / 1e9).
43
+ let seconds = u64:: try_from (
44
+ nanoseconds. saturating_sub ( earlier) . saturating_div ( 1_000_000_000 ) ,
45
+ )
46
+ . unwrap ( ) ;
47
+ // It is impossible for nanosecond to overflow because u32::MAX > 1e9.
48
+ let nanosecond = u32:: try_from ( nanoseconds. wrapping_rem ( 1_000_000_000 ) ) . unwrap ( ) ;
49
+ Duration :: new ( seconds, nanosecond)
49
50
}
50
51
_ => panic ! ( "all `Instant` must be of the same kind" ) ,
51
52
}
You can’t perform that action at this time.
0 commit comments