Refactor to reduce compilation time (#1284)

- Make `binstalk::bins` private.
 - Move mod `signal` into `crates/bin`
 - Make items in `crates/bin/src/lib.rs` private if possible to reduce
   its API generation time.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-08-13 00:25:41 +10:00 committed by GitHub
parent fbed317df5
commit 2375ba48b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 45 deletions

View file

@ -37,7 +37,7 @@ strum = "0.25.0"
target-lexicon = { version = "0.12.11", features = ["std"] }
tempfile = "3.5.0"
thiserror = "1.0.40"
tokio = { version = "1.30.0", features = ["rt", "process", "sync", "signal"], default-features = false }
tokio = { version = "1.30.0", features = ["rt", "process", "sync"], default-features = false }
tracing = "0.1.37"
url = { version = "2.3.1", features = ["serde"] }
xz2 = "0.1.7"

View file

@ -1,7 +1,6 @@
pub(crate) mod futures_resolver;
pub mod jobserver_client;
pub mod remote;
pub mod signal;
pub(crate) mod target_triple;
pub mod tasks;

View file

@ -1,86 +0,0 @@
use std::io;
use super::tasks::AutoAbortJoinHandle;
use crate::errors::BinstallError;
use tokio::signal;
/// This function will poll the handle while listening for ctrl_c,
/// `SIGINT`, `SIGHUP`, `SIGTERM` and `SIGQUIT`.
///
/// When signal is received, [`BinstallError::UserAbort`] will be returned.
///
/// It would also ignore `SIGUSER1` and `SIGUSER2` on unix.
///
/// This function uses [`tokio::signal`] and once exit, does not reset the default
/// signal handler, so be careful when using it.
pub async fn cancel_on_user_sig_term<T>(
handle: AutoAbortJoinHandle<T>,
) -> Result<T, BinstallError> {
ignore_signals()?;
tokio::select! {
biased;
res = wait_on_cancellation_signal() => {
res.map_err(BinstallError::Io)
.and(Err(BinstallError::UserAbort))
}
res = handle => res,
}
}
fn ignore_signals() -> io::Result<()> {
#[cfg(unix)]
unix::ignore_signals_on_unix()?;
Ok(())
}
/// If call to it returns `Ok(())`, then all calls to this function after
/// that also returns `Ok(())`.
async fn wait_on_cancellation_signal() -> Result<(), io::Error> {
#[cfg(unix)]
unix::wait_on_cancellation_signal_unix().await?;
#[cfg(not(unix))]
signal::ctrl_c().await?;
Ok(())
}
#[cfg(unix)]
mod unix {
use super::*;
use signal::unix::{signal, SignalKind};
/// Same as [`wait_on_cancellation_signal`] but is only available on unix.
pub async fn wait_on_cancellation_signal_unix() -> Result<(), io::Error> {
tokio::select! {
biased;
res = wait_for_signal_unix(SignalKind::interrupt()) => res,
res = wait_for_signal_unix(SignalKind::hangup()) => res,
res = wait_for_signal_unix(SignalKind::terminate()) => res,
res = wait_for_signal_unix(SignalKind::quit()) => res,
}
}
/// Wait for first arrival of signal.
pub async fn wait_for_signal_unix(kind: signal::unix::SignalKind) -> Result<(), io::Error> {
let mut sig_listener = signal::unix::signal(kind)?;
if sig_listener.recv().await.is_some() {
Ok(())
} else {
// Use pending() here for the same reason as above.
std::future::pending().await
}
}
pub fn ignore_signals_on_unix() -> Result<(), io::Error> {
drop(signal(SignalKind::user_defined1())?);
drop(signal(SignalKind::user_defined2())?);
Ok(())
}
}

View file

@ -1,6 +1,6 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
pub mod bins;
mod bins;
pub mod drivers;
pub mod errors;
pub mod fetchers;