gwyh/delayed.rs
1use std::future::Future;
2use std::time::Duration;
3
4use tokio::task::JoinHandle;
5
6pub struct Delayed {
7 joinhandle: JoinHandle<()>,
8}
9
10impl Drop for Delayed {
11 fn drop(&mut self) {
12 self.joinhandle.abort();
13 }
14}
15
16impl Delayed {
17 pub fn new<Fut>(duration: Duration, future: Fut) -> Self
18 where
19 Fut: Future + Send + 'static,
20 {
21 let joinhandle = tokio::spawn(async move {
22 tokio::time::sleep(duration).await;
23 future.await;
24 });
25 Self { joinhandle }
26 }
27}