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

For the FixedArray type, generate more appropriate C# code #91

Open
szhang21 opened this issue Sep 6, 2024 · 1 comment
Open

For the FixedArray type, generate more appropriate C# code #91

szhang21 opened this issue Sep 6, 2024 · 1 comment

Comments

@szhang21
Copy link

szhang21 commented Sep 6, 2024

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
Copy link
Author

szhang21 commented Sep 6, 2024

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.

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

No branches or pull requests

1 participant