How to send an enum value from Rust to Deno
Hello there 👋
I am quite a beginner to FFI and I am not sure which resources to read to learn more about it in Rust.
My problem: I got two enums
MyEvent
and MySubEvent
.
I want to emit these events to callback function declared in Deno side
I do not know how to recognize which event has been sent in Deno
5 Replies
Based on the documentation, Deno's FFI API currently supports a list of types that can be used to communicate between Deno and native libraries. However, complex types like enums are not directly supported.
In your case, you are trying to pass an enum from Rust to Deno. This is not directly supported by Deno's FFI API. The types that are currently supported by Deno's FFI API are
i8
, u8
, i16
, u16
, i32
, u32
, i64
, u64
, usize
, f32
, f64
, void
, pointer
, buffer
, function
, and { struct: [...] }
.
One possible workaround could be to represent your enum as an integer or a struct in Rust and then pass it to Deno. On the Deno side, you can then interpret the received integer or struct as your enum. However, this approach requires you to manually manage the mapping between your enum and the integer or struct, and it may not work for complex enums.
Here's an example of how you might do this:
And on the Deno side:
In this example, the Rust enum MyEvent
is represented as an integer (u32
), and this integer is passed to Deno through the FFI API. On the Deno side, the received integeris then interpreted as the enum
MyEvent
.
Please note that this is a workaround and may not work for complex enums or enums withIs it a possible to pass the event type with the event?
The easiest way would be passing around numbers
but since you have different levels of info going around, maybe use serde?