fedimint_core/
runtime.rs

1//! Abstraction over an executor so we can spawn tasks under WASM the same way
2//! we do usually.
3
4use std::future::Future;
5
6use fedimint_logging::LOG_RUNTIME;
7pub use n0_future::task::{JoinError, JoinHandle};
8pub use n0_future::time::{Duration, Elapsed, Instant, sleep, sleep_until, timeout};
9use tracing::Instrument;
10
11use crate::task::MaybeSend;
12
13pub fn spawn<F, T>(name: &str, future: F) -> JoinHandle<T>
14where
15    F: Future<Output = T> + 'static + MaybeSend,
16    T: MaybeSend + 'static,
17{
18    let span = tracing::debug_span!(target: LOG_RUNTIME, parent: None, "spawn", task = name);
19    n0_future::task::spawn(future.instrument(span))
20}
21
22// Note: These functions only exist on non-wasm platforms and you need to handle
23// them conditionally at the call site of packages that compile on wasm
24#[cfg(not(target_family = "wasm"))]
25pub fn block_in_place<F, R>(f: F) -> R
26where
27    F: FnOnce() -> R,
28{
29    // nosemgrep: ban-raw-block-in-place
30    tokio::task::block_in_place(f)
31}
32
33#[cfg(not(target_family = "wasm"))]
34pub fn block_on<F: Future>(future: F) -> F::Output {
35    // nosemgrep: ban-raw-block-on
36    tokio::runtime::Handle::current().block_on(future)
37}