set_timer 0.1.4

Robust interval execution for Rust, supporting async and sync closures. / Rust 健壮的定时间隔执行库,支持异步和同步闭包。
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use std::{future::Future, time::Duration};

use tokio::{spawn, task::JoinHandle, time::sleep};

pub fn set_timer_async<F, Fut>(func: F, period: Duration) -> JoinHandle<()>
where
  F: Fn() -> Fut + Send + Sync + 'static,
  Fut: Future<Output = ()> + Send,
{
  spawn(async move {
    loop {
      func().await;
      sleep(period).await;
    }
  })
}