mod bevy_runtime;
use std::future::Future;
use bevy::prelude::{Deref, DerefMut, Resource};
#[derive(Resource, DerefMut, Deref)]
pub struct EventworkRuntime<RT: Runtime + Send + Sync>(pub RT);
pub trait Runtime: Send + Sync + 'static {
type JoinHandle: JoinHandle;
fn spawn(&self, task: impl Future<Output = ()> + Send + 'static) -> Self::JoinHandle;
fn spawn_local(&self, task: impl Future<Output = ()> + 'static) -> Self::JoinHandle;
}
pub trait JoinHandle: 'static + Send + Sync {
fn abort(&mut self);
}
#[cfg(not(target_arch = "wasm32"))]
pub fn run_async<F, RT: Runtime>(future: F, runtime: &RT) -> <RT as Runtime>::JoinHandle
where
F: Future<Output = ()> + Send + 'static,
{
runtime.spawn(future)
}
#[cfg(target_arch = "wasm32")]
pub fn run_async<F, RT: Runtime>(future: F, runtime: &RT) -> <RT as Runtime>::JoinHandle
where
F: Future<Output = ()> + 'static,
{
runtime.spawn_local(future)
}