pub struct Local<'env, T: ReferenceType> { /* private fields */ }Expand description
A Local, non-null, reference to a Java object (+ Env) limited to the current thread/stack.
Including the env allows for the convenient execution of methods without having to individually pass the env as
an argument to each and every one. Since this is limited to the current thread/stack, these cannot be sanely stored
in any kind of static storage, nor shared between threads - instead use a Global if you need to do either.
Will DeleteLocalRef when dropped, invalidating the jobject but ensuring threads that rarely or never return to
Java may run without being guaranteed to eventually exhaust their local reference limit. If this is not desired,
convert to a plain Ref with:
let local = Local::leak(local);Not FFI Safe: #[repr(rust)], and exact layout is likely to change - depending on exact features used - in the
future. Specifically, on Android, since we’re guaranteed to only have a single ambient VM, we can likely store the
*const JNIEnv in thread local storage instead of lugging it around in every Local. Of course, there’s no
guarantee that’s actually an optimization…
Implementations§
Source§impl<'env, T: ReferenceType> Local<'env, T>
impl<'env, T: ReferenceType> Local<'env, T>
Sourcepub unsafe fn from_raw(env: Env<'env>, object: jobject) -> Self
pub unsafe fn from_raw(env: Env<'env>, object: jobject) -> Self
Wraps an owned raw JNI local reference, taking the ownership.
§Safety
objectmust be an owned non-null JNI local reference that belongs toenv, not to be deleted by another wrapper.objectreferences an instance of typeT.
Sourcepub fn into_raw(self) -> jobject
pub fn into_raw(self) -> jobject
Leaks the Local and turns it into a raw pointer, preserving the ownership of one JNI
local reference; prevents DeleteLocalRef from being called on dropping. See Local::leak.
Sourcepub fn leak(self) -> Ref<'env, T>
pub fn leak(self) -> Ref<'env, T>
Leaks the Local, prevents DeleteLocalRef from being called on dropping.
If the current thread is a Java thread, it will be freed when the control flow returns to Java; otherwise it will be freed when the native thread exits.
NOTE: some JVM implementations have a strict limitation of local reference capacity, an uncatchable error will be thrown if the capacity is full.
Sourcepub fn as_global(&self) -> Global<T>
pub fn as_global(&self) -> Global<T>
Returns a new JNI global reference of the same Java object.
Sourcepub fn as_return(&self) -> Return<'env, T>
pub fn as_return(&self) -> Return<'env, T>
Creates and leaks a new local reference to be returned from the JNI extern callback function.
It will be freed as soon as the control flow returns to Java.
Sourcepub fn into_return(self) -> Return<'env, T>
pub fn into_return(self) -> Return<'env, T>
Leaks the local reference to be returned from the JNI extern callback function.
It will be freed as soon as the control flow returns to Java.
Sourcepub fn cast<U: ReferenceType>(self) -> Result<Local<'env, U>, CastError>
pub fn cast<U: ReferenceType>(self) -> Result<Local<'env, U>, CastError>
Tries to cast itself to a JNI reference of type U.
Sourcepub fn upcast<U: ReferenceType>(self) -> Local<'env, U>where
T: AssignableTo<U>,
pub fn upcast<U: ReferenceType>(self) -> Local<'env, U>where
T: AssignableTo<U>,
Casts itself towards a super class type, without the cost of runtime checking.
Methods from Deref<Target = Ref<'env, T>>§
Sourcepub fn as_global(&self) -> Global<T>
pub fn as_global(&self) -> Global<T>
Returns a new JNI global reference of the same Java object.
Sourcepub fn as_local(&self) -> Local<'env, T>
pub fn as_local(&self) -> Local<'env, T>
Returns a new JNI local reference of the same Java object.
Sourcepub fn as_monitor(&'env self) -> Monitor<'env, T>
pub fn as_monitor(&'env self) -> Monitor<'env, T>
Enters monitored mode or increments the JNI monitor counter in this thread for the referenced Java object. See Monitor for the limited locking functionality.
Sourcepub fn is_same_object<O: ReferenceType>(&self, other: &Ref<'_, O>) -> bool
pub fn is_same_object<O: ReferenceType>(&self, other: &Ref<'_, O>) -> bool
Tests whether two JNI references refer to the same Java object.
Sourcepub unsafe fn cast_ref_unchecked<U: ReferenceType>(&self) -> &Ref<'env, U>
pub unsafe fn cast_ref_unchecked<U: ReferenceType>(&self) -> &Ref<'env, U>
Casts the borrowed Ref to a JNI reference of type U forcefully, without the cost of runtime checking.
§Safety
selfreferences an instance of typeU.
Sourcepub fn cast_ref<U: ReferenceType>(&self) -> Result<&Ref<'env, U>, CastError>
pub fn cast_ref<U: ReferenceType>(&self) -> Result<&Ref<'env, U>, CastError>
Tries to cast the borrowed Ref to a JNI reference of type U.
Sourcepub fn upcast_ref<U: ReferenceType>(&self) -> &Ref<'env, U>where
T: AssignableTo<U>,
pub fn upcast_ref<U: ReferenceType>(&self) -> &Ref<'env, U>where
T: AssignableTo<U>,
Casts the borrowed Ref towards a super class type, without the cost of runtime checking.