blob: 07d6acdf978879da49a404ea5b5794e700fe615e [file] [log] [blame]
//! A simple single-threaded executor.
use std::future::Future;
use std::panic::catch_unwind;
use std::thread;
use async_task::{JoinHandle, Runnable};
use futures_lite::future;
use once_cell::sync::Lazy;
/// Spawns a future on the executor.
fn spawn<F, T>(future: F) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
// A channel that holds scheduled tasks.
static QUEUE: Lazy<flume::Sender<Runnable>> = Lazy::new(|| {
let (sender, receiver) = flume::unbounded::<Runnable>();
// Start the executor thread.
thread::spawn(|| {
for runnable in receiver {
// Ignore panics for simplicity.
let _ignore_panic = catch_unwind(|| runnable.run());
}
});
sender
});
// Create a task that is scheduled by sending itself into the channel.
let schedule = |t| QUEUE.send(t).unwrap();
let (runnable, handle) = async_task::spawn(future, schedule);
// Schedule the task by sending it into the channel.
runnable.schedule();
handle
}
fn main() {
// Spawn a future and await its result.
let handle = spawn(async {
println!("Hello, world!");
});
future::block_on(handle);
}