What's this?
- This Repository provide tools to bind rust interface with other language and export library artifact directly.
- It generate bindings from a Rust package and packaged to android aar or iOS lib. You don't need to write jni or other ffi code with this tool.
Step by step.
- Setup rust environment.
- Install 'rsbind'.
cargo install --git https://github.com/sidneywang/rsbind.git --force -- rsbind
- Create a Rust library, which contains two directory, contract and imp. You can put your interface to contract module and implemation to imp module. Expose these two modules in lib.rs.
// such as your code in contract dir as below:
// Your implementation is as below
- Run rsbind command as below. Then the generated code will be in _gen directory and aar/framework will be in target directory.
Rsbind usage:
- ast: generate simplified ast files with json format to _gen/ast.
- bridge: generate c methods to expose our interface to _gen/[ios/android]_bridge.
- artifact: generate java/swift wrapper and c header, and then put then into a project(_gen/[ios/android]_artifact).
- build: build bridge modules and copy output to artifact project and then build artifact project.
- all: run all the steps for binding.
- It will generate java files packaged in aar or cocoapods lib, then you can integrated them to your android/iOS project and call the functions. For android, you can call like as below:
YourContract.
Swift is very similar.
Configuration
You can create a file named Rsbind.toml to add some configuration.
[]
= ""
= ["armv7-linux-androideabi"]
= ["aarch64-linux-android"]
= ["i686-linux-android"]
= true
= "com.afoxer.xxx.ffi"
= "demo"
= []
= ["xxxx=[]"]
[]
= ""
= ["armv7-apple-ios"]
= ["i386-apple-ios", "x86_64-apple-ios"]
= true
= []
Supported Types
- Parameters: Basic types, Callback, Vec
- Return: Basic types, Struct, Vec
supported types in Callback:
- Parameters: Basic types, Vec, Struct
- Return: Basic types.
Note:
These is not supported yet:
- Vec < Struct >
- Struct in trait paramters.
- Callback in return type.
In Callback:
- Return String.
- TODO: add callback support for return types.
Will support it in near future.
It is different to define a callback and a normal trait. It should contains &self in every callback but not in normal trait.
Callback:
Normal trait: