Rename lib to binstalk (#361)

This commit is contained in:
Félix Saparelli 2022-09-10 18:44:18 +12:00 committed by GitHub
parent a94d83f0d5
commit e25aa50ec9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
49 changed files with 25 additions and 25 deletions

View file

@ -0,0 +1,33 @@
use std::{num::NonZeroUsize, sync::Arc, thread::available_parallelism};
use jobslot::Client;
use tokio::sync::OnceCell;
use crate::errors::BinstallError;
#[derive(Clone)]
pub struct LazyJobserverClient(Arc<OnceCell<Client>>);
impl LazyJobserverClient {
/// This must be called at the start of the program since
/// `Client::from_env` requires that.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
// Safety:
//
// Client::from_env is unsafe because from_raw_fd is unsafe.
// It doesn't do anything that is actually unsafe, like
// dereferencing pointer.
let opt = unsafe { Client::from_env() };
Self(Arc::new(OnceCell::new_with(opt)))
}
pub async fn get(&self) -> Result<&Client, BinstallError> {
self.0
.get_or_try_init(|| async {
let ncore = available_parallelism().map(NonZeroUsize::get).unwrap_or(1);
Ok(Client::new(ncore)?)
})
.await
}
}