wmill/
maybe_future.rs

1// #[cfg(feature = "async")]
2// use futures::future::BoxFuture;
3
4/// A conditional type that represents either:
5/// - A boxed future (async mode)
6/// - A direct value (sync mode)
7pub mod maybe_future {
8    #[cfg(feature = "async")]
9    pub type MaybeFuture<'a, T> = futures::future::BoxFuture<'a, T>;
10    #[cfg(not(feature = "async"))]
11    pub type MaybeFuture<'a, T> = T;
12}
13
14/// Bridges between async and sync execution contexts
15///
16/// Behavior depends on compilation:
17/// - With `async` feature: Returns a boxed future
18/// - Without `async` feature: Blocks on the global runtime
19#[macro_export]
20macro_rules! ret {
21    ($ex:expr) => {
22        #[cfg(feature = "async")]
23        return async move { $ex.await }.boxed();
24
25        #[cfg(not(feature = "async"))]
26        return crate::maybe_future::RUNTIME.block_on($ex);
27    };
28}
29
30/// The global Tokio runtime instance used in sync mode
31///
32/// Initialized lazily with:
33/// - I/O capabilities
34/// - Time utilities
35/// - Current-thread executor
36///
37/// # Panics
38///
39/// If Tokio fails to initialize the runtime
40#[cfg(not(feature = "async"))]
41pub(crate) static RUNTIME: once_cell::sync::Lazy<tokio::runtime::Runtime> =
42    once_cell::sync::Lazy::new(|| {
43        tokio::runtime::Builder::new_current_thread()
44            .enable_io()
45            .enable_time()
46            .build()
47            .unwrap()
48    });