Clamp ticks to 1 and round to nearest.

This commit is contained in:
Caleb Jamison 2023-02-09 20:57:27 -05:00
parent a4371e9544
commit bd7b3bd455

View File

@ -82,8 +82,17 @@ impl Duration {
}
/// Creates a duration corresponding to the specified Hz.
/// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1
/// tick. Doing so will not deadlock, but will certainly not produce the desired output.
pub const fn from_hz(hz: u64) -> Duration {
Duration { ticks: TICK_HZ / hz }
let ticks = {
if hz >= TICK_HZ {
1
} else {
(TICK_HZ + hz / 2) / hz
}
};
Duration { ticks }
}
/// Adds one Duration to another, returning a new Duration or None in the event of an overflow.