I have made a bindgen to allow Dart/Flutter to call Rust via FFI. It is memory safe, and you do not need to care about anything like allocate/free an object.
Question: Anyone interested in it? If many people are interested, I can polish it can make it open-source. (Since you know, making it open-source will require some time and efforts.)
Features
- Memory-safe: never need to think about alloc/free.
- Zero-copy (almost): Big objects can be passed from Rust to Dart without any copy.
- Rich type support: Not only primitives like int/double, but also Uint8List(Vec), List(Vec), any custom structs. You can even use recursive structs.
- Async programming: Your Rust code can run for a long time, and it will not block the main isolate (i.e. not block UI). You can call functions directly in main isolate of Dart, so no need for switching between isolates.
- Easy to use: All you need to do is write down your Rust code. The bindgen will do everything and expose an API in the Dart/Flutter style.
Example
Write the following Rust code (that is all you need to do!):
pub struct TreeNode {
pub value: i32, # of course, also support other types
pub children: Vec<MyTreeNode>,
}
pub fn hello_world(s: MyTreeNode, b: SomeOtherStruct) -> Result<Something> {
Ok(...)
}
It will automatically generate everything, and you only need to call a generated Dart/Flutter API which looks like:
class ExampleApi {
Future<Something> helloWorld({required MyTreeNode a, required SomeOtherStruct b}) async { ... auto generated implementation ... }
}
P.S. There already exists a low-level one (in the C style), but all memory alloc/free should be done manually, so it is quite unsafe. That is why I do this high-level bindgen.