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
pub use std::thread::yield_now;
use crate::channel;
use crate::prelude::*;
use std::thread::{Builder, JoinHandle};
pub struct Thread<T> {
inner: JoinHandle<()>,
rx: channel::Receiver<T>,
}
pub fn block_on<T>(future: impl Future<Output = T>) -> T {
async_io::block_on(future)
}
pub fn sleep(dur: Duration) {
if dur.is_infinite() {
std::thread::sleep(std::time::Duration::new(u64::MAX, 0));
} else {
std::thread::sleep(dur.into());
}
}
pub fn start<T: Send + 'static>(
name: impl Into<String>,
func: impl FnOnce() -> T + Send + 'static,
) -> Thread<T> {
let (tx, rx) = channel::with_capacity(1);
let func = move || {
let output = func();
let _ = tx.try_send(output);
};
let inner = Builder::new().name(name.into()).spawn(func).expect("Failed to start thread");
Thread { inner, rx }
}
impl<T> Thread<T> {
pub async fn join(self) -> Result<T, Panic> {
if let Ok(output) = self.rx.recv().await {
return Ok(output);
}
if let Err(value) = self.inner.join() {
return Err(Panic { value });
}
unreachable!("Thread finished but did not send output.");
}
}
impl<T, E> Thread<Result<T, E>>
where
E: From<Panic>,
{
pub async fn try_join(self) -> Result<T, E> {
self.join().await?
}
}