hopr_async_runtime/
lib.rs

1//! Executor API for HOPR which exposes the necessary async functions depending on the enabled
2//! runtime.
3
4pub use futures::future::AbortHandle;
5
6// Both features could be enabled during testing; therefore, we only use tokio when it's
7// exclusively enabled.
8#[cfg(feature = "runtime-tokio")]
9pub mod prelude {
10    pub use futures::future::{AbortHandle, abortable};
11    pub use tokio::{
12        task::{JoinHandle, spawn, spawn_blocking, spawn_local},
13        time::{sleep, timeout as timeout_fut},
14    };
15}
16
17#[macro_export]
18macro_rules! spawn_as_abortable {
19    ($($expr:expr),*) => {{
20        let (proc, abort_handle) = $crate::prelude::abortable($($expr),*);
21        let _jh = $crate::prelude::spawn(proc);
22        abort_handle
23    }}
24}
25
26// If no runtime is enabled, fail compilation
27#[cfg(not(feature = "runtime-tokio"))]
28compile_error!("No runtime enabled");