randomPoison / cs-bindgen

Experiment in using Rust to build a library that can be loaded by Unity across multiple platforms
4 stars 0 forks source link

Use out parameters to improve overload resolution for type conversion #47

Closed randomPoison closed 4 years ago

randomPoison commented 4 years ago

Change the __FromRaw() and __IntoRaw() conversion functions to take an out parameter instead of directly returning their output. This allows overload resolution to take the output type into account, removing the need for a unique C# type corresponding to every Rust type used in an ABI boundary.

For example, we previously needed a separate RustBool struct so that we could have separate overloads of __FromRaw for byte and bool (since both u8 and bool are represented as byte on the C# side). Now, we can have multiple overloads of __FromRaw that take byte as the input as long as their output types differ.

This change complicates the generated code for functions, since we can no longer chain functions together as easily. However, the extra complication in the generated code is worth it, since this change drastically reduces the amount of monomorphization that we will need to perform when handling generic types (e.g. collection types).