NordSecurity / uniffi-bindgen-go

Uniffi bindings generator for Golang
Mozilla Public License 2.0
65 stars 18 forks source link

Use of invalid enum value causes a panic #11

Open nymoral opened 11 months ago

nymoral commented 11 months ago

Using an invalid valid as an enum causes the rust bindings to panic:

mypackage.UseFancyEnum(mypackage.FancyEnum(0))

causes Rust panic:

thread '<unnamed>' panicked at 'Failed to convert arg 'value': Invalid FancyEnum enum value: 0', [...].uniffi.rs:[...]
dignifiedquire commented 9 months ago

With the use of a private interface should be able to avoid this, sth like this

package main

import "fmt"

type FooBarKind uint

const (
    FooKind       FooBarKind = 0
    BarKind       FooBarKind = 1
    ForgottenKind FooBarKind = 2
)

type Enum interface {
    enum
}

func (v FooBarKind) value() uint {
    return uint(v)
}

type enum interface {
    value() uint
}

func printEnum(val Enum) {
    fmt.Println("hello %v", val)
}

func main() {
    printEnum(FooKind)
    printEnum(3) // compile error cannot use 3 (constant of type int) as Enum value in argument to printEnum: int does not implement Enum (missing method value)
}