I want to use rust on mobile platforms in place of C++, I mean write core logic
on C++ and use on both iOS and Android, and write GUI on Java, Objective-C on appropriate platform.
In order to do it I need some tool to bind Java with Rust, similar to swig.
And looks like there is no such tool as I want, so I wrote proof-of-concept with such functionality:
Suppose you have the following Rust code:
struct Foo {
data: i32
}
impl Foo {
fn new(val: i32) -> Foo {
Foo{data: val}
}
fn f(&self, a: i32, b: i32) -> i32 {
self.data + a + b
}
}
and you want to write in Java something like this:
Foo foo = new Foo(5);
int res = foo.f(1, 2);
assert res == 8;
In order to implement it rust_swig suggests the following functionality,
in Rust project you write (in Rust language):
foreigner_class!(class Foo {
self_type Foo;
constructor Foo::new(_: i32) -> Foo;
method Foo::f(&self, _: i32, _: i32) -> i32;
});
and that's all, as a result rust_swig generates JNI wrappers for Rust functions
and Java code to call these JNI functions.
See rust_swig test for jni for working example.
So questions:
- What do you think about usage of macros for such task, may be any more suitable forms?
- I prefer explicit via implicit so I use such form to explicity marking of exported methods, never think about automate mapping of "pub" functionality of rust crate to Java. Is it possible to map "Rust: many traits plus struct" to "Java: class plus interfaces" in theory?
- As you can see I use full function signagutre in macros, to use only full name of function without specify types of its arguments and output, should I duplicate functionality of racer for parsing cache of cargo, or there is more simple way to achieve this?