make update_progress simpler

This commit is contained in:
extrawurst 2023-09-03 18:34:09 +02:00
parent 52dfefe624
commit 42043bda6f
3 changed files with 30 additions and 21 deletions

View File

@ -2,6 +2,7 @@
.PHONY: debug build-release release-linux-musl test clippy clippy-pedantic install install-debug
ARGS=-l
# ARGS=-l -d ~/code/extern/kubernetes
# ARGS=-l -d ~/code/extern/linux
# ARGS=-l -d ~/code/git-bare-test.git -w ~/code/git-bare-test

View File

@ -7,12 +7,17 @@ use crossbeam_channel::Sender;
use std::sync::{Arc, Mutex, RwLock};
/// Passed to `AsyncJob::run` allowing sending intermediate progress notifications
pub struct RunParams<T: Copy + Send, P: Clone + Send + Sync> {
pub struct RunParams<
T: Copy + Send,
P: Clone + Send + Sync + PartialEq,
> {
sender: Sender<T>,
progress: Arc<RwLock<P>>,
}
impl<T: Copy + Send, P: Clone + Send + Sync> RunParams<T, P> {
impl<T: Copy + Send, P: Clone + Send + Sync + PartialEq>
RunParams<T, P>
{
/// send an intermediate update notification.
/// do not confuse this with the return value of `run`.
/// `send` should only be used about progress notifications
@ -24,9 +29,13 @@ impl<T: Copy + Send, P: Clone + Send + Sync> RunParams<T, P> {
}
/// set the current progress
pub fn set_progress(&self, p: P) -> Result<()> {
*(self.progress.write()?) = p;
Ok(())
pub fn set_progress(&self, p: P) -> Result<bool> {
Ok(if *self.progress.read()? == p {
false
} else {
*(self.progress.write()?) = p;
true
})
}
}
@ -35,7 +44,7 @@ pub trait AsyncJob: Send + Sync + Clone {
/// defines what notification type is used to communicate outside
type Notification: Copy + Send;
/// type of progress
type Progress: Clone + Default + Send + Sync;
type Progress: Clone + Default + Send + Sync + PartialEq;
/// can run a synchronous time intensive task.
/// the returned notification is used to tell interested parties

View File

@ -88,19 +88,14 @@ impl AsyncCommitFilterJob {
let total_amount = commits.len();
let start = Instant::now();
let mut progress = ProgressPercent::new(0, total_amount);
let result = commits
.into_iter()
.enumerate()
.filter_map(|(idx, c)| {
let new_progress =
ProgressPercent::new(idx, total_amount);
if new_progress != progress {
Self::update_progress(params, new_progress);
progress = new_progress;
}
Self::update_progress(
params,
ProgressPercent::new(idx, total_amount),
);
(*self.filter)(repo, &c)
.ok()
@ -115,12 +110,16 @@ impl AsyncCommitFilterJob {
params: &RunParams<AsyncGitNotification, ProgressPercent>,
new_progress: ProgressPercent,
) {
if let Err(e) = params.set_progress(new_progress) {
log::error!("progress error: {e}");
} else if let Err(e) =
params.send(AsyncGitNotification::CommitFilter)
{
log::error!("send error: {e}");
match params.set_progress(new_progress) {
Err(e) => log::error!("progress error: {e}"),
Ok(result) if result => {
if let Err(e) =
params.send(AsyncGitNotification::CommitFilter)
{
log::error!("send error: {e}");
}
}
_ => (),
}
}
}