Skip to content

Commit

Permalink
Make all callbacks optional in libgit2-sys
Browse files Browse the repository at this point in the history
Appease the nightly compiler's checks for undefinededness and reflect
how this is actually what the typedef is in C, a nullable function
pointer.
  • Loading branch information
alexcrichton committed Mar 13, 2020
1 parent d34e49c commit d70b645
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 209 deletions.
335 changes: 186 additions & 149 deletions libgit2-sys/lib.rs

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,27 @@ impl<'repo> Drop for BlobWriter<'repo> {
fn drop(&mut self) {
// We need cleanup in case the stream has not been committed
if self.need_cleanup {
unsafe { ((*self.raw).free)(self.raw) }
unsafe {
if let Some(f) = (*self.raw).free {
f(self.raw)
}
}
}
}
}

impl<'repo> io::Write for BlobWriter<'repo> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
unsafe {
let res = ((*self.raw).write)(self.raw, buf.as_ptr() as *const _, buf.len());
if res < 0 {
Err(io::Error::new(io::ErrorKind::Other, "Write error"))
if let Some(f) = (*self.raw).write {
let res = f(self.raw, buf.as_ptr() as *const _, buf.len());
if res < 0 {
Err(io::Error::new(io::ErrorKind::Other, "Write error"))
} else {
Ok(buf.len())
}
} else {
Ok(buf.len())
Err(io::Error::new(io::ErrorKind::Other, "no write callback"))
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,11 @@ impl<'cb> CheckoutBuilder<'cb> {
opts.their_label = c.as_ptr();
}
if self.progress.is_some() {
let f: raw::git_checkout_progress_cb = progress_cb;
opts.progress_cb = Some(f);
opts.progress_cb = Some(progress_cb);
opts.progress_payload = self as *mut _ as *mut _;
}
if self.notify.is_some() {
let f: raw::git_checkout_notify_cb = notify_cb;
opts.notify_cb = Some(f);
opts.notify_cb = Some(notify_cb);
opts.notify_payload = self as *mut _ as *mut _;
opts.notify_flags = self.notify_flags.bits() as c_uint;
}
Expand Down
6 changes: 5 additions & 1 deletion src/cred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ impl Binding for Cred {
impl Drop for Cred {
fn drop(&mut self) {
if !self.raw.is_null() {
unsafe { ((*self.raw).free)(self.raw) }
unsafe {
if let Some(f) = (*self.raw).free {
f(self.raw)
}
}
}
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,9 @@ impl<'repo> Diff<'repo> {
{
let mut cb: &mut PrintCb<'_> = &mut cb;
let ptr = &mut cb as *mut _;
let print: raw::git_diff_line_cb = Some(print_cb);
unsafe {
try_call!(raw::git_diff_print(
self.raw,
format,
print_cb,
ptr as *mut _
));
try_call!(raw::git_diff_print(self.raw, format, print, ptr as *mut _));
Ok(())
}
}
Expand All @@ -194,24 +190,25 @@ impl<'repo> Diff<'repo> {
};
let ptr = &mut cbs as *mut _;
unsafe {
let binary_cb_c = if cbs.binary.is_some() {
Some(binary_cb_c as raw::git_diff_binary_cb)
let binary_cb_c: raw::git_diff_binary_cb = if cbs.binary.is_some() {
Some(binary_cb_c)
} else {
None
};
let hunk_cb_c = if cbs.hunk.is_some() {
Some(hunk_cb_c as raw::git_diff_hunk_cb)
let hunk_cb_c: raw::git_diff_hunk_cb = if cbs.hunk.is_some() {
Some(hunk_cb_c)
} else {
None
};
let line_cb_c = if cbs.line.is_some() {
Some(line_cb_c as raw::git_diff_line_cb)
let line_cb_c: raw::git_diff_line_cb = if cbs.line.is_some() {
Some(line_cb_c)
} else {
None
};
let file_cb: raw::git_diff_file_cb = Some(file_cb_c);
try_call!(raw::git_diff_foreach(
self.raw,
file_cb_c,
file_cb,
binary_cb_c,
hunk_cb_c,
line_cb_c,
Expand Down
6 changes: 3 additions & 3 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl Index {
let ptr = cb.as_mut();
let callback = ptr
.as_ref()
.map(|_| index_matched_path_cb as raw::git_index_matched_path_cb);
.map(|_| index_matched_path_cb as extern "C" fn(_, _, _) -> _);
unsafe {
try_call!(raw::git_index_add_all(
self.raw,
Expand Down Expand Up @@ -473,7 +473,7 @@ impl Index {
let ptr = cb.as_mut();
let callback = ptr
.as_ref()
.map(|_| index_matched_path_cb as raw::git_index_matched_path_cb);
.map(|_| index_matched_path_cb as extern "C" fn(_, _, _) -> _);
unsafe {
try_call!(raw::git_index_remove_all(
self.raw,
Expand Down Expand Up @@ -511,7 +511,7 @@ impl Index {
let ptr = cb.as_mut();
let callback = ptr
.as_ref()
.map(|_| index_matched_path_cb as raw::git_index_matched_path_cb);
.map(|_| index_matched_path_cb as extern "C" fn(_, _, _) -> _);
unsafe {
try_call!(raw::git_index_update_all(
self.raw,
Expand Down
5 changes: 3 additions & 2 deletions src/odb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ impl<'repo> Odb<'repo> {
let mut data = ForeachCbData {
callback: &mut callback,
};
let cb: raw::git_odb_foreach_cb = Some(foreach_cb);
try_call!(raw::git_odb_foreach(
self.raw(),
foreach_cb,
cb,
&mut data as *mut _ as *mut _
));
Ok(())
Expand Down Expand Up @@ -155,7 +156,7 @@ impl<'repo> Odb<'repo> {
pub fn packwriter(&self) -> Result<OdbPackwriter<'_>, Error> {
let mut out = ptr::null_mut();
let progress = MaybeUninit::uninit();
let progress_cb = write_pack_progress_cb;
let progress_cb: raw::git_indexer_progress_cb = Some(write_pack_progress_cb);
let progress_payload = Box::new(OdbPackwriterCb { cb: None });
let progress_payload_ptr = Box::into_raw(progress_payload);

Expand Down
5 changes: 3 additions & 2 deletions src/packbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ impl<'repo> PackBuilder<'repo> {
{
let mut cb = &mut cb as &mut ForEachCb<'_>;
let ptr = &mut cb as *mut _;
let foreach: raw::git_packbuilder_foreach_cb = Some(foreach_c);
unsafe {
try_call!(raw::git_packbuilder_foreach(
self.raw,
foreach_c,
foreach,
ptr as *mut _
));
}
Expand All @@ -112,7 +113,7 @@ impl<'repo> PackBuilder<'repo> {
{
let mut progress = Box::new(Box::new(progress) as Box<ProgressCb<'_>>);
let ptr = &mut *progress as *mut _;
let progress_c = Some(progress_c as raw::git_packbuilder_progress);
let progress_c: raw::git_packbuilder_progress = Some(progress_c);
unsafe {
try_call!(raw::git_packbuilder_set_callbacks(
self.raw,
Expand Down
3 changes: 2 additions & 1 deletion src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ impl<'buffers> Patch<'buffers> {
pub fn print(&mut self, mut line_cb: &mut LineCb<'_>) -> Result<(), Error> {
let ptr = &mut line_cb as *mut _ as *mut c_void;
unsafe {
try_call!(raw::git_patch_print(self.raw, print_cb, ptr));
let cb: raw::git_diff_line_cb = Some(print_cb);
try_call!(raw::git_patch_print(self.raw, cb, ptr));
Ok(())
}
}
Expand Down
15 changes: 5 additions & 10 deletions src/remote_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,19 @@ impl<'a> Binding for RemoteCallbacks<'a> {
0
);
if self.progress.is_some() {
let f: raw::git_indexer_progress_cb = transfer_progress_cb;
callbacks.transfer_progress = Some(f);
callbacks.transfer_progress = Some(transfer_progress_cb);
}
if self.credentials.is_some() {
let f: raw::git_cred_acquire_cb = credentials_cb;
callbacks.credentials = Some(f);
callbacks.credentials = Some(credentials_cb);
}
if self.sideband_progress.is_some() {
let f: raw::git_transport_message_cb = sideband_progress_cb;
callbacks.sideband_progress = Some(f);
callbacks.sideband_progress = Some(sideband_progress_cb);
}
if self.certificate_check.is_some() {
let f: raw::git_transport_certificate_check_cb = certificate_check_cb;
callbacks.certificate_check = Some(f);
callbacks.certificate_check = Some(certificate_check_cb);
}
if self.push_update_reference.is_some() {
let f: extern "C" fn(_, _, _) -> c_int = push_update_reference_cb;
callbacks.push_update_reference = Some(f);
callbacks.push_update_reference = Some(push_update_reference_cb);
}
if self.update_tips.is_some() {
let f: extern "C" fn(
Expand Down
6 changes: 4 additions & 2 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,10 @@ impl Repository {
repo: self,
ret: &mut ret,
};
let cb: raw::git_submodule_cb = Some(append);
try_call!(raw::git_submodule_foreach(
self.raw,
append,
cb,
&mut data as *mut _ as *mut c_void
));
}
Expand Down Expand Up @@ -2426,9 +2427,10 @@ impl Repository {
let mut data = StashCbData {
callback: &mut callback,
};
let cb: raw::git_stash_cb = Some(stash_cb);
try_call!(raw::git_stash_foreach(
self.raw(),
stash_cb,
cb,
&mut data as *mut _ as *mut _
));
Ok(())
Expand Down
19 changes: 8 additions & 11 deletions src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@ where
});
let prefix = CString::new(prefix)?;
let datap = (&mut *data) as *mut TransportData as *mut c_void;
try_call!(raw::git_transport_register(
prefix,
transport_factory,
datap
));
let factory: raw::git_transport_cb = Some(transport_factory);
try_call!(raw::git_transport_register(prefix, factory, datap));
mem::forget(data);
Ok(())
}
Expand All @@ -141,9 +138,9 @@ impl Transport {

let mut raw = Box::new(RawSmartSubtransport {
raw: raw::git_smart_subtransport {
action: subtransport_action,
close: subtransport_close,
free: subtransport_free,
action: Some(subtransport_action),
close: Some(subtransport_close),
free: Some(subtransport_free),
},
obj: Box::new(subtransport),
});
Expand Down Expand Up @@ -257,9 +254,9 @@ extern "C" fn subtransport_action(
*stream = mem::transmute(Box::new(RawSmartSubtransportStream {
raw: raw::git_smart_subtransport_stream {
subtransport: raw_transport,
read: stream_read,
write: stream_write,
free: stream_free,
read: Some(stream_read),
write: Some(stream_write),
free: Some(stream_free),
},
obj: obj,
}));
Expand Down
2 changes: 1 addition & 1 deletion src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'repo> Tree<'repo> {
raw::git_tree_walk(
self.raw(),
mode.into(),
treewalk_cb::<T>,
Some(treewalk_cb::<T>),
&mut data as *mut _ as *mut c_void,
);
Ok(())
Expand Down
7 changes: 2 additions & 5 deletions src/treebuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,9 @@ impl<'repo> TreeBuilder<'repo> {
{
let mut cb: &mut FilterCb<'_> = &mut filter;
let ptr = &mut cb as *mut _;
let cb: raw::git_treebuilder_filter_cb = Some(filter_cb);
unsafe {
try_call!(raw::git_treebuilder_filter(
self.raw,
filter_cb,
ptr as *mut _
));
try_call!(raw::git_treebuilder_filter(self.raw, cb, ptr as *mut _));
panic::check();
}
Ok(())
Expand Down

0 comments on commit d70b645

Please sign in to comment.