From b6245bcf4be9e78fe04a784667faa679961fff6e Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 7 Jun 2022 16:13:38 +1000 Subject: [PATCH 1/3] Spawn `entry()` in `main` to improve parallelism Using `rt.block_on`, the future returned by `entry` can only be run on the main thread. Buf if we use `tokio::spawn`, then it can be run on any thread. Signed-off-by: Jiahao XU --- src/main.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c913dc5..87f63063 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use miette::{miette, IntoDiagnostic, Result, WrapErr}; use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode}; use structopt::StructOpt; use tempfile::TempDir; -use tokio::{process::Command, runtime::Runtime}; +use tokio::{process::Command, runtime::Runtime, task::JoinError}; use cargo_binstall::{ bins, @@ -84,6 +84,7 @@ enum MainExit { Success(Duration), Error(BinstallError), Report(miette::Report), + JoinErr(JoinError), } impl Termination for MainExit { @@ -99,6 +100,11 @@ impl Termination for MainExit { eprintln!("{err:?}"); ExitCode::from(16) } + Self::JoinErr(err) => { + error!("Fatal error:"); + eprintln!("{err:?}"); + ExitCode::from(16) + } } } } @@ -107,19 +113,22 @@ fn main() -> MainExit { let start = Instant::now(); let rt = Runtime::new().unwrap(); - let result = rt.block_on(entry()); + let handle = rt.spawn(entry()); + let result = rt.block_on(handle); drop(rt); let done = start.elapsed(); debug!("run time: {done:?}"); result - .map(|_| MainExit::Success(done)) - .unwrap_or_else(|err| { - err.downcast::() - .map(MainExit::Error) - .unwrap_or_else(MainExit::Report) + .map(|res| { + res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| { + err.downcast::() + .map(MainExit::Error) + .unwrap_or_else(MainExit::Report) + }) }) + .unwrap_or_else(MainExit::JoinErr) } async fn entry() -> Result<()> { From 903c9f559171ecd5fe07f36a59da325c2a270066 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 7 Jun 2022 16:19:07 +1000 Subject: [PATCH 2/3] Refactor: Use `Result::map_or_else` in `main` Signed-off-by: Jiahao XU --- src/main.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 87f63063..5d578545 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,15 +120,13 @@ fn main() -> MainExit { let done = start.elapsed(); debug!("run time: {done:?}"); - result - .map(|res| { - res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| { - err.downcast::() - .map(MainExit::Error) - .unwrap_or_else(MainExit::Report) - }) + result.map_or_else(MainExit::JoinErr, |res| { + res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| { + err.downcast::() + .map(MainExit::Error) + .unwrap_or_else(MainExit::Report) }) - .unwrap_or_else(MainExit::JoinErr) + }) } async fn entry() -> Result<()> { From 456e8964833f31bd62a15cd3e98afb6d5e8cc2b9 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 7 Jun 2022 16:22:01 +1000 Subject: [PATCH 3/3] Use code 17 for `MainExit::JoinErr` Signed-off-by: Jiahao XU --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 5d578545..0a6a10e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,7 +103,7 @@ impl Termination for MainExit { Self::JoinErr(err) => { error!("Fatal error:"); eprintln!("{err:?}"); - ExitCode::from(16) + ExitCode::from(17) } } }