-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
proc_run.rs
56 lines (47 loc) · 1.23 KB
/
proc_run.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
//! A function that runs a future to completion on a dedicated thread.
use std::future::Future;
use std::sync::Arc;
use std::thread;
use crossbeam::channel;
use futures::executor;
use lightproc::prelude::*;
use std::sync::atomic::AtomicUsize;
fn spawn_on_thread<F, R>(fut: F) -> ProcHandle<R>
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
let (sender, receiver) = channel::unbounded();
let sender = Arc::new(sender);
let s = Arc::downgrade(&sender);
let future = async move {
let _ = sender;
fut.await
};
let schedule = move |t| s.upgrade().unwrap().send(t).unwrap();
let (proc, handle) = LightProc::build(
future,
schedule,
ProcStack {
pid: AtomicUsize::new(1),
after_complete: Some(Arc::new(|| {
println!("After complete");
})),
after_start: Some(Arc::new(|| {
println!("After start");
}))
}
);
proc.schedule();
thread::spawn(move || {
for proc in receiver {
proc.run();
}
});
handle
}
fn main() {
executor::block_on(spawn_on_thread(async {
println!("Hello, world!");
}));
}