Struct VM

Source
pub struct VM(/* private fields */);
Expand description

FFI: Use &VM instead of *const JavaVM. This represents a global, process-wide Java exection environment.

On Android, there is only one VM per-process, although on desktop it’s possible (if rare) to have multiple VMs within the same process. While this library does not yet support having multiple VMs active simultaniously, please don’t hesitate to file an issue if this is an important use case for you.

This is a “safe” alternative to jni_sys::JavaVM raw pointers, with the following caveats:

  1. A null vm will result in undefined behavior. Java should not be invoking your native functions with a null *mut JavaVM, however, so I don’t believe this is a problem in practice unless you’ve bindgened the C header definitions elsewhere, calling them (requiring unsafe), and passing null pointers (generally UB for JNI functions anyways, so can be seen as a caller soundness issue.)

  2. Allowing the underlying JavaVM to be modified is undefined behavior. I don’t believe the JNI libraries modify the JavaVM, so as long as you’re not accepting a *mut JavaVM elsewhere, using unsafe to dereference it, and mucking with the methods on it yourself, I believe this “should” be fine.

Implementations§

Source§

impl VM

Source

pub fn as_java_vm(&self) -> *const JavaVM

Source

pub unsafe fn from_jni_local(vm: &JavaVM) -> &VM

Source

pub fn with_env<F, R>(&self, callback: F) -> R
where F: FnOnce(&Env) -> R,

Trait Implementations§

Source§

impl Send for VM

Source§

impl Sync for VM

Auto Trait Implementations§

§

impl Freeze for VM

§

impl RefUnwindSafe for VM

§

impl Unpin for VM

§

impl UnwindSafe for VM

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, 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.