use std::future::Future;
use std::sync::OnceLock;
use asupersync::runtime::Runtime;
use asupersync::runtime::RuntimeBuilder;
use asupersync::runtime::reactor::create_reactor;
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
pub fn block_on<F: Future>(future: F) -> F::Output {
let runtime = RUNTIME.get_or_init(|| {
let reactor = create_reactor().expect("failed to create platform I/O reactor");
RuntimeBuilder::current_thread()
.with_reactor(reactor)
.build()
.expect("failed to build asupersync runtime")
});
runtime.block_on(future)
}
#[cfg(test)]
mod tests {
use super::block_on;
#[test]
fn block_on_runs_async_blocks() {
let out = block_on(async { 1 + 1 });
assert_eq!(out, 2);
}
#[test]
fn block_on_can_be_called_multiple_times() {
let a = block_on(async { "a" });
let b = block_on(async { "b" });
assert_eq!(a, "a");
assert_eq!(b, "b");
}
}