Request for comments about design of "rust for java" wrapper

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 { + 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:

  1. What do you think about usage of macros for such task, may be any more suitable forms?
  2. 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?
  3. 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?
1 Like

Did you see posted to /r/rust a day ago? Might be useful.

Did you see posted to /r/rust a day ago? Might be useful.

No, I have not seen it, thanks for pointing out.
Actually my project is about "how NOT to do what is described in this article",
but the article is interesting from internal implementation's point of view.

What do you think about r/rust, is my questions appropriate for it?

It's definitely appropriate for either location. I wasn't redirecting you over there, just wanted to make sure you saw a relevant recent post. I'm personally interested in Java/Rust interop, and can see value in both approaches.