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
use tokio::runtime::current_thread::{self, Handle};
use futures::{IntoFuture, Future};
use std::io;
use std::cell::Cell;
use std::marker::PhantomData;
const RUNTIME_NOT_AVAIL: &'static str = "Runtime is not available! Initialize it or do not use it within blocking calls!";
thread_local!(static TOKIO: Cell<Option<current_thread::Runtime>> = Cell::new(None));
pub struct Runtime {
_dummy: PhantomData<*mut u8>
}
impl Runtime {
pub fn new() -> io::Result<Self> {
let runtime = current_thread::Runtime::new()?;
TOKIO.with(|rt| match rt.replace(None) {
None => rt.set(Some(runtime)),
Some(old) => {
drop(old);
panic!("Double set of runtime!")
}
});
Ok(Runtime {
_dummy: PhantomData
})
}
}
impl Drop for Runtime {
fn drop(&mut self) {
TOKIO.with(|rt| rt.replace(None));
}
}
pub fn init() -> Runtime {
Runtime::new().expect("To create runtime module")
}
pub fn run<R: IntoFuture, F: FnOnce() -> R>(runner: F) -> Result<R::Item, R::Error> {
block_on(runner().into_future())
}
pub fn block_on<F: Future>(future: F) -> Result<F::Item, F::Error> {
TOKIO.with(|rt| match unsafe { &mut *rt.as_ptr() } {
Some(tokio) => tokio.block_on(future),
None => panic!(RUNTIME_NOT_AVAIL)
})
}
pub fn spawn<F: Future<Item=(), Error=()> + 'static>(fut: F) {
TOKIO.with(|rt| match unsafe { &mut *rt.as_ptr() } {
Some(tokio) => tokio.spawn(fut),
None => panic!(RUNTIME_NOT_AVAIL)
});
}
pub fn handle() -> Handle {
TOKIO.with(|rt| match unsafe { &*rt.as_ptr() } {
Some(tokio) => tokio.handle(),
None => panic!(RUNTIME_NOT_AVAIL)
})
}
#[cfg(not(feature = "multi"))]
impl<F: Future> super::AutoRuntime for F {
#[inline]
fn finish(self) -> Result<Self::Item, Self::Error> {
block_on(self)
}
#[inline]
fn spawn(self) where Self: 'static + Future<Item=(), Error=()> {
spawn(self);
}
}