set_timer 0.1.3

Robust interval execution for Rust, supporting async and sync closures. / Rust 健壮的定时间隔执行库,支持异步和同步闭包。
Documentation
#![cfg_attr(docsrs, feature(doc_cfg))]

use std::{future::Future, time::Duration};

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

#[cfg(feature = "async")]
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;
    }
  })
}

#[cfg(feature = "sync")]
pub fn set_timer<F>(func: F, period: Duration) -> JoinHandle<()>
where
  F: Fn() + Send + Sync + 'static,
{
  spawn(async move {
    loop {
      func();
      sleep(period).await;
    }
  })
}