In order to return the length of a string to C#, the Rust code currently takes an extra *mut u32 parameter in order which it sets to the length of the string. Instead, it would be easier to return a struct that contains the pointer to the string buffer and the length of the string. C# has the StructLayout attribute which allows us to ensure that the struct is laid out the same between Rust and C#. So the following two definitions should be compatible:
[StructLayout(LayoutKind.Sequential)]
public struct FfiString
{
public IntPtr Pointer;
public int Length;
}
We could further extend this with generic a generic type definition that will work for all slice types, though doing so isn't necessary for this first pass since we currently only support String specifically.
In order to return the length of a string to C#, the Rust code currently takes an extra
*mut u32
parameter in order which it sets to the length of the string. Instead, it would be easier to return a struct that contains the pointer to the string buffer and the length of the string. C# has theStructLayout
attribute which allows us to ensure that the struct is laid out the same between Rust and C#. So the following two definitions should be compatible:We could further extend this with generic a generic type definition that will work for all slice types, though doing so isn't necessary for this first pass since we currently only support
String
specifically.