@@ -39,15 +39,11 @@ impl Instant {
39
39
InstantKind :: Virtual { nanoseconds } ,
40
40
InstantKind :: Virtual { nanoseconds : earlier } ,
41
41
) => {
42
- // Trade some nanosecond precision to prevent as much overflow as possible.
43
- let duration = match u64:: try_from (
44
- // Manually convert from nanosecond to millisecond.
45
- // If it exceeded u64::MAX millisecond, we will just use u64::MAX millisecond,
46
- // Duration can't take in more than u64::MAX millisecond.
47
- nanoseconds. saturating_sub ( earlier) . saturating_div ( 1_000_000 ) ,
48
- ) {
49
- Ok ( millisecond) => Duration :: from_millis ( millisecond) ,
50
- _ => Duration :: from_millis ( u64:: MAX ) ,
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 ) ,
51
47
} ;
52
48
Duration :: new ( duration. as_secs ( ) , duration. subsec_nanos ( ) )
53
49
}
@@ -104,7 +100,7 @@ impl Clock {
104
100
ClockKind :: Host { .. } => std:: thread:: sleep ( duration) ,
105
101
ClockKind :: Virtual { nanoseconds } => {
106
102
// Just pretend that we have slept for some time.
107
- let nanos: u128 = duration. as_nanos ( ) . try_into ( ) . unwrap ( ) ;
103
+ let nanos: u128 = duration. as_nanos ( ) ;
108
104
nanoseconds. update ( |x| x + nanos) ;
109
105
}
110
106
}
0 commit comments