Impl new fn helpers::create_jobserver_client

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-18 21:04:36 +10:00
parent 8cc085b1b6
commit c67c59b3ca
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -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<T>(task: tokio::task::JoinHandle<T>) -> miette::Result<T
.map_err(|join_err| miette::miette!("Task failed to join: {}", join_err))
}
pub fn create_jobserver_client() -> Result<jobserver::Client, BinstallError> {
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<P: AsRef<Path>>(
manifest_path: P,