Skip to main content

aurelin_framework/
timebox.rs

1use std::time::Duration;
2
3use tokio::time::{Instant, sleep_until};
4
5/// Ensures that a failed operation takes at least a configured duration.
6#[derive(Clone, Copy, Debug)]
7pub struct Timebox {
8    deadline: Instant,
9}
10
11impl Timebox {
12    #[must_use]
13    pub fn start(duration: Duration) -> Self {
14        Self { deadline: Instant::now() + duration }
15    }
16
17    pub async fn wait(self) {
18        sleep_until(self.deadline).await;
19    }
20}