Last active 1 year ago

jackz's Avatar jackz revised this gist 1 year ago. Go to revision

No changes

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

1 file changed, 29 insertions

tokio_timer.rs

@@ -1,3 +1,32 @@
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 +
1 30 #[macro_export]
2 31 /// Creates a timer that calls a function every Duration
3 32 /// # Examples

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

1 file changed, 8 deletions

example.rs (file deleted)

@@ -1,8 +0,0 @@
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 - }

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

1 file changed, 8 insertions

example.rs(file created)

@@ -0,0 +1,8 @@
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 + }

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

1 file changed, 8 deletions

example.rs (file deleted)

@@ -1,8 +0,0 @@
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 - }

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

No changes

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

No changes

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

1 file changed, 8 insertions

example.rs(file created)

@@ -0,0 +1,8 @@
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 + }

jackz's Avatar Jackz revised this gist 2 years ago. Go to revision

1 file changed, 64 insertions

tokio_timer.rs(file created)

@@ -0,0 +1,64 @@
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 + }
Newer Older