1use std::future::Future;
2
3#[cfg(not(target_arch = "wasm32"))]
4pub type JoinHandle<F> = tokio::task::JoinHandle<F>;
5
6#[cfg(target_arch = "wasm32")]
9#[derive(Clone, Copy)]
10pub struct FakeJoinHandle<F> {
11 _p: std::marker::PhantomData<F>,
12}
13#[cfg(target_arch = "wasm32")]
14pub type JoinHandle<F> = FakeJoinHandle<F>;
15
16#[cfg(target_arch = "wasm32")]
17#[track_caller]
18pub fn spawn_future<F>(future: F) -> JoinHandle<F::Output>
19where
20 F: Future + 'static,
21{
22 wasm_bindgen_futures::spawn_local(async move {
23 future.await;
25 });
26 FakeJoinHandle {
27 _p: std::marker::PhantomData,
28 }
29}
30
31#[cfg(not(target_arch = "wasm32"))]
33#[track_caller]
34pub fn spawn_future<F>(future: F) -> JoinHandle<F::Output>
35where
36 F: Future + Send + 'static,
37 F::Output: Send + 'static,
38{
39 tokio::spawn(future)
40}
41
42#[cfg(not(target_arch = "wasm32"))]
44#[track_caller]
45pub fn spawn_named_future<F>(future: F, name: &str) -> JoinHandle<F::Output>
46where
47 F: Future + Send + 'static,
48 F::Output: Send + 'static,
49{
50 cfg_if::cfg_if! {if #[cfg(all(tokio_unstable, feature="tokio-tracing"))] {
51 #[allow(clippy::expect_used)]
52 tokio::task::Builder::new().name(name).spawn(future).expect("failed to spawn future")
53 } else {
54 let _ = name;
55 tracing::debug!(r#"the underlying binary hasn't been built with `RUSTFLAGS="--cfg tokio_unstable"` - the future naming won't do anything"#);
56 spawn_future(future)
57 }}
58}
59
60#[cfg(target_arch = "wasm32")]
61#[track_caller]
62pub fn spawn_named_future<F>(future: F, name: &str) -> JoinHandle<F::Output>
63where
64 F: Future + 'static,
65{
66 let _ = name;
68 spawn_future(future)
69}
70
71#[macro_export]
72macro_rules! spawn_future {
73 ($future:expr) => {{ $crate::spawn_future($future) }};
74 ($future:expr, $name:expr) => {{ $crate::spawn_named_future($future, $name) }};
75}