Last active
December 9, 2021 14:06
-
-
Save toksdotdev/9becb63e9668bd785295148e40149773 to your computer and use it in GitHub Desktop.
Run an async block of code synchronously in the current thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::future::Future; | |
use actix_rt::Runtime; | |
/// Runs the provided future, blocking the current thread until the future completes. | |
/// | |
/// A good use case for this would be initialising | |
pub fn run_sync<F>(future: F) -> F::Output | |
where | |
F: Future, | |
{ | |
Runtime::new().unwrap().block_on(future) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment