Struct jni::objects::WeakRef

source ·
pub struct WeakRef { /* private fields */ }
Expand description

A weak global JVM reference. These are global in scope like GlobalRef, and may outlive the JNIEnv they came from, but are not guaranteed to not get collected until released.

WeakRef can be cloned to use the same weak reference in different contexts. If you want to create yet another weak ref to the same java object, call WeakRef::clone_in_jvm.

Underlying weak reference will be dropped, when the last instance of WeakRef leaves its scope.

It is recommended that a native thread that drops the weak reference is attached to the Java thread (i.e., has an instance of JNIEnv). If the native thread is not attached, the WeakRef#drop will print a warning and implicitly attach and detach it, which significantly affects performance.

Implementations§

source§

impl WeakRef

source

pub fn as_raw(&self) -> jweak

Returns the raw JNI weak reference.

source

pub fn upgrade_local<'local>( &self, env: &JNIEnv<'local> ) -> Result<Option<JObject<'local>>>

Creates a new local reference to this object.

This object may have already been garbage collected by the time this method is called. If so, this method returns Ok(None). Otherwise, it returns Ok(Some(r)) where r is the new local reference.

If this method returns Ok(Some(r)), it is guaranteed that the object will not be garbage collected at least until r is deleted or becomes invalid.

source

pub fn upgrade_global(&self, env: &JNIEnv<'_>) -> Result<Option<GlobalRef>>

Creates a new strong global reference to this object.

This object may have already been garbage collected by the time this method is called. If so, this method returns Ok(None). Otherwise, it returns Ok(Some(r)) where r is the new strong global reference.

If this method returns Ok(Some(r)), it is guaranteed that the object will not be garbage collected at least until r is dropped.

source

pub fn is_garbage_collected(&self, env: &JNIEnv<'_>) -> Result<bool>

Checks if the object referred to by this WeakRef has been garbage collected.

Note that garbage collection can happen at any moment, so a return of Ok(true) from this method does not guarantee that WeakRef::upgrade_local or WeakRef::upgrade_global will succeed.

This is equivalent to self.is_same_object(env, JObject::null()).

source

pub fn is_same_object<'local, O>( &self, env: &JNIEnv<'local>, object: O ) -> Result<bool>where O: AsRef<JObject<'local>>,

Returns true if this weak reference refers to the given object. Otherwise returns false.

If object is null, then this method is equivalent to WeakRef::is_garbage_collected: it returns true if the object referred to by this WeakRef has been garbage collected, or false if the object has not yet been garbage collected.

source

pub fn is_weak_ref_to_same_object( &self, env: &JNIEnv<'_>, other: &WeakRef ) -> Result<bool>

Returns true if this weak reference refers to the same object as another weak reference. Otherwise returns false.

This method will also return true if both weak references refer to an object that has been garbage collected.

source

pub fn clone_in_jvm(&self, env: &JNIEnv<'_>) -> Result<Option<WeakRef>>

Creates a new weak reference to the same object that this one refers to.

WeakRef implements Clone, which should normally be used whenever a new WeakRef to the same object is needed. However, that only increments an internal reference count and does not actually create a new weak reference in the JVM. If you specifically need to have the JVM create a new weak reference, use this method instead of Clone.

This method returns Ok(None) if the object has already been garbage collected.

Trait Implementations§

source§

impl Clone for WeakRef

source§

fn clone(&self) -> WeakRef

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Send for WeakRef

source§

impl Sync for WeakRef

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.