Refactor: Extract new mod jobserver_client.rs

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-20 17:37:02 +10:00
parent 2490cd5a84
commit a1d7a7c117
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
2 changed files with 23 additions and 18 deletions

View file

@ -1,9 +1,7 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
use std::io; use std::io;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::thread::available_parallelism;
use bytes::Bytes; use bytes::Bytes;
use cargo_toml::Manifest; use cargo_toml::Manifest;
@ -30,6 +28,9 @@ pub use ui_thread::UIThread;
mod extracter; mod extracter;
mod stream_readable; mod stream_readable;
mod jobserver_client;
pub use jobserver_client::*;
mod path_ext; mod path_ext;
pub use path_ext::*; pub use path_ext::*;
@ -46,22 +47,6 @@ pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) ->
} }
} }
pub fn create_jobserver_client() -> Result<jobserver::Client, BinstallError> {
use jobserver::Client;
// Safety:
//
// Client::from_env is unsafe because from_raw_fd is unsafe.
// It doesn't do anything that is actually unsafe, like
// dereferencing pointer.
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 /// Load binstall metadata from the crate `Cargo.toml` at the provided path
pub fn load_manifest_path<P: AsRef<Path>>( pub fn load_manifest_path<P: AsRef<Path>>(
manifest_path: P, manifest_path: P,

View file

@ -0,0 +1,20 @@
use std::num::NonZeroUsize;
use std::thread::available_parallelism;
use crate::BinstallError;
pub fn create_jobserver_client() -> Result<jobserver::Client, BinstallError> {
use jobserver::Client;
// Safety:
//
// Client::from_env is unsafe because from_raw_fd is unsafe.
// It doesn't do anything that is actually unsafe, like
// dereferencing pointer.
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)?)
}
}