From c67c59b3ca577e0da02b9f38dce852eefca01eff Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Mon, 18 Jul 2022 21:04:36 +1000 Subject: [PATCH] Impl new fn `helpers::create_jobserver_client` Signed-off-by: Jiahao XU --- src/helpers.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/helpers.rs b/src/helpers.rs index 2732f1e7..c442e478 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,7 +1,9 @@ use std::fmt::Debug; use std::fs; use std::io; +use std::num::NonZeroUsize; use std::path::{Path, PathBuf}; +use std::thread::available_parallelism; use bytes::Bytes; use cargo_toml::Manifest; @@ -42,6 +44,20 @@ pub async fn await_task(task: tokio::task::JoinHandle) -> miette::Result Result { + use jobserver::Client; + + // Safety: + // + // Client::from_env is unsafe because from_raw_fd is unsafe. + if let Some(client) = unsafe { Client::from_env() } { + Ok(client) + } else { + let ncore = available_parallelism().map(NonZeroUsize::get).unwrap_or(1); + Ok(Client::new(ncore)?) + } +} + /// Load binstall metadata from the crate `Cargo.toml` at the provided path pub fn load_manifest_path>( manifest_path: P,