Skip to content

Instantly share code, notes, and snippets.

@toksdotdev
Last active December 9, 2021 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toksdotdev/9becb63e9668bd785295148e40149773 to your computer and use it in GitHub Desktop.
Save toksdotdev/9becb63e9668bd785295148e40149773 to your computer and use it in GitHub Desktop.
Run an async block of code synchronously in the current thread
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