#[cfg(target_arch = "wasm32")]
mod wasm_time;
pub mod time {
pub use web_time::{Duration, Instant};
#[cfg(not(target_arch = "wasm32"))]
pub use tokio::time::{error::Elapsed, sleep, timeout};
#[cfg(target_arch = "wasm32")]
pub use super::wasm_time::{Elapsed, sleep, timeout};
}
pub mod task {
pub struct AbortOnDropHandle<T>(tokio::task::JoinHandle<T>);
impl<T> Drop for AbortOnDropHandle<T> {
fn drop(&mut self) {
self.0.abort();
}
}
impl<T> AbortOnDropHandle<T> {
pub fn new(handle: tokio::task::JoinHandle<T>) -> Self {
Self(handle)
}
}
impl<T> std::ops::Deref for AbortOnDropHandle<T> {
type Target = tokio::task::JoinHandle<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> std::future::Future for AbortOnDropHandle<T> {
type Output = Result<T, tokio::task::JoinError>;
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
std::pin::Pin::new(&mut self.0).poll(cx)
}
}
}
macro_rules! maybe_spawn_blocking {
($body:expr) => {{
#[cfg(not(target_arch = "wasm32"))]
{
tokio::task::spawn_blocking(move || $body).await?
}
#[cfg(target_arch = "wasm32")]
{
$body
}
}};
}
macro_rules! join_set_spawn {
($set:expr, $fut:expr) => {{
#[cfg(not(target_arch = "wasm32"))]
$set.spawn($fut);
#[cfg(target_arch = "wasm32")]
$set.spawn_local($fut);
}};
}
macro_rules! maybe_spawn {
($fut:expr) => {{
#[cfg(not(target_arch = "wasm32"))]
{
tokio::spawn($fut)
}
#[cfg(target_arch = "wasm32")]
{
tokio::task::spawn_local($fut)
}
}};
}