Skip to content

Add support for recursive types in struct #451

@CinchBlue

Description

@CinchBlue

If you have a lib.rs and a java_glue.rs.in, this will not work:

// lib.rs
mod java_glue;

pub enum RecursiveEnum {
    Null,
    S
}

#[derive(Debug, Clone, Default)]
pub struct Lol {
    pub s: String,
}


#[derive(Debug, Clone, Default)]
pub struct RecursiveFoo {
    pub s: String,
    pub child: Vec<RecursiveFoo>,
}

impl RecursiveFoo {
    pub fn new(s: String, child: Vec<RecursiveFoo>) -> Self {
        Self { s, child }
    }
}
// java_glue.rs.in
use crate::*;
use jni_sys::*;
foreign_class!(class Lol {
    self_type Lol;
    constructor  Lol::default() -> Lol;
});

foreign_class!(class RecursiveFoo {
    self_type RecursiveFoo;
    constructor  RecursiveFoo::new(s: String, child: Vec<RecursiveFoo>) -> RecursiveFoo;
});

You will get an error like:

error: failed to run custom build command for `lol2_lib_android2 v0.1.0 (/Users/austin/work/android.debug/my_repo/lol/lib_android2)`

Caused by:
  process didn't exit successfully: `/Users/austin/work/android.debug/lol/target/release/build/lol_lib_android2-e0e77b6c90dc0964/build-script-build` (exit status: 101)
  --- stderr
  error in android bindings: src/java_glue.rs.in
  parsing of android bindings: src/java_glue.rs.in failed
  error: Do not know conversion from Java type to such rust type 'Vec < RecursiveFoo >'
      constructor  RecursiveFoo::new(s: String, child: Vec<RecursiveFoo>) -> RecursiveFoo;
                                                       ^^^^^^^^^^^^^^^^^

  At android bindings: src/java_glue.rs.in:10:53
  thread 'main' panicked at 'explicit panic', /Users/austin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flapigen-0.6.0/src/error.rs:88:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: Recipe `build-android-lib2` failed with exit code 101

but this type is acceptable to Rust if you compile without the build script.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Java/JNICase specific only for Java/JNI interface generationenhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions