Struct java_spaghetti::Env 
source · pub struct Env<'env> { /* private fields */ }Expand description
FFI: Use Env instead of *const JNIEnv. This represents a per-thread Java exection environment.
A “safe” alternative to jni_sys::JNIEnv raw pointers, with the following caveats:
- 
A null env will result in undefined behavior. Java should not be invoking your native functions with a null *mut JNIEnv, however, so I don’t believe this is a problem in practice unless you’ve bindgened the C header definitions elsewhere, calling them (requiring unsafe), and passing null pointers (generally UB for JNI functions anyways, so can be seen as a caller soundness issue.)
- 
Allowing the underlying JNIEnv to be modified is undefined behavior. I don’t believe the JNI libraries modify the JNIEnv, so as long as you’re not accepting a *mut JNIEnv elsewhere, using unsafe to dereference it, and mucking with the methods on it yourself, I believe this “should” be fine. 
§Example
§MainActivity.java
package com.maulingmonkey.example;
public class MainActivity extends androidx.appcompat.app.AppCompatActivity {
    @Override
    public native boolean dispatchKeyEvent(android.view.KeyEvent keyEvent);
    // ...
}
§main_activity.rs
use jni_sys::{jboolean, jobject, JNI_TRUE}; // TODO: Replace with safer equivalent
use java_spaghetti::Env;
#[no_mangle] pub extern "system"
fn Java_com_maulingmonkey_example_MainActivity_dispatchKeyEvent<'env>(
    _env:       Env<'env>,
    _this:      jobject, // TODO: Replace with safer equivalent
    _key_event: jobject  // TODO: Replace with safer equivalent
) -> jboolean {
    // ...
    JNI_TRUE
}