use std::fmt;
pub async fn run_blocking<F, R>(f: F) -> Result<R, BlockingError>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
#[cfg(not(target_arch = "wasm32"))]
{
tokio::task::spawn_blocking(f).await.map_err(BlockingError)
}
#[cfg(target_arch = "wasm32")]
{
Ok(f())
}
}
#[derive(Debug)]
pub struct BlockingError(#[cfg(not(target_arch = "wasm32"))] tokio::task::JoinError);
impl fmt::Display for BlockingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(not(target_arch = "wasm32"))]
{
self.0.fmt(f)
}
#[cfg(target_arch = "wasm32")]
{
f.write_str("blocking work failed")
}
}
}
impl std::error::Error for BlockingError {}