1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use ;
use Display;
use Hash;
use Duration;
use StreamExt;
use ;
/// An abstration over [`DelayQueue`] that allows you to create a delay, with associated data.
///
/// Users can add data to the delay-map with [`insert()`](DelayHandler::insert). The associated data
/// is removed and returned when delay is timedout by `.await`ing on [`next()`](DelayHandler::next).
/// Users can also prematurely remove the delay from the delay-map with [`remove()`](DelayHandler::remove).
///
/// ### Examples
/// 1. Insert 3 numbers into delay-map with 10s delays, print them as they timeout
/// ```no_run
/// # use delay_handler::DelayHandler;
/// # use std::time::Duration;
/// # async fn run() {
/// let mut handler = DelayHandler::default();
/// // Adds 1, 2, 3 to the delay-map, each with 10s delay
/// handler.insert(1, Duration::from_secs(10));
/// handler.insert(2, Duration::from_secs(10));
/// handler.insert(3, Duration::from_secs(10));
///
/// // Expect a delay of ~10s, after which 1, 2, 3 should print to stdout, in quick succession.
/// while let Some(expired) = handler.next().await {
/// println!("{}", expired);
/// }
/// # }
/// ```
/// 2. Insert 3 numbers into delay-map with different delays, print them as they timeout
/// ```no_run
/// # use delay_handler::DelayHandler;
/// # use std::time::Duration;
/// # async fn run() {
/// let mut handler = DelayHandler::default();
/// // Adds 1, 2 to the delay-map, with different delays
/// handler.insert(1, Duration::from_secs(10));
/// handler.insert(2, Duration::from_secs(5));
///
/// // With a delay of ~5s between, the prints should come in the order of 2 and 1.
/// while let Some(expired) = handler.next().await {
/// println!("{}", expired);
/// }
/// # }
/// ```
///
/// 3. Insert 3 numbers into delay-map with different delays, remove print as delays are timedout
/// ```no_run
/// # use delay_handler::DelayHandler;
/// # use std::time::Duration;
/// # async fn run() {
/// let mut handler = DelayHandler::default();
/// // Adds 1, 2, 3 to the delay-map, each with different delays
/// handler.insert(1, Duration::from_secs(15));
/// handler.insert(2, Duration::from_secs(5));
/// handler.insert(3, Duration::from_secs(10));
///
/// // Remove 3 from the delay-map
/// handler.remove(&3);
///
/// // Prints should be in the order of first 2 and ~10s later 1.
/// while let Some(expired) = handler.next().await {
/// println!("{}", expired);
/// }
/// # }
/// ```