feat: tentative Vec<T> impl for objects

This commit is contained in:
zaaarf 2024-09-22 18:53:39 +02:00
parent 66e810015e
commit a12c26328c
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B

View file

@ -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; type T = jni::sys::jobjectArray;
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> { 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() { for (n, el) in self.into_iter().enumerate() {
let string = env.new_string(el)?; let el = el.into_java(env)?;
env.set_object_array_element(&mut array, n as i32, string)?; env.set_object_array_element(&mut array, n as i32, &el)?;
} }
Ok(array.into_raw()) Ok(array.into_raw())
} }
@ -175,3 +175,15 @@ impl<'j> IntoJava<'j> for uuid::Uuid {
.map(|j| j.as_raw()) .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"
}
}