Skip to content

Commit 42043bd

Browse files
committed
make update_progress simpler
1 parent 52dfefe commit 42043bd

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.PHONY: debug build-release release-linux-musl test clippy clippy-pedantic install install-debug
33

44
ARGS=-l
5+
# ARGS=-l -d ~/code/extern/kubernetes
56
# ARGS=-l -d ~/code/extern/linux
67
# ARGS=-l -d ~/code/git-bare-test.git -w ~/code/git-bare-test
78

asyncgit/src/asyncjob/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ use crossbeam_channel::Sender;
77
use std::sync::{Arc, Mutex, RwLock};
88

99
/// Passed to `AsyncJob::run` allowing sending intermediate progress notifications
10-
pub struct RunParams<T: Copy + Send, P: Clone + Send + Sync> {
10+
pub struct RunParams<
11+
T: Copy + Send,
12+
P: Clone + Send + Sync + PartialEq,
13+
> {
1114
sender: Sender<T>,
1215
progress: Arc<RwLock<P>>,
1316
}
1417

15-
impl<T: Copy + Send, P: Clone + Send + Sync> RunParams<T, P> {
18+
impl<T: Copy + Send, P: Clone + Send + Sync + PartialEq>
19+
RunParams<T, P>
20+
{
1621
/// send an intermediate update notification.
1722
/// do not confuse this with the return value of `run`.
1823
/// `send` should only be used about progress notifications
@@ -24,9 +29,13 @@ impl<T: Copy + Send, P: Clone + Send + Sync> RunParams<T, P> {
2429
}
2530

2631
/// set the current progress
27-
pub fn set_progress(&self, p: P) -> Result<()> {
28-
*(self.progress.write()?) = p;
29-
Ok(())
32+
pub fn set_progress(&self, p: P) -> Result<bool> {
33+
Ok(if *self.progress.read()? == p {
34+
false
35+
} else {
36+
*(self.progress.write()?) = p;
37+
true
38+
})
3039
}
3140
}
3241

@@ -35,7 +44,7 @@ pub trait AsyncJob: Send + Sync + Clone {
3544
/// defines what notification type is used to communicate outside
3645
type Notification: Copy + Send;
3746
/// type of progress
38-
type Progress: Clone + Default + Send + Sync;
47+
type Progress: Clone + Default + Send + Sync + PartialEq;
3948

4049
/// can run a synchronous time intensive task.
4150
/// the returned notification is used to tell interested parties

asyncgit/src/filter_commits.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,14 @@ impl AsyncCommitFilterJob {
8888
let total_amount = commits.len();
8989
let start = Instant::now();
9090

91-
let mut progress = ProgressPercent::new(0, total_amount);
92-
9391
let result = commits
9492
.into_iter()
9593
.enumerate()
9694
.filter_map(|(idx, c)| {
97-
let new_progress =
98-
ProgressPercent::new(idx, total_amount);
99-
100-
if new_progress != progress {
101-
Self::update_progress(params, new_progress);
102-
progress = new_progress;
103-
}
95+
Self::update_progress(
96+
params,
97+
ProgressPercent::new(idx, total_amount),
98+
);
10499

105100
(*self.filter)(repo, &c)
106101
.ok()
@@ -115,12 +110,16 @@ impl AsyncCommitFilterJob {
115110
params: &RunParams<AsyncGitNotification, ProgressPercent>,
116111
new_progress: ProgressPercent,
117112
) {
118-
if let Err(e) = params.set_progress(new_progress) {
119-
log::error!("progress error: {e}");
120-
} else if let Err(e) =
121-
params.send(AsyncGitNotification::CommitFilter)
122-
{
123-
log::error!("send error: {e}");
113+
match params.set_progress(new_progress) {
114+
Err(e) => log::error!("progress error: {e}"),
115+
Ok(result) if result => {
116+
if let Err(e) =
117+
params.send(AsyncGitNotification::CommitFilter)
118+
{
119+
log::error!("send error: {e}");
120+
}
121+
}
122+
_ => (),
124123
}
125124
}
126125
}

0 commit comments

Comments
 (0)