Trying to create Rust bindings for speech-dispatcher. Yesterday I published the speech-dispatcher-sys crate based on the newest bindgen. I’d been using an older bindgen a few months ago, and in my higher-level speech-dispatcher crate I had code like:

So I used to be able to cast the enum value to u32, but it now looks like the bindgen functions in the sys crate take the enum directly.

I guess my initial question is, how do I change this code such that the sys crate gets what it expects? Do I have to implement Into or add match statements on each of dozens of functions or enums? But, looking deeper, another possibility would be if I could re-export these awful sys enum values as something more rusty. How should I fix this?

That’s because Rust will corrupt memory and crash hard if you cast u32 to an enum with a value that isn’t strictly part of the enum. C sometimes uses enums for values that are OR-ed together, and Rust can’t allow that.

For example:

enum Foo {
A = 1,
B = 2,
}
extern "C" fn take_enum(foo: Foo);

calling take_enum(3) is undefined behavior, and the risk is not theoretical, but it actually causes match to misbehave badly.