123 lines
4.6 KiB
Rust
Raw Normal View History

2022-11-26 21:37:23 -05:00
use super::*;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use async_executors::{Bindgen, LocalSpawnHandleExt, SpawnHandleExt};
2024-07-20 19:42:25 -04:00
pub fn spawn<Out>(_name: &str, future: impl Future<Output = Out> + Send + 'static) -> MustJoinHandle<Out>
2022-11-26 21:37:23 -05:00
where
Out: Send + 'static,
{
MustJoinHandle::new(
Bindgen
.spawn_handle(future)
.expect("wasm-bindgen-futures spawn_handle_local should never error out"),
)
}
2024-07-20 19:42:25 -04:00
pub fn spawn_local<Out>(_name: &str, future: impl Future<Output = Out> + 'static) -> MustJoinHandle<Out>
2022-11-26 21:37:23 -05:00
where
Out: 'static,
{
MustJoinHandle::new(
Bindgen
.spawn_handle_local(future)
.expect("wasm-bindgen-futures spawn_handle_local should never error out"),
)
}
2024-07-20 19:42:25 -04:00
pub fn spawn_detached<Out>(_name: &str, future: impl Future<Output = Out> + Send + 'static)
2022-11-26 21:37:23 -05:00
where
Out: Send + 'static,
{
Bindgen
.spawn_handle_local(future)
.expect("wasm-bindgen-futures spawn_handle_local should never error out")
.detach()
}
2024-07-20 19:42:25 -04:00
pub fn spawn_detached_local<Out>(_name: &str, future: impl Future<Output = Out> + 'static)
2022-11-26 21:37:23 -05:00
where
Out: 'static,
{
Bindgen
.spawn_handle_local(future)
.expect("wasm-bindgen-futures spawn_handle_local should never error out")
.detach()
}
} else {
2024-07-20 19:42:25 -04:00
pub fn spawn<Out>(name: &str, future: impl Future<Output = Out> + Send + 'static) -> MustJoinHandle<Out>
2022-11-26 21:37:23 -05:00
where
Out: Send + 'static,
{
cfg_if! {
if #[cfg(feature="rt-async-std")] {
2024-07-21 22:14:04 -04:00
MustJoinHandle::new(async_std::task::Builder::new().name(name.to_string()).spawn(future).unwrap())
2022-11-26 21:37:23 -05:00
} else if #[cfg(feature="rt-tokio")] {
2024-07-20 19:42:25 -04:00
MustJoinHandle::new(tokio::task::Builder::new().name(name).spawn(future).unwrap())
2022-11-26 21:37:23 -05:00
}
}
}
2024-07-20 19:42:25 -04:00
pub fn spawn_local<Out>(name: &str, future: impl Future<Output = Out> + 'static) -> MustJoinHandle<Out>
2022-11-26 21:37:23 -05:00
where
Out: 'static,
{
cfg_if! {
if #[cfg(feature="rt-async-std")] {
2024-07-21 22:14:04 -04:00
MustJoinHandle::new(async_std::task::Builder::new().name(name.to_string()).local(future).unwrap())
2022-11-26 21:37:23 -05:00
} else if #[cfg(feature="rt-tokio")] {
2024-07-20 19:42:25 -04:00
MustJoinHandle::new(tokio::task::Builder::new().name(name).spawn_local(future).unwrap())
2022-11-26 21:37:23 -05:00
}
}
}
2024-07-20 19:42:25 -04:00
pub fn spawn_detached<Out>(name: &str, future: impl Future<Output = Out> + Send + 'static)
2022-11-26 21:37:23 -05:00
where
Out: Send + 'static,
{
cfg_if! {
if #[cfg(feature="rt-async-std")] {
2024-07-21 22:14:04 -04:00
drop(async_std::task::Builder::new().name(name.to_string()).spawn(future).unwrap());
2022-11-26 21:37:23 -05:00
} else if #[cfg(feature="rt-tokio")] {
2024-07-20 19:42:25 -04:00
drop(tokio::task::Builder::new().name(name).spawn(future).unwrap());
2022-11-26 21:37:23 -05:00
}
}
}
2024-07-20 19:42:25 -04:00
pub fn spawn_detached_local<Out>(name: &str,future: impl Future<Output = Out> + 'static)
2022-11-26 21:37:23 -05:00
where
Out: 'static,
{
cfg_if! {
if #[cfg(feature="rt-async-std")] {
2024-07-21 22:14:04 -04:00
drop(async_std::task::Builder::new().name(name.to_string()).local(future).unwrap());
2022-11-26 21:37:23 -05:00
} else if #[cfg(feature="rt-tokio")] {
2024-07-20 19:42:25 -04:00
drop(tokio::task::Builder::new().name(name).spawn_local(future).unwrap());
2022-11-26 21:37:23 -05:00
}
}
}
2022-11-27 11:27:06 -05:00
#[allow(unused_variables)]
2024-07-20 19:42:25 -04:00
pub async fn blocking_wrapper<F, R>(name: &str, blocking_task: F, err_result: R) -> R
2022-11-26 21:37:23 -05:00
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
// run blocking stuff in blocking thread
cfg_if! {
if #[cfg(feature="rt-async-std")] {
2024-07-21 22:14:04 -04:00
let _name = name;
// async_std::task::Builder blocking doesn't work like spawn_blocking()
async_std::task::spawn_blocking(blocking_task).await
2022-11-26 21:37:23 -05:00
} else if #[cfg(feature="rt-tokio")] {
2024-07-20 19:42:25 -04:00
tokio::task::Builder::new().name(name).spawn_blocking(blocking_task).unwrap().await.unwrap_or(err_result)
2022-11-26 21:37:23 -05:00
} else {
#[compile_error("must use an executor")]
}
}
}
}
}