pub struct ScopedSleep {
target_time: std::time::Instant,
}
impl ScopedSleep {
pub fn new(sleep_duration: std::time::Duration) -> Self {
ScopedSleep {
target_time: std::time::Instant::now() + sleep_duration,
}
}
pub fn from_secs(sleep_duration_sec: f64) -> Self {
Self::new(std::time::Duration::from_secs_f64(sleep_duration_sec))
}
}
impl Drop for ScopedSleep {
fn drop(&mut self) {
let now = std::time::Instant::now();
if now < self.target_time {
std::thread::sleep(self.target_time - now);
}
}
}