use std::future::Future;
use std::panic::catch_unwind;
use std::thread;
use crossbeam::channel::{unbounded, Sender};
use futures::executor;
use lazy_static::lazy_static;
type Task = async_task::Task<()>;
type JoinHandle<T> = async_task::JoinHandle<T, ()>;
fn spawn<F, R>(future: F) -> JoinHandle<R>
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
{
lazy_static! {
static ref QUEUE: Sender<Task> = {
let (sender, receiver) = unbounded::<Task>();
thread::spawn(|| {
for task in receiver {
let _ignore_panic = catch_unwind(|| task.run());
}
});
sender
};
}
let schedule = |t| QUEUE.send(t).unwrap();
let (task, handle) = async_task::spawn(future, schedule, ());
task.schedule();
handle
}
fn main() {
let handle = spawn(async {
println!("Hello, world!");
});
executor::block_on(handle);
}