mirror of
https://github.com/Xevion/smart-rgb.git
synced 2025-12-06 01:16:24 -06:00
60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use std::future::Future;
|
|
|
|
/// Spawn an async task on the appropriate runtime for the platform.
|
|
///
|
|
/// On native targets, uses tokio::spawn for multi-threaded execution.
|
|
/// On WASM targets, uses wasm_bindgen_futures::spawn_local for browser integration.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub fn spawn_task<F>(future: F)
|
|
where
|
|
F: Future<Output = ()> + Send + 'static,
|
|
{
|
|
tokio::spawn(future);
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub fn spawn_task<F>(future: F)
|
|
where
|
|
F: Future<Output = ()> + 'static,
|
|
{
|
|
wasm_bindgen_futures::spawn_local(future);
|
|
}
|
|
|
|
/// Spawn an async task that returns a value on the appropriate runtime.
|
|
///
|
|
/// On native targets, uses tokio::spawn and returns a JoinHandle.
|
|
/// On WASM targets, uses wasm_bindgen_futures::spawn_local and immediately returns None
|
|
/// since WASM doesn't support waiting on spawned tasks.
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
pub fn spawn_task_with_handle<F, T>(future: F) -> tokio::task::JoinHandle<T>
|
|
where
|
|
F: Future<Output = T> + Send + 'static,
|
|
T: Send + 'static,
|
|
{
|
|
tokio::spawn(future)
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub fn spawn_task_with_handle<F, T>(future: F)
|
|
where
|
|
F: Future<Output = T> + 'static,
|
|
T: 'static,
|
|
{
|
|
wasm_bindgen_futures::spawn_local(async move {
|
|
let _ = future.await;
|
|
});
|
|
}
|
|
|
|
/// Extension trait to convert any Result into anyhow::Result with string error conversion.
|
|
///
|
|
/// This is useful for WASM where some error types don't implement std::error::Error.
|
|
pub trait IntoAnyhow<T> {
|
|
fn into_anyhow(self) -> anyhow::Result<T>;
|
|
}
|
|
|
|
impl<T, E: std::fmt::Display> IntoAnyhow<T> for Result<T, E> {
|
|
fn into_anyhow(self) -> anyhow::Result<T> {
|
|
self.map_err(|e| anyhow::anyhow!(e.to_string()))
|
|
}
|
|
}
|