Ref

Struct Ref 

Source
pub struct Ref<'env, T: ReferenceType> { /* private fields */ }
Expand description

A non-null, reference to a Java object (+ Env). This may refer to a Local, Global, local Arg, etc.

Ref guarantees not to have a Drop implementation. It does not own the JNI reference.

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 Ref. Of course, there’s no guarantee that’s actually an optimization

Implementations§

Source§

impl<'env, T: ReferenceType> Ref<'env, T>

Source

pub unsafe fn from_raw(env: Env<'env>, object: jobject) -> Self

Wraps an raw JNI reference.

§Safety
  • object is a non-null JNI reference, and it must keep valid under env before the created Ref goes out of scope. This means it should not be a raw pointer managed by Local or any other wrapper that deletes it on dropping.
  • object references an instance of type T.
Source

pub fn env(&self) -> Env<'env>

Gets the Env under which the JNI reference is valid.

Source

pub fn as_raw(&self) -> jobject

Returns the raw JNI reference pointer.

Source

pub fn as_global(&self) -> Global<T>

Returns a new JNI global reference of the same Java object.

Source

pub fn as_local(&self) -> Local<'env, T>

Returns a new JNI local reference of the same Java object.

Source

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.

Source

pub fn is_same_object<O: ReferenceType>(&self, other: &Ref<'_, O>) -> bool

Tests whether two JNI references refer to the same Java object.

Source

pub unsafe fn cast_unchecked<U: ReferenceType>(self) -> Ref<'env, U>

Casts itself to a JNI reference of type U forcefully, without the cost of runtime checking.

§Safety
  • self references an instance of type U.
Source

pub fn cast<U: ReferenceType>(self) -> Result<Ref<'env, U>, CastError>

Tries to cast itself to a JNI reference of type U.

Source

pub fn upcast<U: ReferenceType>(self) -> Ref<'env, U>
where T: AssignableTo<U>,

Casts itself towards a super class type, without the cost of runtime checking.

Source

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
  • self references an instance of type U.
Source

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.

Source

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.

Trait Implementations§

Source§

impl<T: ReferenceType, U: AssignableTo<T>> AsArg<T> for Ref<'_, U>

Source§

fn as_arg(&self) -> jobject

Source§

impl<'env, T: ReferenceType> AsRef<Ref<'env, T>> for Local<'env, T>

Source§

fn as_ref(&self) -> &Ref<'env, T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'env, T: JavaDebug> Debug for Ref<'env, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'env, T: JavaDisplay> Display for Ref<'env, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'env, T: ReferenceType> From<&Ref<'env, T>> for Global<T>

Source§

fn from(x: &Ref<'env, T>) -> Self

Converts to this type from the input type.
Source§

impl<'env, T: ReferenceType> From<&Ref<'env, T>> for Local<'env, T>

Source§

fn from(x: &Ref<'env, T>) -> Self

Converts to this type from the input type.
Source§

impl<'env, T: ReferenceType> From<Ref<'env, T>> for Global<T>

Source§

fn from(x: Ref<'env, T>) -> Self

Converts to this type from the input type.
Source§

impl<'env, T: ReferenceType> From<Ref<'env, T>> for Local<'env, T>

Source§

fn from(x: Ref<'env, T>) -> Self

Converts to this type from the input type.
Source§

impl<'env, T: ReferenceType> Receiver for Ref<'env, T>

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.

Auto Trait Implementations§

§

impl<'env, T> Freeze for Ref<'env, T>

§

impl<'env, T> RefUnwindSafe for Ref<'env, T>
where T: RefUnwindSafe,

§

impl<'env, T> !Send for Ref<'env, T>

§

impl<'env, T> !Sync for Ref<'env, T>

§

impl<'env, T> Unpin for Ref<'env, T>
where T: Unpin,

§

impl<'env, T> !UnwindSafe for Ref<'env, T>

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<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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.