use crate::observer::ObserverNotified;
use std::convert::Infallible;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
#[cfg(test)]
pub use wasm_lite_std as thread;
struct WasmStaticTaskFuture<F, N, E>
where
F: Future + 'static,
N: ObserverNotified<F::Output>,
E: crate::SomeStaticExecutor,
F::Output: 'static + Unpin,
{
spawned: Pin<Box<crate::task::SpawnedStaticTask<F, N, E>>>,
}
impl<F, N, E> WasmStaticTaskFuture<F, N, E>
where
F: Future + 'static,
N: ObserverNotified<F::Output>,
E: crate::SomeStaticExecutor,
F::Output: 'static + Unpin,
{
fn new(spawned: crate::task::SpawnedStaticTask<F, N, E>) -> Self {
Self {
spawned: Box::pin(spawned),
}
}
}
impl<F, N, E> Future for WasmStaticTaskFuture<F, N, E>
where
F: Future + 'static,
N: ObserverNotified<F::Output>,
E: crate::SomeStaticExecutor,
F::Output: 'static + Unpin,
{
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.spawned.as_mut().poll::<Infallible>(cx, None, None)
}
}
impl<F, N, E> Drop for WasmStaticTaskFuture<F, N, E>
where
F: Future + 'static,
N: ObserverNotified<F::Output>,
E: crate::SomeStaticExecutor,
F::Output: 'static + Unpin,
{
fn drop(&mut self) {
wasm_lite_std::task_finished();
}
}
#[allow(dead_code)]
pub(crate) fn run_local_task<F, N, E>(_spawned: crate::task::SpawnedLocalTask<F, N, E>)
where
F: Future,
N: ObserverNotified<F::Output>,
E: for<'a> crate::SomeLocalExecutor<'a>,
{
panic!(
"Local task spawning without a proper executor is no longer supported. Please configure a local executor before spawning local tasks."
);
}
pub(crate) async fn wait_until(deadline: crate::sys::Instant) {
let now = crate::sys::Instant::now();
if deadline <= now {
return;
}
wasm_lite_std::sleep_async(deadline.duration_since(now)).await;
}
pub(crate) fn run_static_task<F, N, E>(spawned: crate::task::SpawnedStaticTask<F, N, E>)
where
F: Future + 'static,
N: ObserverNotified<F::Output>,
E: crate::SomeStaticExecutor,
F::Output: 'static + Unpin,
{
let poll_after = spawned.poll_after();
let wasm_future = WasmStaticTaskFuture::new(spawned);
wasm_lite_std::task_begin();
wasm_lite_std::spawn_local(async move {
wait_until(poll_after).await;
wasm_future.await;
});
}