telltale_runtime/util/spawn.rs
1//! Runtime abstraction layer for cross-platform async execution
2//!
3//! Provides platform-specific implementations for spawning tasks and running futures.
4//! Supports both native (Tokio) and WASM (wasm-bindgen-futures) targets.
5
6use cfg_if::cfg_if;
7use std::future::Future;
8
9/// Marker trait for runtime implementations (not used as trait object)
10pub trait AsyncRuntime: Send + Sync + 'static {}
11
12/// Helper function to spawn a task using platform-specific runtime
13///
14/// On native targets, uses `tokio::spawn`.
15/// On WASM targets, uses `wasm_bindgen_futures::spawn_local`.
16pub fn spawn<F>(future: F)
17where
18 F: Future<Output = ()> + Send + 'static,
19{
20 cfg_if! {
21 if #[cfg(target_arch = "wasm32")] {
22 wasm_bindgen_futures::spawn_local(future);
23 } else {
24 tokio::spawn(future);
25 }
26 }
27}
28
29/// Helper function to spawn a local task using platform-specific runtime
30///
31/// On native targets, uses `tokio::task::spawn_local`.
32/// On WASM targets, uses `wasm_bindgen_futures::spawn_local`.
33pub fn spawn_local<F>(future: F)
34where
35 F: Future<Output = ()> + 'static,
36{
37 cfg_if! {
38 if #[cfg(target_arch = "wasm32")] {
39 wasm_bindgen_futures::spawn_local(future);
40 } else {
41 tokio::task::spawn_local(future);
42 }
43 }
44}