Struct AutoLocal

Source
pub struct AutoLocal<'a, 'b>
where 'a: 'b,
{ /* private fields */ }
Expand description

Auto-delete wrapper for local refs.

Anything passed to a foreign method and returned from JNI methods is considered a local ref unless it is specified otherwise. These refs are automatically deleted once the foreign method exits, but it’s possible that they may reach the JVM-imposed limit before that happens.

This wrapper provides automatic local ref deletion when it goes out of scope.

NOTE: This comes with some potential safety risks. DO NOT use this to wrap something unless you’re SURE it won’t be used after this wrapper gets dropped. Otherwise, you’ll get a nasty JVM crash.

See also the JNI specification for details on referencing Java objects and some extra information.

Implementations§

Source§

impl<'a, 'b> AutoLocal<'a, 'b>

Source

pub fn new(env: &'b JNIEnv<'a>, obj: JObject<'a>) -> AutoLocal<'a, 'b>

Creates a new auto-delete wrapper for a local ref.

Once this wrapper goes out of scope, the delete_local_ref will be called on the object. While wrapped, the object can be accessed via the Deref impl.

Source

pub fn forget(self) -> JObject<'a>

Forget the wrapper, returning the original object.

This prevents delete_local_ref from being called when the AutoLocal gets dropped. You must either remember to delete the local ref manually, or be ok with it getting deleted once the foreign method returns.

Source

pub fn as_obj<'c>(&self) -> JObject<'c>
where 'a: 'c,

Get a reference to the wrapped object

Unlike forget, this ensures the wrapper from being dropped while the returned JObject is still live.

Trait Implementations§

Source§

impl<'a, 'b, 'c> Desc<'a, JClass<'b>> for &'b AutoLocal<'c, '_>
where 'c: 'b,

This conversion assumes that the AutoLocal is a pointer to a class object.

Source§

fn lookup(self, _: &JNIEnv<'a>) -> Result<JClass<'b>, Error>

Look up the concrete type from the JVM.
Source§

impl<'a, 'b> Drop for AutoLocal<'a, 'b>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> From<&'a AutoLocal<'a, '_>> for JObject<'a>

Source§

fn from(other: &'a AutoLocal<'_, '_>) -> JObject<'a>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, 'b> Freeze for AutoLocal<'a, 'b>

§

impl<'a, 'b> RefUnwindSafe for AutoLocal<'a, 'b>

§

impl<'a, 'b> !Send for AutoLocal<'a, 'b>

§

impl<'a, 'b> !Sync for AutoLocal<'a, 'b>

§

impl<'a, 'b> Unpin for AutoLocal<'a, 'b>

§

impl<'a, 'b> UnwindSafe for AutoLocal<'a, 'b>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<'a, T> Desc<'a, T> for T

Source§

fn lookup(self, _: &JNIEnv<'a>) -> Result<T, Error>

Different

Source§

impl<'a, T> Desc<'a, T> for T

Source§

fn lookup(self, _: &JNIEnv<'a>) -> Result<T, Error>

Look up the concrete type from the JVM.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.