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;
5use futures::future::abortable;
6
7#[cfg(feature = "runtime-async-std")]
8#[deprecated(note = "Use `runtime-tokio` feature, the `async-std` crate is deprecated")]
9pub mod prelude {
10    pub use async_std::{
11        future::timeout as timeout_fut,
12        task::{JoinHandle, sleep, spawn, spawn_blocking, spawn_local},
13    };
14}
15
16// Both features could be enabled during testing; therefore, we only use tokio when it's
17// exclusively enabled.
18#[cfg(feature = "runtime-tokio")]
19pub mod prelude {
20    pub use tokio::{
21        task::{JoinHandle, spawn, spawn_blocking, spawn_local},
22        time::{sleep, timeout as timeout_fut},
23    };
24}
25
26pub fn spawn_as_abortable<F, T>(f: F) -> AbortHandle
27where
28    F: std::future::Future<Output = T> + Send + 'static,
29    T: Send + 'static,
30{
31    let (proc, abort_handle) = abortable(f);
32    let _jh = prelude::spawn(proc);
33    abort_handle
34}
35
36// If no runtime is enabled, fail compilation
37#[cfg(all(not(feature = "runtime-tokio"), not(feature = "runtime-async-std")))]
38compile_error!("No runtime enabled");