Enums in UDL
Our simple enum example is defined in UDL as:
enum Animal {
"Dog",
"Cat",
};
Enums with fields
Enumerations with associated data require a different syntax,
due to the limitations of using WebIDL as the basis for UniFFI's interface language.
An enum like IpAddr
is specifiedl in UDL like:
[Enum]
interface IpAddr {
V4(u8 q1, u8 q2, u8 q3, u8 q4);
V6(string addr);
};
Remote, non-exhaustive enums
One corner case is an enum that's defined in another crate and has the non_exhaustive` attribute.
In this case, UniFFI needs to generate a default arm when matching against the enum variants, or else a compile error will be generated.
Use the [NonExhaustive]
attribute to handle this case:
[Enum]
[NonExhaustive]
interface Message {
Send(u32 from, u32 to, string contents);
Quit();
};
Note: since UniFFI generates a default arm, if you leave out a variant, or if the upstream crate adds a new variant, this won't be caught at compile time. Any attempt to pass that variant across the FFI will result in a panic.