Last active 1741880596

Revision c963d3d080d36f23d2239c6848c0e231e15a54e4

example.rs Raw
1fn main() {
2 timer_start!(cleanup_interval, {
3 println!("[Cleanup] Cleaning messages every {} seconds", cleanup_interval.as_secs());
4 }, {
5 let mut lock = manager.lock().await;
6 lock.cleanup();
7 });
8}
tokio_timer.rs Raw
1#[macro_export]
2/// Creates a timer that calls a function every Duration
3/// # Examples
4
5/// ```
6/// timer!(update_interval, {
7/// let mut lock = manager.lock().await;
8/// });
9/// ```
10///
11/// This is the same as:
12/// ```
13/// tokio::spawn(async move {
14/// let mut timer = tokio::time::interval(update_interval);
15/// timer.tick().await;
16/// loop {
17/// timer.tick().await;
18/// // your function
19/// }
20/// })
21/// ```
22macro_rules! timer {
23 ( $i:expr, $x:expr) => {
24 {
25 tokio::spawn(async move {
26 let mut timer = tokio::time::interval($i);
27 timer.tick().await;
28 loop {
29 timer.tick().await;
30 $x;
31 }
32 })
33 }
34 };
35}
36
37#[macro_export]
38/// Creates a timer that calls a function every Duration with an additional function called on start
39///
40/// See the [timer!] macro for more information
41/// # Examples
42
43/// ```
44/// timer_start!(update_interval, {
45/// println!("my timer is started!");
46/// }, {
47/// let mut lock = manager.lock().await;
48/// });
49/// ```
50macro_rules! timer_start {
51 ( $i:expr, $s:expr, $x:expr) => {
52 {
53 tokio::spawn(async move {
54 let mut timer = tokio::time::interval($i);
55 timer.tick().await;
56 $s;
57 loop {
58 timer.tick().await;
59 $x;
60 }
61 })
62 }
63 };
64}
65