Skip to content

Commit 7b21df5

Browse files
author
Stjepan Glavina
committed
Simplify
1 parent d6505ef commit 7b21df5

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

examples/priority.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,30 @@ impl PriorityExecutor {
3939
self.ex[priority as usize].spawn(future)
4040
}
4141

42-
/// Runs the executor until the future completes.
43-
async fn run<T>(&self, future: impl Future<Output = T>) -> T {
44-
future
45-
.or(async {
46-
// Keep ticking inner executors forever.
47-
loop {
48-
let t0 = self.ex[0].tick();
49-
let t1 = self.ex[1].tick();
50-
let t2 = self.ex[2].tick();
42+
/// Runs the executor forever.
43+
async fn run(&self) {
44+
loop {
45+
for _ in 0..200 {
46+
let t0 = self.ex[0].tick();
47+
let t1 = self.ex[1].tick();
48+
let t2 = self.ex[2].tick();
5149

52-
// Wait until one of the ticks completes, trying them in order from highest
53-
// priority to lowest priority.
54-
t0.or(t1).or(t2).await;
55-
}
56-
})
57-
.await
50+
// Wait until one of the ticks completes, trying them in order from highest
51+
// priority to lowest priority.
52+
t0.or(t1).or(t2).await;
53+
}
54+
55+
// Yield every now and then.
56+
future::yield_now().await;
57+
}
5858
}
5959
}
6060

6161
fn main() {
6262
static EX: PriorityExecutor = PriorityExecutor::new();
6363

6464
// Spawn a thread running the executor forever.
65-
thread::spawn(|| {
66-
let forever = future::pending::<()>();
67-
future::block_on(EX.run(forever));
68-
});
65+
thread::spawn(|| future::block_on(EX.run()));
6966

7067
let mut tasks = Vec::new();
7168

0 commit comments

Comments
 (0)