example.rs
· 230 B · Rust
Raw
fn main() {
timer_start!(cleanup_interval, {
println!("[Cleanup] Cleaning messages every {} seconds", cleanup_interval.as_secs());
}, {
let mut lock = manager.lock().await;
lock.cleanup();
});
}
| 1 | fn 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
· 1.5 KiB · Rust
Raw
#[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 | /// 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 | /// ``` |
| 22 | macro_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 | /// ``` |
| 50 | macro_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 |