use core::cell::RefCell;
use core::future::Future;
use executor_core::LocalExecutor;
use executor_core::async_task::{self, AsyncTask, Runnable};
thread_local! {
static PARKED_RUNNABLES: RefCell<Vec<Runnable>> = const { RefCell::new(Vec::new()) };
}
#[derive(Clone, Copy, Debug, Default)]
pub struct TestLocalExecutor;
impl LocalExecutor for TestLocalExecutor {
type Task<T: 'static> = AsyncTask<T>;
fn spawn_local<Fut>(&self, fut: Fut) -> Self::Task<Fut::Output>
where
Fut: Future + 'static,
{
let (runnable, task) = async_task::spawn_local(fut, |runnable: Runnable| {
PARKED_RUNNABLES.with(|parked| parked.borrow_mut().push(runnable));
});
runnable.schedule();
task
}
}
pub fn install_test_executor() {
let _ = executor_core::try_init_local_executor(TestLocalExecutor);
}
#[must_use]
pub fn drain_parked_local_work() -> usize {
let ready = PARKED_RUNNABLES.with(|parked| core::mem::take(&mut *parked.borrow_mut()));
let count = ready.len();
for runnable in ready {
runnable.run();
}
count
}