@@ -39,15 +39,11 @@ impl Instant {
3939 InstantKind :: Virtual { nanoseconds } ,
4040 InstantKind :: Virtual { nanoseconds : earlier } ,
4141 ) => {
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 ) ,
5147 } ;
5248 Duration :: new ( duration. as_secs ( ) , duration. subsec_nanos ( ) )
5349 }
@@ -104,7 +100,7 @@ impl Clock {
104100 ClockKind :: Host { .. } => std:: thread:: sleep ( duration) ,
105101 ClockKind :: Virtual { nanoseconds } => {
106102 // 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 ( ) ;
108104 nanoseconds. update ( |x| x + nanos) ;
109105 }
110106 }
0 commit comments