tokio_timer.rs
· 2.0 KiB · Rust
Raw
#[macro_export]
/// Runs your code after Duration has passed
/// # Examples
/// ```
/// timeout!(Duration::from_seconds(15), {
/// println!("Shutting down now...")
/// });
/// ```
///
/// This is the same as:
/// ```
/// tokio::spawn(async move {
/// let mut timer = tokio::time::interval(Duration::from_seconds(15);
/// timer.tick().await;
/// //your function
/// })
/// ```
macro_rules! timeout {
( $i:expr, $x:expr) => {
{
tokio::spawn(async move {
tokio::time::sleep($i).await;
$x;
})
}
};
}
#[macro_export]
/// Creates a timer that calls a function every Duration
/// # Examples
/// ```
/// timer!(update_interval, {
/// let mut lock = manager.lock().await;
/// });
/// ```
///
/// This is the same as:
/// ```
/// tokio::spawn(async move {
/// let mut timer = tokio::time::interval(update_interval);
/// timer.tick().await;
/// loop {
/// timer.tick().await;
/// // your function
/// }
/// })
/// ```
macro_rules! timer {
( $i:expr, $x:expr) => {
{
tokio::spawn(async move {
let mut timer = tokio::time::interval($i);
timer.tick().await;
loop {
timer.tick().await;
$x;
}
})
}
};
}
#[macro_export]
/// Creates a timer that calls a function every Duration with an additional function called on start
///
/// See the [timer!] macro for more information
/// # Examples
/// ```
/// timer_start!(update_interval, {
/// println!("my timer is started!");
/// }, {
/// let mut lock = manager.lock().await;
/// });
/// ```
macro_rules! timer_start {
( $i:expr, $s:expr, $x:expr) => {
{
tokio::spawn(async move {
let mut timer = tokio::time::interval($i);
timer.tick().await;
$s;
loop {
timer.tick().await;
$x;
}
})
}
};
}
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 | /// ``` |
19 | macro_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 | /// ``` |
51 | macro_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 | /// ``` |
79 | macro_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 |