Skip to content

repr(transparent) on a tuple struct wrapping an array #777

@mversic

Description

@mversic

1: With the latest cbindgen, the following code:

#[repr(C)]
pub struct K([u64; 2]);
#[no_mangle]
pub unsafe extern "C" fn function(arg: K) -> K {
    a
}

correctly produces:

typedef struct K {
  uint64_t _0[2];
} K;
struct K function(struct K arg);

whereas the following code:

#[repr(transparent)]
pub struct K([u64; 2]);
#[no_mangle]
pub unsafe extern "C" fn function(arg: K) -> K {
    a
}

incorrectly produces:

typedef uint64_t K[2];
K function(K arg);

The 2nd example is incorrect because arrays in C decay to pointers (generated typedef is just an alias for an array) which means that the generated binding doesn't have the same ABI as it's rust equivalent. I see no reason to forbid this conversion (as is done for [u64;2]), rather I would expect that the same code is generated as in the 1st example

2: As a side note, I would also like to ask why is there no workaround to support raw [u64;2] in cbindgen like:

#[no_mangle]
pub unsafe extern "C" fn function(arg: [u64; 2]) -> [u64; 2] {
    a
}

Possible solutions to support this are to generate a custom wrapper struct on the C side, or maybe add a cbindgen.toml option

Somewhat related to #171 but this issue is, as far as I can see, resolved and should be closed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions