0

I'm trying to call a rust library from java package and as part of that, i'm converting (serializing) an object before sending it over the wire.

How ever, I'm getting the following error.

expected `*mut jni::sys::_jobject`, found struct `jni::objects::JPrimitiveArray`

code sample:

fn serialize_response<T: Message>(
    env: &JNIEnv,
    value: T,
) -> Result<jbyteArray, ClientJniError> {
    let serialized = value.write_to_bytes()?;
    Ok(env.byte_array_from_slice(serialized.as_slice())?)
}

What am I doing wrong here?

Similar problem, while trying to deserialize the input.

fn deserialize_from_jni<T: Message>(env: &JNIEnv, buffer: &jbyteArray) -> Result<T, ClientJniError> {
    let buffer_deserialized =
        Message::parse_from_bytes(&*env.convert_byte_array(buffer)?.as_slice())?;
    Ok(buffer_deserialized)
}
Message::parse_from_bytes(&*env.convert_byte_array(buffer)?.as_slice())?;
                                ------------------ ^^^^^^ the trait `std::convert::AsRef<jni::objects::JPrimitiveArray<'_, i8>>` is not implemented for `*mut jni::sys::_jobject`
                                |
                                required by a bound introduced by this call
cafce25
  • 15,907
  • 4
  • 25
  • 31
py_ios_dev
  • 487
  • 1
  • 7
  • 19

2 Answers2

0

Using an older version of jni fixed the issue.

py_ios_dev
  • 487
  • 1
  • 7
  • 19
0

Using version 0.21.1 of the jni crate, your first example can be fixed by calling .as_raw() on the result of env.byte_array_from_slice(...).

The second example can be fixed by calling JByteArray::from_raw(buffer.clone()) in an unsafe block.

Botje
  • 26,269
  • 3
  • 31
  • 41