Home > Software engineering >  jni-android-sys: How to convert locals to args
jni-android-sys: How to convert locals to args

Time:03-19

lib.rs:

use jni_glue::Env;
use jni_android_sys;

#[no_mangle]
extern "C" fn for_example(env: Env) {
    let jstr = jni_android_sys::java::lang::String::new(&env).unwrap(); /* fn new<'env>(
                                                                            __jni_env: &'env Env
                                                                        ) -> Result<Local<'env, String>, Local<'env, Throwable>>
                                                                        */
    let stfs = jni_android_sys::android::os::StatFs::new(&env, jstr).unwrap();
                                                                      /* fn new<'env>(
                                                                     __jni_env: &'env Env,
                                                                     arg0: impl Into<Option<&'env String>>
                                                                      ) -> Result<Local<'env, StatFs>, Local<'env, Throwable>>
                                                                      */
}

And i get an error

error[E0277]: the trait bound `Option<&jni_android_sys::java::lang::String>: From<Local<'_, jni_android_sys::java::lang::String>>` is not satisfied
  --> src/lib.rs:9:64
   |
9  |     let stfs = jni_android_sys::android::os::StatFs::new(&env, jstr).unwrap();
   |                -----------------------------------------       ^^^^ the trait `From<Local<'_, jni_android_sys::java::lang::String>>` is not implemented for `Option<&jni_android_sys::java::lang::String>`

In theory, I need to convert Local<'env, String> to &' env String, but how? What does this mean and how to fix it?

CodePudding user response:

A Local<'_, T> implements Deref<Target = T>. So you should be able to dereference it like so:

StatFs::new(&env, &*jstr)
  • Related