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
#![allow(warnings)]
#![feature(panic_info_message)]
extern crate futures;
extern crate crossbeam_channel;
extern crate parking_lot;
extern crate log;
extern crate pi_local_timer;
pub mod lock;
pub mod rt;
pub mod task;
pub mod local_queue;
use std::thread;
use std::io::Result;
use std::time::Duration;
use futures::{future::BoxFuture, task::ArcWake, FutureExt};
pub trait AsyncTask: ArcWake {
type Out;
fn get_inner(&self) -> Option<BoxFuture<'static, Self::Out>>;
fn set_inner(&self, inner: Option<BoxFuture<'static, Self::Out>>);
}
pub trait AsyncSpawner<T: AsyncTask<Out = O>, O> {
fn can_spawn(&self) -> bool;
fn spawn(&self, task: T) -> Result<()>;
}
pub trait AsyncExecutor {
type Out;
type Task: AsyncTask<Out = Self::Out>;
type Spawner: AsyncSpawner<Self::Task, Self::Out>;
fn get_spawner(&self) -> Self::Spawner;
fn run_once(&mut self) -> AsyncExecutorResult;
fn run(&mut self) -> Result<()> {
loop {
match self.run_once() {
AsyncExecutorResult::Sleep(timeout) => {
thread::sleep(Duration::from_millis(timeout as u64));
continue;
},
AsyncExecutorResult::Stop(result) => {
return result;
},
AsyncExecutorResult::Ok => {
continue;
},
}
}
}
}
#[derive(Debug)]
pub enum AsyncExecutorResult {
Sleep(usize),
Stop(Result<()>),
Ok,
}