mirror of
https://github.com/hexedtech/jni-toolbox.git
synced 2024-11-25 00:44:53 +01:00
feat: tentative Vec<T> impl for objects
This commit is contained in:
parent
66e810015e
commit
a12c26328c
1 changed files with 16 additions and 4 deletions
20
src/lib.rs
20
src/lib.rs
|
@ -140,14 +140,14 @@ impl<'j> IntoJava<'j> for String {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'j> IntoJava<'j> for Vec<String> {
|
||||
impl<'j, E> IntoJava<'j> for Vec<E> where E: JavaArrayElement<'j> + IntoJava<'j, T: std::convert::AsRef<jni::objects::JObject<'j>>> {
|
||||
type T = jni::sys::jobjectArray;
|
||||
|
||||
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
||||
let mut array = env.new_object_array(self.len() as i32, "java/lang/String", JObject::null())?;
|
||||
let mut array = env.new_object_array(self.len() as i32, E::class(), JObject::null())?;
|
||||
for (n, el) in self.into_iter().enumerate() {
|
||||
let string = env.new_string(el)?;
|
||||
env.set_object_array_element(&mut array, n as i32, string)?;
|
||||
let el = el.into_java(env)?;
|
||||
env.set_object_array_element(&mut array, n as i32, &el)?;
|
||||
}
|
||||
Ok(array.into_raw())
|
||||
}
|
||||
|
@ -175,3 +175,15 @@ impl<'j> IntoJava<'j> for uuid::Uuid {
|
|||
.map(|j| j.as_raw())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait JavaArrayElement<'j> {
|
||||
type T;
|
||||
fn class() -> &'static str;
|
||||
}
|
||||
|
||||
impl<'j> JavaArrayElement<'j> for String {
|
||||
type T = jni::objects::JString<'j>;
|
||||
fn class() -> &'static str {
|
||||
"java/lang/String"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue