Struct jni::objects::JList

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

Wrapper for JObjects that implement java/util/List. Provides methods to get, add, and remove elements.

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> JList<'local, 'other_local_1, 'obj_ref>

source

pub fn from_env( env: &mut JNIEnv<'local>, obj: &'obj_ref JObject<'other_local_1> ) -> Result<JList<'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>, idx: jint ) -> 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 add(&self, env: &mut JNIEnv<'_>, value: &JObject<'_>) -> Result<()>

Append an element to the list

source

pub fn insert( &self, env: &mut JNIEnv<'_>, idx: jint, value: &JObject<'_> ) -> Result<()>

Insert an element at a specific index

source

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

Remove an element from the list by index

source

pub fn size(&self, env: &mut JNIEnv<'_>) -> Result<jint>

Get the size of the list

source

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

Pop the last element from the list

Note that this calls size() to determine the last index.

source

pub fn iter<'list>( &'list self, env: &mut JNIEnv<'_> ) -> Result<JListIter<'list, 'local, 'obj_ref, 'other_local_1>>

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 = list.iter(env)?;

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

    // Do something with `obj` here.
}

Each call to next creates a new local reference. To prevent excessive memory usage or overflow error, the local reference should be deleted using JNIEnv::delete_local_ref or JNIEnv::auto_local before the next loop iteration. Alternatively, if the list 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<JList<'local, 'other_local_1, 'obj_ref>> for JList<'local, 'other_local_1, 'obj_ref>

source§

fn as_ref(&self) -> &JList<'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 JList<'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 JList<'local, 'other_local_1, 'obj_ref>

§

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

§

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

§

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

§

impl<'local, 'other_local_1, 'obj_ref> UnwindSafe for JList<'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.