use std::time::{Duration, Instant};
pub struct DropWaiter {
start: Instant,
min: Duration,
}
impl DropWaiter {
pub fn new(min: Duration) -> Self {
Self {
start: Instant::now(),
min,
}
}
}
impl Drop for DropWaiter {
fn drop(&mut self) {
let elapsed = self.start.elapsed();
if let Some(remaining) = self.min.checked_sub(elapsed) {
std::thread::sleep(remaining);
}
}
}
#[cfg(feature = "async-security")]
pub struct AsyncDropWaiter {
start: Instant,
waited: bool,
}
#[cfg(feature = "async-security")]
impl AsyncDropWaiter {
pub fn new(min: Duration) -> Self {
Self {
start: Instant::now() + min,
waited: false,
}
}
pub async fn wait(&mut self) {
tokio::time::sleep_until(self.start.into()).await;
self.waited = true;
}
}
#[cfg(feature = "async-security")]
impl Drop for AsyncDropWaiter {
fn drop(&mut self) {
if !self.waited {
panic!("Dropped AsyncDropWaiter without waiting.")
}
}
}