-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using an Option<>
with a function/nonnull type alias generates incorrect bindings
#326
Comments
Option<>
with a function type alias generates incorrect bindingsOption<>
with a function/nonnull type alias generates incorrect bindings
So there are two issues here:
This is due to the same reason, which is that those methods don't really have the type information of what the path refers to. This is all conceptually fixable, but I haven't dug into how easy / worth it is it. |
FWIW this is blocking miniz_oxide from exposing proper C bindings. |
This seems pretty serious. It makes C bindgen pretty much useless for expressing callbacks, which is a very common part of C libraries. For other functions and types, C bindgen is amazing! I looked into working around this by excluding certain types / functions from C bindgen analysis, but I couldn't figure out how to do that effectively. I also thought that perhaps I could just include some text that I produced manually, but I didn't see how to do that either. If there is a workaround that doesn't involve manually editing my header, I can't figure it out. (I made a sample project to experiment -- see
|
This probably needs a bit of work on cbindgen's side to fix, but it should not be too terrible, at the very least for easy cases... What probably needs to be done is to make That being said:
Are the
The easiest one (though I agree is not amazing) is just defining the "optional callback" as a separate typedef, so: Of course that's not terribly amazing. But it seems better than dealing with manually-generated stuff. |
I believe that a workaround using hand-coded text for some of the API would require #303 (header is included before defining the "optional callback" as a separate typedef makes it so the function signature is not available in the headerfile, I tried defining an extra type like this:
then renaming in cbindgen.toml config:
but then I get the extra typedef that requires manual removal (probably the order of rename operations causes |
The workaround I found was to just not use Option in the function interface and check for null
here's the change to my example: ultrasaurus/rust-clib@f93e819 I don't love this solution, but I like it better than having a manual step in my build process :) |
That is UB, and probably the null check will be optimized away in release builds. |
That's not quite what I meant. This seems to do the trick, or am I missing something? The pub type StatusCallback = Option<extern "C" fn(i32) -> ()>;
#[no_mangle]
pub extern "C" fn root(ptr: StatusCallback) {} |
That totally worked! awesome :) Btw: not sure what you mean by "That is UB" -- undefined behavior? what part of my workaround were you referring to? |
From the point of view of the compiler, |
With the following code:
bindgen will generate the following incorrect header:
instead of the expected
The text was updated successfully, but these errors were encountered: