Skip to content

Commit a073509

Browse files
committed
Generify ForeachCallbacks a bit.
In particular, this makes the file callback optional as well. It also renames the struct to be a bit less specific to the foreach method; the next patch will reuse this in a different API.
1 parent c42e8e9 commit a073509

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/diff.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ pub type BinaryCb<'a> = dyn FnMut(DiffDelta<'_>, DiffBinary<'_>) -> bool + 'a;
109109
pub type HunkCb<'a> = dyn FnMut(DiffDelta<'_>, DiffHunk<'_>) -> bool + 'a;
110110
pub type LineCb<'a> = dyn FnMut(DiffDelta<'_>, Option<DiffHunk<'_>>, DiffLine<'_>) -> bool + 'a;
111111

112-
struct ForeachCallbacks<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h> {
113-
file: &'a mut FileCb<'b>,
112+
struct DiffCallbacks<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h> {
113+
file: Option<&'a mut FileCb<'b>>,
114114
binary: Option<&'c mut BinaryCb<'d>>,
115115
hunk: Option<&'e mut HunkCb<'f>>,
116116
line: Option<&'g mut LineCb<'h>>,
@@ -182,8 +182,8 @@ impl<'repo> Diff<'repo> {
182182
hunk_cb: Option<&mut HunkCb<'_>>,
183183
line_cb: Option<&mut LineCb<'_>>,
184184
) -> Result<(), Error> {
185-
let mut cbs = ForeachCallbacks {
186-
file: file_cb,
185+
let mut cbs = DiffCallbacks {
186+
file: Some(file_cb),
187187
binary: binary_cb,
188188
hunk: hunk_cb,
189189
line: line_cb,
@@ -276,8 +276,11 @@ extern "C" fn file_cb_c(
276276
let delta = Binding::from_raw(delta as *mut _);
277277

278278
let r = panic::wrap(|| {
279-
let cbs = data as *mut ForeachCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
280-
((*cbs).file)(delta, progress)
279+
let cbs = data as *mut DiffCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
280+
match (*cbs).file {
281+
Some(ref mut cb) => cb(delta, progress),
282+
None => false,
283+
}
281284
});
282285
if r == Some(true) {
283286
0
@@ -297,7 +300,7 @@ extern "C" fn binary_cb_c(
297300
let binary = Binding::from_raw(binary);
298301

299302
let r = panic::wrap(|| {
300-
let cbs = data as *mut ForeachCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
303+
let cbs = data as *mut DiffCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
301304
match (*cbs).binary {
302305
Some(ref mut cb) => cb(delta, binary),
303306
None => false,
@@ -321,7 +324,7 @@ extern "C" fn hunk_cb_c(
321324
let hunk = Binding::from_raw(hunk);
322325

323326
let r = panic::wrap(|| {
324-
let cbs = data as *mut ForeachCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
327+
let cbs = data as *mut DiffCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
325328
match (*cbs).hunk {
326329
Some(ref mut cb) => cb(delta, hunk),
327330
None => false,
@@ -347,7 +350,7 @@ extern "C" fn line_cb_c(
347350
let line = Binding::from_raw(line);
348351

349352
let r = panic::wrap(|| {
350-
let cbs = data as *mut ForeachCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
353+
let cbs = data as *mut DiffCallbacks<'_, '_, '_, '_, '_, '_, '_, '_>;
351354
match (*cbs).line {
352355
Some(ref mut cb) => cb(delta, hunk, line),
353356
None => false,

0 commit comments

Comments
 (0)