veilid_tools/
spawn.rs

1use super::*;
2
3cfg_if! {
4    if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
5        use async_executors::{Bindgen, LocalSpawnHandleExt, SpawnHandleExt};
6
7        pub fn spawn<Out>(_name: &str, future: impl Future<Output = Out> + Send + 'static) -> MustJoinHandle<Out>
8        where
9            Out: Send + 'static,
10        {
11            MustJoinHandle::new(
12                Bindgen
13                    .spawn_handle(future)
14                    .expect("wasm-bindgen-futures spawn_handle_local should never error out"),
15            )
16        }
17
18        pub fn spawn_local<Out>(_name: &str, future: impl Future<Output = Out> + 'static) -> MustJoinHandle<Out>
19        where
20            Out: 'static,
21        {
22            MustJoinHandle::new(
23                Bindgen
24                    .spawn_handle_local(future)
25                    .expect("wasm-bindgen-futures spawn_handle_local should never error out"),
26            )
27        }
28
29        pub fn spawn_detached<Out>(_name: &str, future: impl Future<Output = Out> + Send + 'static)
30        where
31            Out: Send + 'static,
32        {
33            Bindgen
34                .spawn_handle_local(future)
35                .expect("wasm-bindgen-futures spawn_handle_local should never error out")
36                .detach()
37        }
38        pub fn spawn_detached_local<Out>(_name: &str, future: impl Future<Output = Out> + 'static)
39        where
40            Out: 'static,
41        {
42            Bindgen
43                .spawn_handle_local(future)
44                .expect("wasm-bindgen-futures spawn_handle_local should never error out")
45                .detach()
46        }
47
48    } else {
49
50        pub fn spawn<Out>(name: &str, future: impl Future<Output = Out> + Send + 'static) -> MustJoinHandle<Out>
51        where
52            Out: Send + 'static,
53        {
54            cfg_if! {
55                if #[cfg(feature="rt-async-std")] {
56                    MustJoinHandle::new(async_std::task::Builder::new().name(name.to_string()).spawn(future).unwrap())
57                } else if #[cfg(all(tokio_unstable, feature="rt-tokio", feature="tracing"))] {
58                    MustJoinHandle::new(tokio::task::Builder::new().name(name).spawn(future).unwrap())
59                } else if #[cfg(feature="rt-tokio")] {
60                    let _name = name;
61                    MustJoinHandle::new(tokio::task::spawn(future))
62                }
63            }
64        }
65
66        pub fn spawn_local<Out>(name: &str, future: impl Future<Output = Out> + 'static) -> MustJoinHandle<Out>
67        where
68            Out: 'static,
69        {
70            cfg_if! {
71                if #[cfg(feature="rt-async-std")] {
72                    MustJoinHandle::new(async_std::task::Builder::new().name(name.to_string()).local(future).unwrap())
73                } else if #[cfg(all(tokio_unstable, feature="rt-tokio", feature="tracing"))] {
74                    MustJoinHandle::new(tokio::task::Builder::new().name(name).spawn_local(future).unwrap())
75                } else if #[cfg(feature="rt-tokio")] {
76                    let _name = name;
77                    MustJoinHandle::new(tokio::task::spawn_local(future))
78                }
79            }
80        }
81
82        pub fn spawn_detached<Out>(name: &str, future: impl Future<Output = Out> + Send + 'static)
83        where
84            Out: Send + 'static,
85        {
86            cfg_if! {
87                if #[cfg(feature="rt-async-std")] {
88                    drop(async_std::task::Builder::new().name(name.to_string()).spawn(future).unwrap());
89                } else if #[cfg(all(tokio_unstable, feature="rt-tokio", feature="tracing"))] {
90                    drop(tokio::task::Builder::new().name(name).spawn(future).unwrap());
91                } else if #[cfg(feature="rt-tokio")] {
92                    let _name = name;
93                    drop(tokio::task::spawn(future))
94                }
95            }
96        }
97
98        pub fn spawn_detached_local<Out>(name: &str,future: impl Future<Output = Out> + 'static)
99        where
100            Out: 'static,
101        {
102            cfg_if! {
103                if #[cfg(feature="rt-async-std")] {
104                    drop(async_std::task::Builder::new().name(name.to_string()).local(future).unwrap());
105                } else if #[cfg(all(tokio_unstable, feature="rt-tokio", feature="tracing"))] {
106                    drop(tokio::task::Builder::new().name(name).spawn_local(future).unwrap());
107                } else if #[cfg(feature="rt-tokio")] {
108                    let _name = name;
109                    drop(tokio::task::spawn_local(future))
110                }
111            }
112        }
113
114        #[allow(unused_variables)]
115        pub async fn blocking_wrapper<F, R>(name: &str, blocking_task: F, err_result: R) -> R
116        where
117            F: FnOnce() -> R + Send + 'static,
118            R: Send + 'static,
119        {
120            // run blocking stuff in blocking thread
121            cfg_if! {
122                if #[cfg(feature="rt-async-std")] {
123                    let _name = name;
124                    // async_std::task::Builder blocking doesn't work like spawn_blocking()
125                    async_std::task::spawn_blocking(blocking_task).await
126                } else if #[cfg(all(tokio_unstable, feature="rt-tokio", feature="tracing"))] {
127                    tokio::task::Builder::new().name(name).spawn_blocking(blocking_task).unwrap().await.unwrap_or(err_result)
128                } else if #[cfg(feature="rt-tokio")] {
129                    let _name = name;
130                    tokio::task::spawn_blocking(blocking_task).await.unwrap_or(err_result)
131                } else {
132                    #[compile_error("must use an executor")]
133                }
134            }
135        }
136    }
137}