hopr_async_runtime/
lib.rs

1//! Executor API for HOPR which exposes the necessary async functions depending on the enabled
2//! runtime.
3//!
4//!
5#[cfg(feature = "runtime-async-std")]
6pub mod prelude {
7    pub use async_std::future::timeout as timeout_fut;
8    pub use async_std::task::{sleep, spawn, spawn_blocking, spawn_local, JoinHandle};
9
10    pub async fn cancel_join_handle<T>(handle: JoinHandle<T>) {
11        handle.cancel().await;
12    }
13}
14
15// Both features could be enabled during testing, therefore we only use tokio when its
16// exclusively enabled.
17#[cfg(all(feature = "runtime-tokio", not(feature = "runtime-async-std")))]
18pub mod prelude {
19    pub use tokio::time::timeout as timeout_fut;
20    pub use tokio::{
21        task::{spawn, spawn_blocking, spawn_local, JoinHandle},
22        time::sleep,
23    };
24
25    pub async fn cancel_join_handle<T>(handle: JoinHandle<T>) {
26        handle.abort()
27    }
28}
29
30// If no runtime is enabled, fail compilation
31#[cfg(all(not(feature = "runtime-tokio"), not(feature = "runtime-async-std")))]
32compile_error!("No runtime enabled");