csbindgen icon indicating copy to clipboard operation
csbindgen copied to clipboard

For the FixedArray type, generate more appropriate C# code

Open szhang21 opened this issue 1 year ago • 1 comments

This issue was discovered in Magicphysx. Here is the original Rust code and the currently generated C# code, it does not ensure the consistency of the struct's memory layout.

#[derive(Clone, Copy)]
#[cfg_attr(feature = "debug-structs", derive(Debug))]
#[repr(C)]
pub struct PxContactModifyPair {
    pub actor: [*const PxRigidActor; 2],
    pub shape: [*const PxShape; 2],
    pub transform: [PxTransform; 2],
    pub contacts: PxContactSet,
}
[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct PxContactModifyPair
{
    public fixed byte/* PxRigidActor, this length is invalid so must keep pointer and can't edit from C# */ actor[2];
    public fixed byte/* PxShape, this length is invalid so must keep pointer and can't edit from C# */ shape[2];
    public fixed byte/* PxTransform, this length is invalid so must keep pointer and can't edit from C# */ transform[2];
    public PxContactSet contacts;
}

I believe the following generated code would be more appropriate.

[StructLayout(LayoutKind.Sequential)]
public unsafe partial struct PxContactModifyPair
{
    public PxRigidActor* actor1;
    public PxRigidActor* actor2;
    public PxShape* shape1;
    public PxShape* shape2;
    public PxTransform transform1;
    public PxTransform transform2;
    public PxContactSet contacts;
}

szhang21 avatar Sep 06 '24 02:09 szhang21

In this case, if we directly pass the C# struct to native code, it will also produce the correct result and we can directly operate on the members of the returned structure from native.

szhang21 avatar Sep 06 '24 05:09 szhang21

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Mar 06 '25 00:03 github-actions[bot]