#[cfg(native_winhttp)]
#[derive(Debug)]
pub struct Runtime;
#[cfg(native_winhttp)]
impl Runtime {
pub fn block_on<F: std::future::Future<Output = T>, T>(&self, f: F) -> T {
futures_executor::block_on(f)
}
}
#[cfg(not(native_winhttp))]
pub type Runtime = tokio::runtime::Runtime;
#[cfg(native_winhttp)]
pub fn runtime() -> std::io::Result<Runtime> {
Ok(Runtime)
}
#[cfg(not(native_winhttp))]
pub fn runtime() -> std::io::Result<Runtime> {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
}
pub fn block_on<F: std::future::Future<Output = T>, T>(f: F) -> std::io::Result<T> {
let rt = runtime()?;
Ok(rt.block_on(f))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn runtime_lifecycle() {
let rt = runtime().expect("runtime() should succeed");
let s = format!("{rt:?}");
assert!(!s.is_empty(), "Debug output should not be empty");
let val = rt.block_on(async { 7 + 3 });
assert_eq!(val, 10);
let a = rt.block_on(async { 1 });
let b = rt.block_on(async { 2 });
let c = rt.block_on(async { 3 });
assert_eq!(a + b + c, 6);
let val = block_on(async { 42 }).expect("runtime creation should succeed");
assert_eq!(val, 42);
}
}