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

Use of invalid enum value causes a panic #11

Open
nymoral opened this issue Aug 2, 2023 · 1 comment
Open

Use of invalid enum value causes a panic #11

nymoral opened this issue Aug 2, 2023 · 1 comment
Labels
bug Something isn't working enhancement New feature or request

Comments

@nymoral
Copy link

nymoral commented Aug 2, 2023

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

dignifiedquire commented Oct 3, 2023

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)
}

@arg0d arg0d added enhancement New feature or request bug Something isn't working labels Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants