Struct jni::objects::JMap

source ·
pub struct JMap<'local, 'other_local_1: 'obj_ref, 'obj_ref> { /* private fields */ }
Expand description

Wrapper for JObjects that implement java/util/Map. Provides methods to get and set entries and a way to iterate over key/value pairs.

Looks up the class and method ids on creation rather than for every method call.

Implementations§

source§

impl<'local, 'other_local_1: 'obj_ref, 'obj_ref> JMap<'local, 'other_local_1, 'obj_ref>

source

pub fn from_env( env: &mut JNIEnv<'local>, obj: &'obj_ref JObject<'other_local_1> ) -> Result<JMap<'local, 'other_local_1, 'obj_ref>>

Create a map from the environment and an object. This looks up the necessary class and method ids to call all of the methods on it so that exra work doesn’t need to be done on every method call.

source

pub fn get<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, key: &JObject<'_> ) -> Result<Option<JObject<'other_local_2>>>

Look up the value for a key. Returns Some if it’s found and None if a null pointer would be returned.

source

pub fn put<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, key: &JObject<'_>, value: &JObject<'_> ) -> Result<Option<JObject<'other_local_2>>>

Look up the value for a key. Returns Some with the old value if the key already existed and None if it’s a new key.

source

pub fn remove<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, key: &JObject<'_> ) -> Result<Option<JObject<'other_local_2>>>

Remove a value from the map. Returns Some with the removed value and None if there was no value for the key.

source

pub fn iter<'map, 'iter_local>( &'map self, env: &mut JNIEnv<'iter_local> ) -> Result<JMapIter<'map, 'local, 'other_local_1, 'obj_ref, 'iter_local>>

Get key/value iterator for the map. This is done by getting the EntrySet from java and iterating over it.

The returned iterator does not implement std::iter::Iterator and cannot be used with a for loop. This is because its next method uses a &mut JNIEnv to call the Java iterator. Use a while let loop instead:

let mut iterator = map.iter(env)?;

while let Some((key, value)) = iterator.next(env)? {
    let key: AutoLocal<JObject> = env.auto_local(key);
    let value: AutoLocal<JObject> = env.auto_local(value);

    // Do something with `key` and `value` here.
}

Each call to next creates two new local references. To prevent excessive memory usage or overflow error, the local references should be deleted using JNIEnv::delete_local_ref or JNIEnv::auto_local before the next loop iteration. Alternatively, if the map is known to have a small, predictable size, the loop could be wrapped in JNIEnv::with_local_frame to delete all of the local references at once.

Trait Implementations§

source§

impl<'local, 'other_local_1: 'obj_ref, 'obj_ref> AsRef<JMap<'local, 'other_local_1, 'obj_ref>> for JMap<'local, 'other_local_1, 'obj_ref>

source§

fn as_ref(&self) -> &JMap<'local, 'other_local_1, 'obj_ref>

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

impl<'local, 'other_local_1: 'obj_ref, 'obj_ref> AsRef<JObject<'other_local_1>> for JMap<'local, 'other_local_1, 'obj_ref>

source§

fn as_ref(&self) -> &JObject<'other_local_1>

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

Auto Trait Implementations§

§

impl<'local, 'other_local_1, 'obj_ref> RefUnwindSafe for JMap<'local, 'other_local_1, 'obj_ref>

§

impl<'local, 'other_local_1, 'obj_ref> !Send for JMap<'local, 'other_local_1, 'obj_ref>

§

impl<'local, 'other_local_1, 'obj_ref> !Sync for JMap<'local, 'other_local_1, 'obj_ref>

§

impl<'local, 'other_local_1, 'obj_ref> Unpin for JMap<'local, 'other_local_1, 'obj_ref>where 'other_local_1: 'obj_ref,

§

impl<'local, 'other_local_1, 'obj_ref> UnwindSafe for JMap<'local, 'other_local_1, 'obj_ref>

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