Rust-bindgen: Generating array pointers

Created on 13 May 2019  路  9Comments  路  Source: rust-lang/rust-bindgen

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]);
}
I-unclear enhancement

Most helpful comment

@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

All 9 comments

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.

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

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.

@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

Yes. This should be pretty easy to patch:

https://github.com/rust-lang/rust-bindgen/blob/e5250a01b98cb8ca9dc4584788dbc5db68147a71/src/codegen/mod.rs#L3909

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

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

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

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

Yeah, that's definitely a plus :)

Was this page helpful?
0 / 5 - 0 ratings