Skip to content

Commit 5c1bd03

Browse files
CopilotByron
andcommitted
feat!: Use std::ops::ControlFlow where possible
Co-authored-by: Byron <63622+Byron@users.noreply.github.com>
1 parent f1b3906 commit 5c1bd03

File tree

46 files changed

+194
-252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+194
-252
lines changed

gitoxide-core/src/hours/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub fn spawn_tree_delta_threads<'scope>(
182182
}
183183
},
184184
}
185-
Ok::<_, std::io::Error>(Default::default())
185+
Ok::<_, std::io::Error>(std::ops::ControlFlow::Continue(()))
186186
})?;
187187
out.push((commit_idx, files, lines));
188188
}

gitoxide-core/src/query/engine/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ pub fn update(
318318
});
319319
}
320320
}
321-
Ok::<_, Infallible>(Default::default())
321+
Ok::<_, Infallible>(std::ops::ControlFlow::Continue(()))
322322
})?;
323323
out_chunk.push(CommitDiffStats {
324324
id: commit,

gitoxide-core/src/repository/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ pub(crate) mod function {
391391
fn emit(&mut self, entry: EntryRef<'_>, collapsed_directory_status: Option<Status>) -> walk::Action {
392392
let res = self.inner.emit(entry, collapsed_directory_status);
393393
if gix::interrupt::is_triggered() {
394-
return walk::Action::Cancel;
394+
return std::ops::ControlFlow::Break(());
395395
}
396396
res
397397
}

gitoxide-core/src/repository/tree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ mod entries {
9393

9494
fn visit_tree(&mut self, _entry: &EntryRef<'_>) -> Action {
9595
self.stats.num_trees += 1;
96-
Action::Continue
96+
std::ops::ControlFlow::Continue(true)
9797
}
9898

9999
fn visit_nontree(&mut self, entry: &EntryRef<'_>) -> Action {
@@ -115,7 +115,7 @@ mod entries {
115115
Link => self.stats.num_links += 1,
116116
Tree => unreachable!("BUG"),
117117
}
118-
Action::Continue
118+
std::ops::ControlFlow::Continue(true)
119119
}
120120
}
121121
}

gix-blame/src/file/function.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ fn tree_diff_without_rewrites_at_file_path(
649649
}
650650

651651
fn visit(&mut self, change: gix_diff::tree::visit::Change) -> gix_diff::tree::visit::Action {
652-
use gix_diff::tree::{visit, visit::Change::*};
652+
use gix_diff::tree::visit::Change::*;
653653

654654
if self.inner.path() == self.interesting_path {
655655
self.change = Some(match change {
@@ -687,9 +687,9 @@ fn tree_diff_without_rewrites_at_file_path(
687687
},
688688
});
689689

690-
visit::Action::Cancel
690+
std::ops::ControlFlow::Break(())
691691
} else {
692-
visit::Action::Continue
692+
std::ops::ControlFlow::Continue(())
693693
}
694694
}
695695
}
@@ -730,9 +730,9 @@ fn tree_diff_with_rewrites_at_file_path(
730730
|change_ref| -> Result<_, std::convert::Infallible> {
731731
if change_ref.location() == file_path {
732732
change = Some(change_ref.into_owned());
733-
Ok(gix_diff::tree_with_rewrites::Action::Cancel)
733+
Ok(std::ops::ControlFlow::Break(()))
734734
} else {
735-
Ok(gix_diff::tree_with_rewrites::Action::Continue)
735+
Ok(std::ops::ControlFlow::Continue(()))
736736
}
737737
},
738738
options,

gix-diff/src/index/function.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ where
9595
let (rhs_idx, rhs_path, rhs_entry) = rhs;
9696
match lhs_path.cmp(rhs_path) {
9797
Ordering::Less => match emit_deletion(lhs, &mut cb, tracker.as_mut())? {
98-
Action::Continue => {
98+
std::ops::ControlFlow::Continue(()) => {
9999
lhs_storage = lhs_iter.next();
100100
}
101-
Action::Cancel => return Ok(None),
101+
std::ops::ControlFlow::Break(()) => return Ok(None),
102102
},
103103
Ordering::Equal => {
104104
if ignore_unmerged_and_intent_to_add(rhs) {
@@ -123,31 +123,31 @@ where
123123
};
124124
if let Some(change) = change {
125125
match cb(change).map_err(|err| Error::Callback(err.into()))? {
126-
Action::Continue => {}
127-
Action::Cancel => return Ok(None),
126+
std::ops::ControlFlow::Continue(()) => {}
127+
std::ops::ControlFlow::Break(()) => return Ok(None),
128128
}
129129
}
130130
}
131131
lhs_storage = lhs_iter.next();
132132
rhs_storage = rhs_iter.next();
133133
}
134134
Ordering::Greater => match emit_addition(rhs, &mut cb, tracker.as_mut())? {
135-
Action::Continue => {
135+
std::ops::ControlFlow::Continue(()) => {
136136
rhs_storage = rhs_iter.next();
137137
}
138-
Action::Cancel => return Ok(None),
138+
std::ops::ControlFlow::Break(()) => return Ok(None),
139139
},
140140
}
141141
}
142142
(Some(lhs), None) => match emit_deletion(lhs, &mut cb, tracker.as_mut())? {
143-
Action::Cancel => return Ok(None),
144-
Action::Continue => {
143+
std::ops::ControlFlow::Break(()) => return Ok(None),
144+
std::ops::ControlFlow::Continue(()) => {
145145
lhs_storage = lhs_iter.next();
146146
}
147147
},
148148
(None, Some(rhs)) => match emit_addition(rhs, &mut cb, tracker.as_mut())? {
149-
Action::Cancel => return Ok(None),
150-
Action::Continue => {
149+
std::ops::ControlFlow::Break(()) => return Ok(None),
150+
std::ops::ControlFlow::Continue(()) => {
151151
rhs_storage = rhs_iter.next();
152152
}
153153
},
@@ -180,11 +180,11 @@ where
180180
dst.change
181181
};
182182
match cb(change) {
183-
Ok(Action::Continue) => crate::tree::visit::Action::Continue,
184-
Ok(Action::Cancel) => crate::tree::visit::Action::Cancel,
183+
Ok(std::ops::ControlFlow::Continue(())) => std::ops::ControlFlow::Continue(()),
184+
Ok(std::ops::ControlFlow::Break(())) => std::ops::ControlFlow::Break(()),
185185
Err(err) => {
186186
cb_err = Some(Error::Callback(err.into()));
187-
crate::tree::visit::Action::Cancel
187+
std::ops::ControlFlow::Break(())
188188
}
189189
}
190190
},
@@ -239,7 +239,7 @@ where
239239
None => change,
240240
Some(tracker) => match tracker.try_push_change(change, path) {
241241
Some(change) => change,
242-
None => return Ok(Action::Continue),
242+
None => return Ok(std::ops::ControlFlow::Continue(())),
243243
},
244244
};
245245

@@ -255,7 +255,7 @@ where
255255
E: Into<Box<dyn std::error::Error + Send + Sync>>,
256256
{
257257
if ignore_unmerged_and_intent_to_add((idx, path, entry)) {
258-
return Ok(Action::Continue);
258+
return Ok(std::ops::ControlFlow::Continue(()));
259259
}
260260

261261
let change = ChangeRef::Addition {
@@ -269,7 +269,7 @@ where
269269
None => change,
270270
Some(tracker) => match tracker.try_push_change(change, path) {
271271
Some(change) => change,
272-
None => return Ok(Action::Continue),
272+
None => return Ok(std::ops::ControlFlow::Continue(())),
273273
},
274274
};
275275

gix-diff/src/index/mod.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,11 @@ pub enum Error {
1717
}
1818

1919
/// What to do after a [ChangeRef] was passed ot the callback of [`index()`](crate::index()).
20-
#[derive(Default, Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
21-
pub enum Action {
22-
/// Continue the operation.
23-
#[default]
24-
Continue,
25-
/// Stop the operation immediately.
26-
///
27-
/// This is useful if one just wants to determine if something changed or not.
28-
Cancel,
29-
}
20+
///
21+
/// Use [`std::ops::ControlFlow::Continue`] to continue the operation.
22+
/// Use [`std::ops::ControlFlow::Break`] to stop the operation immediately.
23+
/// This is useful if one just wants to determine if something changed or not.
24+
pub type Action = std::ops::ControlFlow<()>;
3025

3126
/// Options to configure how rewrites are tracked as part of the [`index()`](crate::index()) call.
3227
pub struct RewriteOptions<'a, Find>

gix-diff/src/rewrites/tracker.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ impl<T: Change> Tracker<T> {
342342
change: item.change,
343343
},
344344
None,
345-
) == Action::Cancel
345+
)
346+
.is_break()
346347
{
347348
break;
348349
}
@@ -369,7 +370,10 @@ impl<T: Change> Tracker<T> {
369370
// https://github.com/git/git/blob/cc01bad4a9f566cf4453c7edd6b433851b0835e2/diffcore-rename.c#L350-L369
370371
// We would need a hashmap to be OK to not use the limit here, otherwise the performance is too bad.
371372
// This also means we don't find all renames if we hit the rename limit.
372-
if self.match_pairs(cb, None /* by identity */, kind, out, diff_cache, objects, filter)? == Action::Cancel {
373+
if self
374+
.match_pairs(cb, None /* by identity */, kind, out, diff_cache, objects, filter)?
375+
.is_break()
376+
{
373377
return Ok(());
374378
}
375379
if needs_second_pass {
@@ -394,7 +398,7 @@ impl<T: Change> Tracker<T> {
394398
}
395399
};
396400
if !is_limited {
397-
self.match_pairs(cb, percentage, kind, out, diff_cache, objects, None)?;
401+
let _ = self.match_pairs(cb, percentage, kind, out, diff_cache, objects, None)?;
398402
}
399403
}
400404
Ok(())
@@ -478,7 +482,7 @@ impl<T: Change> Tracker<T> {
478482
gix_trace::warn!(
479483
"Cancelled rename matching as there were too many iterations ({num_checks} > {max_checks})"
480484
);
481-
return Ok(Action::Cancel);
485+
return Ok(std::ops::ControlFlow::Break(()));
482486
}
483487
let Some((src, src_idx)) = src else {
484488
continue;
@@ -496,15 +500,15 @@ impl<T: Change> Tracker<T> {
496500
self.items[dest_idx].emitted = true;
497501
self.items[src_idx].emitted = true;
498502

499-
if res == Action::Cancel {
500-
return Ok(Action::Cancel);
503+
if res.is_break() {
504+
return Ok(std::ops::ControlFlow::Break(()));
501505
}
502506

503507
match relations {
504508
Some((Relation::Parent(src), Relation::Parent(dst))) => {
505509
let res = self.emit_child_renames_matching_identity(cb, kind, src, dst)?;
506-
if res == Action::Cancel {
507-
return Ok(Action::Cancel);
510+
if res.is_break() {
511+
return Ok(std::ops::ControlFlow::Break(()));
508512
}
509513
}
510514
Some((Relation::ChildOfParent(src), Relation::ChildOfParent(dst))) => {
@@ -513,7 +517,7 @@ impl<T: Change> Tracker<T> {
513517
_ => {}
514518
}
515519
}
516-
Ok(Action::Continue)
520+
Ok(std::ops::ControlFlow::Continue(()))
517521
}
518522

519523
/// Emit the children of `src_parent_id` and `dst_parent_id` as pairs of exact matches, which are assumed
@@ -567,15 +571,15 @@ impl<T: Change> Tracker<T> {
567571
src_item.emitted = true;
568572
dst_item.emitted = true;
569573

570-
if res == Action::Cancel {
574+
if res.is_break() {
571575
return Ok(res);
572576
}
573577
} else {
574578
gix_trace::warn!("Children of parents with change-id {src_parent_id} and {dst_parent_id} were not equal, even though their parents claimed to be");
575579
break;
576580
}
577581
}
578-
Ok(Action::Continue)
582+
Ok(std::ops::ControlFlow::Continue(()))
579583
}
580584

581585
/// Find directories with relation id that haven't been emitted yet and store them for lookup.
@@ -621,7 +625,7 @@ impl<T: Change> Tracker<T> {
621625
self.items[src_idx].emitted = true;
622626
self.items[dst_idx].emitted = true;
623627

624-
if res == Action::Cancel {
628+
if res.is_break() {
625629
return Ok(());
626630
}
627631
}

gix-diff/src/tree/function.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::tree::{
2323
/// by the delegate implementation which should be as specific as possible. Rename tracking can be computed on top of the changes
2424
/// received by the `delegate`.
2525
/// * cycle checking is not performed, but can be performed in the delegate which can return
26-
/// [`tree::visit::Action::Cancel`](crate::tree::visit::Action::Cancel) to stop the traversal.
26+
/// [`std::ops::ControlFlow::Break`] to stop the traversal.
2727
///
2828
/// [git_cmp_c]: https://github.com/git/git/blob/ef8ce8f3d4344fd3af049c17eeba5cd20d98b69f/tree-diff.c#L72-L88
2929
/// [git_cmp_rs]: https://github.com/GitoxideLabs/gitoxide/blob/795962b107d86f58b1f7c75006da256d19cc80ad/gix-object/src/tree/mod.rs#L263-L273
@@ -148,7 +148,7 @@ fn delete_entry_schedule_recursion(
148148
oid: entry.oid.to_owned(),
149149
relation,
150150
})
151-
.cancelled();
151+
.is_break();
152152
if is_cancelled {
153153
return Err(Error::Cancelled);
154154
}
@@ -180,7 +180,7 @@ fn add_entry_schedule_recursion(
180180
oid: entry.oid.to_owned(),
181181
relation,
182182
})
183-
.cancelled()
183+
.is_break()
184184
{
185185
return Err(Error::Cancelled);
186186
}
@@ -309,7 +309,7 @@ fn handle_lhs_and_rhs_with_equal_filenames(
309309
entry_mode: rhs.mode,
310310
oid: rhs.oid.to_owned(),
311311
})
312-
.cancelled()
312+
.is_break()
313313
{
314314
return Err(Error::Cancelled);
315315
}
@@ -327,7 +327,7 @@ fn handle_lhs_and_rhs_with_equal_filenames(
327327
oid: lhs.oid.to_owned(),
328328
relation: None,
329329
})
330-
.cancelled()
330+
.is_break()
331331
{
332332
return Err(Error::Cancelled);
333333
}
@@ -342,7 +342,7 @@ fn handle_lhs_and_rhs_with_equal_filenames(
342342
oid: rhs.oid.to_owned(),
343343
relation,
344344
})
345-
.cancelled()
345+
.is_break()
346346
{
347347
return Err(Error::Cancelled);
348348
}
@@ -360,7 +360,7 @@ fn handle_lhs_and_rhs_with_equal_filenames(
360360
oid: lhs.oid.to_owned(),
361361
relation,
362362
})
363-
.cancelled()
363+
.is_break()
364364
{
365365
return Err(Error::Cancelled);
366366
}
@@ -370,7 +370,7 @@ fn handle_lhs_and_rhs_with_equal_filenames(
370370
oid: rhs.oid.to_owned(),
371371
relation: None,
372372
})
373-
.cancelled()
373+
.is_break()
374374
{
375375
return Err(Error::Cancelled);
376376
}
@@ -387,7 +387,7 @@ fn handle_lhs_and_rhs_with_equal_filenames(
387387
entry_mode: rhs.mode,
388388
oid: rhs.oid.to_owned(),
389389
})
390-
.cancelled()
390+
.is_break()
391391
{
392392
return Err(Error::Cancelled);
393393
}

gix-diff/src/tree/recorder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,6 @@ impl Visit for Recorder {
174174
path: self.path_clone(),
175175
},
176176
});
177-
visit::Action::Continue
177+
std::ops::ControlFlow::Continue(())
178178
}
179179
}

0 commit comments

Comments
 (0)