Last active 1741880596

tokio_timer.rs Raw
1#[macro_export]
2/// Runs your code after Duration has passed
3/// # Examples
4
5/// ```
6/// timeout!(Duration::from_seconds(15), {
7/// println!("Shutting down now...")
8/// });
9/// ```
10///
11/// This is the same as:
12/// ```
13/// tokio::spawn(async move {
14/// let mut timer = tokio::time::interval(Duration::from_seconds(15);
15/// timer.tick().await;
16/// //your function
17/// })
18/// ```
19macro_rules! timeout {
20 ( $i:expr, $x:expr) => {
21 {
22 tokio::spawn(async move {
23 tokio::time::sleep($i).await;
24 $x;
25 })
26 }
27 };
28}
29
30#[macro_export]
31/// Creates a timer that calls a function every Duration
32/// # Examples
33
34/// ```
35/// timer!(update_interval, {
36/// let mut lock = manager.lock().await;
37/// });
38/// ```
39///
40/// This is the same as:
41/// ```
42/// tokio::spawn(async move {
43/// let mut timer = tokio::time::interval(update_interval);
44/// timer.tick().await;
45/// loop {
46/// timer.tick().await;
47/// // your function
48/// }
49/// })
50/// ```
51macro_rules! timer {
52 ( $i:expr, $x:expr) => {
53 {
54 tokio::spawn(async move {
55 let mut timer = tokio::time::interval($i);
56 timer.tick().await;
57 loop {
58 timer.tick().await;
59 $x;
60 }
61 })
62 }
63 };
64}
65
66#[macro_export]
67/// Creates a timer that calls a function every Duration with an additional function called on start
68///
69/// See the [timer!] macro for more information
70/// # Examples
71
72/// ```
73/// timer_start!(update_interval, {
74/// println!("my timer is started!");
75/// }, {
76/// let mut lock = manager.lock().await;
77/// });
78/// ```
79macro_rules! timer_start {
80 ( $i:expr, $s:expr, $x:expr) => {
81 {
82 tokio::spawn(async move {
83 let mut timer = tokio::time::interval($i);
84 timer.tick().await;
85 $s;
86 loop {
87 timer.tick().await;
88 $x;
89 }
90 })
91 }
92 };
93}
94