Skip to content
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

Generating array pointers #1561

Closed
elichai opened this issue May 13, 2019 · 9 comments · Fixed by #1564
Closed

Generating array pointers #1561

elichai opened this issue May 13, 2019 · 9 comments · Fixed by #1564

Comments

@elichai
Copy link
Contributor

elichai commented May 13, 2019

Input C Header

#include <stdint.h>
void my_func(uint8_t arr[20]);

Bindgen Invocation

$ bindgen test.h

Actual Results

extern "C" {
    pub fn my_func(arr: *mut u8);
}

Expected Results

I would've want it to produce this code instead:

extern "C" {
    pub fn my_func(arr: *mut [u8; 20]);
}
@emilio
Copy link
Contributor

emilio commented May 13, 2019

Per IRC discussion, this is patchable, assuming they're equivalent, but it's not 100% clear to me they are, as pointers to slices and array in rust are so weird.

@elichai
Copy link
Contributor Author

elichai commented May 13, 2019

I really hope they're equivalent, let's see if someone that knows will answer.
posted also in the forum after someone asked me to https://users.rust-lang.org/t/is-using-array-pointers-with-ffi-ub/28197

@elichai
Copy link
Contributor Author

elichai commented May 14, 2019

Right now the responses in the forum seems to suggest that this isn't UB, and there's a difference between slices and arrays.
But I hoped that someone more known in the community will respond.

@Proximyst
Copy link

@emilio The memory structure of the type *const T and *const [u8; size_of::<T>()] are the same, seeing as they're contiguous. Seeing as they're the same, and *const T is the same as const *uint8_t, const *uint8_t also has to be the same as *const [u8; size_of::<T>()], and the fix elichai is proposing is a valid one.

Source:

An array is a collection of objects of the same type T, stored in contiguous memory.
https://doc.rust-lang.org/rust-by-example/primitives/array.html

@emilio
Copy link
Contributor

emilio commented May 20, 2019

Yes. This should be pretty easy to patch:

let arg_ty = match *arg_ty.canonical_type(ctx).kind() {

Probably worth making it configurable, given it's trivial to keep both behaviors.

@elichai
Copy link
Contributor Author

elichai commented May 20, 2019

how do you want to pass the desired behavior? add an option to the builder?

I can give this a try and make a PR

@emilio
Copy link
Contributor

emilio commented May 20, 2019

Yeah, the options should be accessible from there, see similar uses of ctx.options() in that file.

@elichai
Copy link
Contributor Author

elichai commented May 20, 2019

ok, thanks that way it shouldn't even be a breaking change if the default stays the same :)

@emilio
Copy link
Contributor

emilio commented May 20, 2019

Yeah, that's definitely a plus :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants