headless_lms_utils/futures.rs
1use futures::{Future, FutureExt};
2
3/// For use with join! or try_join! Makes the future to run parallelly with other tasks instead of concurrently. See: <https://docs.rs/tokio/latest/tokio/macro.try_join.html#runtime-characteristics>.
4pub async fn run_parallelly<T>(
5 future: impl Future<Output = anyhow::Result<T>> + std::marker::Send + 'static,
6) -> anyhow::Result<T>
7where
8 T: std::marker::Send + 'static,
9{
10 // boxing our futures helps avoid stack overflow
11 let handle = tokio::spawn(future.boxed());
12 match handle.await {
13 Ok(Ok(result)) => Ok(result),
14 Ok(Err(err)) => Err(err),
15 Err(err) => anyhow::bail!("{}", err),
16 }
17}