loadstar/src/utilities/async_block.rs
jank 69d71d6588
All checks were successful
Cargo Build & Test / Tests (pull_request) Successful in 14s
Cargo Build & Test / check-cargo-version (pull_request) Successful in 6s
Cargo Build & Test / Test publish (pull_request) Successful in 22s
Add ability to run async functions in blocking mode
2024-08-21 15:55:25 +02:00

12 lines
446 B
Rust

use std::future::Future;
pub fn run_async<T>(async_operation: impl Future<Output = T>) -> T {
// Use `block_in_place` to ensure runtime is dropped in a blocking context
tokio::task::block_in_place(|| {
// Get the current Tokio runtime handle
let handle = tokio::runtime::Handle::current();
// Run the async operation using the handle and wait for it to complete
handle.block_on(async_operation)
})
}