Skip to content

Commit f720a26

Browse files
JBYoshiJonathanWoollett-Light
authored andcommitted
Add try_from/unwrap for u128 time values
Some of Rust's time measurements use u128 for outputs but u64 for inputs. I don't expect we'll overflow a u64 - 2^64 nanoseconds is over 500 years - so I've added try_from()/unwrap() calls to those conversions. Signed-off-by: Jonathan Browne <[email protected]>
1 parent e261223 commit f720a26

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/vmm/src/rate_limiter/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ impl RateLimiter {
417417
// order to enforce the bandwidth limit we need to prevent
418418
// further calls to the rate limiter for
419419
// `ratio * refill_time` milliseconds.
420-
#[allow(clippy::cast_sign_loss)] // ratio is always positive
420+
// The conversion should be safe because the ratio is positive.
421+
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
421422
self.activate_timer(TimerState::Oneshot(Duration::from_millis(
422423
(ratio * refill_time as f64) as u64,
423424
)));

src/vmm/src/rate_limiter/persist.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ impl Persist<'_> for TokenBucket {
3131
one_time_burst: self.one_time_burst,
3232
refill_time: self.refill_time,
3333
budget: self.budget,
34-
elapsed_ns: self.last_update.elapsed().as_nanos() as u64,
34+
// This should be safe for a duration of about 584 years.
35+
elapsed_ns: u64::try_from(self.last_update.elapsed().as_nanos()).unwrap(),
3536
}
3637
}
3738

0 commit comments

Comments
 (0)