
Development Environment
- Rust
- Dart
- libclang (for generating bindings)
- Linux
apt-get install libclang-dev
- MacOS
brew install llvm
- Linux
On Linux ffigen looks for libclang at /usr/lib/llvm-11/lib/libclang.so so you may need to symlink to the version specific library: ln -s /usr/lib/llvm-11/lib/libclang.so.1 /usr/lib/llvm-11/lib/libclang.so.
Usage
View the example directory for a runnable example.
In your crate's lib.rs add a RUNTIME static that will survive for the lifetime of the program. RUNTIME must hold an instance of membrane::App<Runtime> where Runtime has the membrane::Interface trait implemented for whichever async framework you wish to use. In our examples we use tokio to provide the runtime behavior, you are welcome to copy it:
use ;
;
static RUNTIME: = new;
Then write some code that is annotated with the #[async_dart] macro. No need to use C types here, just use Rust String, i64, f64, bool, structs, or enums as usual (or with Option). The functions can be anywhere in your program and may return either an async Result<T, E> or an impl Stream<Item = Result<T, E>>:
use async_dart;
use Stream;
use cratedata;
pub async
And now you are ready to generate the Dart package. Note that this code goes in a bin/generator.rs or similar to be ran with cargo run or a build task rather than in build.rs (which only runs before compilation):
If everything went as planned you can now call Rust from Dart with:
(--enable-asserts enables a pretty print toString() in the generated classes)
import 'package:dart_example/accounts.dart';
void main(List<String> arguments) async {
var accounts = AccountsApi();
print(await accounts.contact(id: "1"));
}
If you get an error on Linux about not being able to load libexample.so then add the pub package's path to LD_LIBRARY_PATH.