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

Prevent name collisions between enum variants and other types #42

Closed randomPoison closed 4 years ago

randomPoison commented 4 years ago

Change how data-carrying enums are represented in C# to put the structs for the variants inside a wrapper class. This effectively namespaces the variants such that they won't collide if a variant has the same name as another type in the same namespace.

This also has the added benefit of making data-carrying enums feel a bit more like regular enums on the C# side. For example, the following exported enum:

#[cs_bindgen]
pub enum MyEnum {
    Foo,
    Bar { name: String },
}

Is used like this on the C# side:

let value = new MyEnum.Foo();

switch (value)
{
    case MyEnum.Foo foo:
        // ...

    case MyEnum.Bar bar:
        // ...
}

In order to better handle naming collisions, I've updated the bits of code that generate type references to fully-qualify the generated type name for all user-defined types. For now that just means prefixing them with global::, but this can be extended to include the full namespace once custom namespaces are supported.