pub struct Weak<T>{ /* private fields */ }Expand description
A global reference to a Java object that does not prevent it from being garbage collected.
Weak global references have the same properties as ordinary “strong” global references, with one exception: a weak global reference does not prevent the referenced Java object from being garbage collected. In other words, the Java object can be garbage collected even if there is a weak global reference to it.
§Upgrading
Because the Java object referred to by a weak global reference may be
garbage collected at any moment, it cannot be directly used (such as
calling methods on the referenced Java object). Instead, it must first be
upgraded to a local or strong global reference, using the
Weak::upgrade_local or Weak::upgrade_global method,
respectively.
Both upgrade methods return an Option. If, when the upgrade method is
called, the Java object has not yet been garbage collected, then the
Option will be Some containing a newly created strong reference that
can be used as normal. If not, the Option will be None.
Upgrading a weak global reference does not delete it. It is only deleted
when the Weak is dropped, and it can be upgraded more than once.
§Creating and Deleting
To create a weak global reference, use the Env::new_weak_ref method.
To delete it, simply drop the Weak (but be sure to do so on an attached
thread if possible; see the warning below).
It is also possible to create a new JNI weak global reference from an
existing one. To do that, use the Weak::clone_in_jvm method.
§Warning: Drop On an Attached Thread If Possible
When a Weak is dropped, a JNI call is made to delete the global
reference. If this frequently happens on a thread that is not already
attached to the JVM, the thread will be temporarily attached using
JavaVM::attach_current_thread_for_scope, causing a severe performance
penalty.
To avoid this performance penalty, ensure that Weaks are only dropped
on a thread that is already attached (or never dropped at all).
In the event that a global reference is dropped on an unattached thread, a
message is logged at log::Level::Warn.
Implementations§
Source§impl<T> Weak<T>
impl<T> Weak<T>
Sourcepub fn null() -> Self
pub fn null() -> Self
Creates a Global wrapper for a null reference
This is equivalent Weak::default()
A null Weak acts as-if the object has been garbage collected
(Self::is_garbage_collected() will return true).
Sourcepub fn upgrade_local<'local>(
&self,
env: &mut Env<'local>,
) -> Result<Option<T::Kind<'local>>>
pub fn upgrade_local<'local>( &self, env: &mut Env<'local>, ) -> Result<Option<T::Kind<'local>>>
Creates a new local reference to this object.
This returns None if the object has already been garbage collected, otherwise it returns
Some(new_local_reference).
If this method returns Some(r), it is guaranteed that the object will not be garbage
collected at least until r is deleted or becomes invalid.
Sourcepub fn upgrade_global(
&self,
env: &Env<'_>,
) -> Result<Option<Global<T::GlobalKind>>>
pub fn upgrade_global( &self, env: &Env<'_>, ) -> Result<Option<Global<T::GlobalKind>>>
Creates a new strong global reference to this object.
This returns None if the object has already been garbage collected, otherwise it returns
Some(new_local_reference).
If this method returns Some(r), it is guaranteed that the object will not be garbage
collected at least until r is deleted or becomes invalid.
Sourcepub fn is_garbage_collected(&self, env: &Env<'_>) -> Result<bool>
pub fn is_garbage_collected(&self, env: &Env<'_>) -> Result<bool>
Checks if the object referred to by this Weak 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 Weak::upgrade_local or Weak::upgrade_global
will succeed.
This is equivalent to
self.is_same_object(env, JObject::null()).
This may return Error::JavaException if called while there is a pending exception.
Sourcepub fn is_same_object<'local, O>(
&self,
env: &Env<'local>,
object: O,
) -> Result<bool>
👎Deprecated: Use Env::is_same_object
pub fn is_same_object<'local, O>( &self, env: &Env<'local>, object: O, ) -> Result<bool>
Use Env::is_same_object
Returns true if this weak reference refers to the given object. Otherwise returns false.
If object is null, then this method is equivalent to
Weak::is_garbage_collected: it returns true if the object referred to by this
Weak has been garbage collected, or false if the object has not yet been garbage
collected.
Sourcepub fn is_weak_ref_to_same_object(
&self,
env: &Env<'_>,
other: &Self,
) -> Result<bool>
👎Deprecated: Use Env::is_same_object
pub fn is_weak_ref_to_same_object( &self, env: &Env<'_>, other: &Self, ) -> Result<bool>
Use Env::is_same_object
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.
Sourcepub fn clone_in_jvm(
&self,
env: &mut Env<'_>,
) -> Result<Option<Weak<T::GlobalKind>>>
pub fn clone_in_jvm( &self, env: &mut Env<'_>, ) -> Result<Option<Weak<T::GlobalKind>>>
Creates a new weak reference to the same object that this one refers to.
This method returns None if the object has already been garbage collected.
Trait Implementations§
Source§impl<T> Reference for Weak<T>
impl<T> Reference for Weak<T>
Source§type Kind<'env> = <T as Reference>::Kind<'env>
type Kind<'env> = <T as Reference>::Kind<'env>
Self::Kind type corresponds to the underlying
class type (such as JObject or JString), parameterized by the
lifetime that indicates whether the type holds a global reference
('static) or a local reference that’s tied to a JNI stack frame. Read moreSource§type GlobalKind = <T as Reference>::GlobalKind
type GlobalKind = <T as Reference>::GlobalKind
GlobalKind type should be equivalent to
Kind<'static>, with the additional bound that ensures the type is
Send + Sync Read moreSource§fn as_raw(&self) -> jobject
fn as_raw(&self) -> jobject
crate::sys::jobject reference.Source§fn class_name() -> Cow<'static, JNIStr>
fn class_name() -> Cow<'static, JNIStr>
Source§fn lookup_class<'caller>(
env: &Env<'_>,
loader_context: &LoaderContext<'_, '_>,
) -> Result<impl Deref<Target = Global<JClass<'static>>> + 'caller>
fn lookup_class<'caller>( env: &Env<'_>, loader_context: &LoaderContext<'_, '_>, ) -> Result<impl Deref<Target = Global<JClass<'static>>> + 'caller>
Source§unsafe fn kind_from_raw<'env>(local_ref: jobject) -> Self::Kind<'env>
unsafe fn kind_from_raw<'env>(local_ref: jobject) -> Self::Kind<'env>
Self::Kind for the given reference that is tied
to the specified lifetime. Read moreSource§unsafe fn global_kind_from_raw(global_ref: jobject) -> Self::GlobalKind
unsafe fn global_kind_from_raw(global_ref: jobject) -> Self::GlobalKind
Source§fn null<'any>() -> Self::Kind<'any>
fn null<'any>() -> Self::Kind<'any>
null reference based on Self::Kind