Dushistov / flapigen-rs

Tool for connecting programs or libraries written in Rust with other languages
BSD 3-Clause "New" or "Revised" License
787 stars 59 forks source link

error: Do not know conversation from Java type to such rust type 'Vec < Foo >' #270

Closed arrtchiu closed 5 years ago

arrtchiu commented 5 years ago

Sorry for all the questions, I really appreciate your help - let's discuss if you'd like me to contribute financially!

For some reason, I can return Vec<Foo>s from methods but not accept them as params. References and arrays also have the same issue.

--- stderr
error in android bindings: src/ffi_glue.rs.in
parsing of android bindings: src/ffi_glue.rs.in failed
error: Do not know conversation from Java type to such rust type 'Vec < Foo >'
    fn TestContainers::set_struct_vec(&mut self, _: Vec<Foo>);
                                                 ^^^

I basically copied the code from the tests:

#[derive(Clone, Default)]
pub struct Foo {
    u32: bar,
}

#[derive(Default)]
pub struct TestContainers {
    struct_vec: Vec<Foo>,
}

impl TestContainers {
    fn set_struct_vec(&mut self, v: Vec<Foo>) {
        self.struct_vec = v;
    }
}

foreign_class!(class Foo {
    self_type Foo;
    constructor Foo::default() -> Foo;
});

foreign_class!(class TestContainers {
    self_type TestContainers;
    constructor TestContainers::default() -> TestContainers;
    fn TestContainers::set_struct_vec(&mut self, _: Vec<Foo>);
});
Dushistov commented 5 years ago

For some reason, I can return Vecs from methods but not accept them as params. References > and arrays also have the same issue.

All type conversation rules is described via Domain specific language, that looks like Rust macroses.

Corresponding rule related to your problem you can find here:

https://github.com/Dushistov/rust_swig/blob/84846c680c84981188025ac641dfa30e4002bd1d/macroslib/src/java_jni/jni-include.rs#L470

As you see to "trigger" this rule your type should implement Clone, rust_swig doesn't parse your real Rust code, so you should point rust_swig about Clone implementation via:

foreign_class!(
#[derive(Clone)]
class Foo {

Sorry for all the questions you'd like me to contribute financially!

I am more interesting in code contribution and testing. So contribution via questions is not bad, because of it real usage in other words testing. May be someday I improve error handling to report rules that may be matched, but were skipped because of trait bounds mismatch.

arrtchiu commented 5 years ago

Thanks! Adding that derive inside the foreign_class! did the trick. Is something bad possible if I mismatch them?

Dushistov commented 5 years ago

Is something bad possible if I mismatch them?

No, rust_swig generates normal Rust code, so you got compilation error about missed function or type mismatch in generated code if something goes wrong.