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
use aselect::aselect;
use futures::StreamExt;
use std::pin::pin;
use std::time::Duration;
use tokio::time::{sleep, Instant};
#[tokio::main]
async fn main() {
let counter = 0u32;
let mut stream = pin!(aselect!(
{
// Capture variable 'counter'
mutable(counter);
},
// First select arm. A unique name for each arm must be provided (`timer1`).
// Sleeps 0.3 seconds, over and over.
timer1(
{
// Print value of counter, then increment
println!("Counter = {:?}", counter);
*counter += 1;
// Create a future. Will be available to async block below.
sleep(Duration::from_millis(300))
},
async |sleep| {
// 'sleep' is the future created above
let sleep_start = Instant::now();
sleep.await;
// Value returned by this async block is given to block below
sleep_start.elapsed()
},
|time_slept| {
// Print value returned from future
println!("Slept {:?}", time_slept);
// Do not produce a result from the 'aselect' future.
None
}
),
// Second select arm.
// Sleeps 1 second, then produces a value.
timer2(
{
// Similar to above, but now sleep 10 seconds
tokio::time::sleep(tokio::time::Duration::from_secs(1))
},
async |sleep| {
sleep.await;
},
|time_slept| {
// After the 10 seconds have elapsed,
Some("finished")
}
),
));
while let Some(value) = stream.next().await {
println!("Produced value: {}", value);
}
}