Skip to main content

calimero_utils_actix/
lib.rs

1use std::sync::OnceLock;
2
3use eyre::bail;
4use tokio::runtime::{Handle, RuntimeFlavor};
5
6pub mod adapters;
7pub mod lazy;
8#[doc(hidden)]
9pub mod macros;
10
11pub use lazy::{LazyAddr, LazyRecipient};
12
13static GLOBAL_RUNTIME: OnceLock<Handle> = OnceLock::new();
14
15pub fn init_global_runtime() -> eyre::Result<()> {
16    let handle = Handle::current();
17
18    if handle.runtime_flavor() == RuntimeFlavor::CurrentThread {
19        bail!("global runtime must not be a current-thread runtime");
20    }
21
22    if GLOBAL_RUNTIME.set(handle).is_err() {
23        bail!("global runtime already initialized");
24    }
25
26    Ok(())
27}
28
29pub fn global_runtime() -> &'static Handle {
30    GLOBAL_RUNTIME
31        .get()
32        .expect("global runtime not initialized")
33}