From 42043bda6f38812f815204e3fdaca422bcb599ef Mon Sep 17 00:00:00 2001 From: extrawurst Date: Sun, 3 Sep 2023 18:34:09 +0200 Subject: [PATCH] make update_progress simpler --- Makefile | 1 + asyncgit/src/asyncjob/mod.rs | 21 +++++++++++++++------ asyncgit/src/filter_commits.rs | 29 ++++++++++++++--------------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index f93c5df1ee..c25c5e6e1d 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/asyncgit/src/asyncjob/mod.rs b/asyncgit/src/asyncjob/mod.rs index d17d293280..d92be0de67 100644 --- a/asyncgit/src/asyncjob/mod.rs +++ b/asyncgit/src/asyncjob/mod.rs @@ -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 { +pub struct RunParams< + T: Copy + Send, + P: Clone + Send + Sync + PartialEq, +> { sender: Sender, progress: Arc>, } -impl RunParams { +impl + RunParams +{ /// 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 RunParams { } /// 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 { + 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 diff --git a/asyncgit/src/filter_commits.rs b/asyncgit/src/filter_commits.rs index 0c28635d14..3f5799b095 100644 --- a/asyncgit/src/filter_commits.rs +++ b/asyncgit/src/filter_commits.rs @@ -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, 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}"); + } + } + _ => (), } } }