-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecutor.rs
61 lines (53 loc) · 1.59 KB
/
executor.rs
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
use std::{
panic::{self, catch_unwind, UnwindSafe},
pin::Pin,
task::Poll,
};
use crate::cancelable::Future;
/// A simple executor that runs a single root future.
///
/// Normally runs to completion, but can be cancelled.
pub struct Executor<F: Future> {
task: Option<Pin<Box<F>>>,
complete: bool,
}
impl<F: Future> Executor<F> {
pub fn new(future: F) -> Self {
Self {
task: Some(Box::pin(future)),
complete: false,
}
}
pub fn poll(&mut self) -> Poll<F::Output> {
// we temporarily remove the root task so that if it panics we don't try
// to resume it.
let mut task = self.task.take();
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
let poll = task.as_mut().unwrap().as_mut().poll(&mut cx);
self.task = task;
match poll {
Poll::Ready(v) => {
self.complete = true;
Poll::Ready(v)
}
Poll::Pending => Poll::Pending,
}
}
pub fn run(mut self) -> F::Output {
loop {
if let Poll::Ready(v) = self.poll() {
return v;
}
}
}
}
impl<F: Future> Drop for Executor<F> {
fn drop(&mut self) {
if !self.complete
&& let Some(mut task) = self.task.take()
{
let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref());
while let Poll::Pending = task.as_mut().poll_cancel(&mut cx) {}
}
}
}