cargo install --git https://github.com/sidneywang/rsbind.git --force -- rsbind
pub trait Callback { fn on_callback(arg1: i64, arg2: String); }
pub struct StructSimple { pub arg3: String, pub arg4: bool, } ```
```rust // Your implementation is as below pub struct YourImplemetation {}
impl YourContract for YourImplemetation { fn testsimple(arg1: i32, arg2: String) -> String { format!("Your testsimple result is {}_{}", arg1, arg2) }
fn test_callback(arg: Box<Callback>) {
arg.on_callback(123i64, "hello callback".to_owned());
}
fn test_struct() -> StructSimple {
StructSimple {
arg1: "struct".to_owned(),
arg2: true
}
}
} ```
Rsbind usage:
sh
rsbind path-of-project android/ios/all ast/bridge/artifact/header/build/all
- 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.
java
YourContract.test_callback(new Callback(){
void on_callback(long arg1, String arg2) {
// do your things.
}
})
Swift is very similar.You can create a file named Rsbind.toml to add some configuration. ```toml [android] rustcparam = "" arch = ["armv7-linux-androideabi"] arch64 = ["aarch64-linux-android"] archx86 = ["i686-linux-android"] release = true namespace = "com.afoxer.xxx.ffi" soname = "demo" extlib = [] featuresdef = ["xxxx=[]"]
[ios] rustcparam = "" archphone = ["armv7-apple-ios"] archsimu = ["i386-apple-ios", "x8664-apple-ios"] release = true features_def = [] ```
supported types in Callback: - Parameters: Basic types, Vec, Struct - Return: Basic types.
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:
rust
pub trait Callback : Sync {
fn on_callback(&self, arg1: i32, arg2: String, arg3: bool, arg4: f32, arg5: f64) -> i32;
fn on_callback2(&self, arg1: bool) -> bool;
fn on_callback_complex(&self, arg1: StructSimple) -> bool;
fn on_callback_arg_vec(&self, arg1: Vec<StructSimple>) -> bool;
fn on_callback_arg_vec_simple(&self, arg1: Vec<String>) -> bool;
}
Normal trait:
```rust
pub trait TestContract1 {
fn testargvec(arg: Vec
```