Skip to content

Commit

Permalink
feat: FromJava impl for primitive arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
alemidev committed Sep 24, 2024
1 parent 5259d87 commit d0dbd2d
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions src/from_java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ macro_rules! auto_from_java {
};
}

auto_from_java!(i64, jni::sys::jlong);
auto_from_java!(i32, jni::sys::jint);
auto_from_java!(i16, jni::sys::jshort);
auto_from_java!(i8, jni::sys::jbyte);
auto_from_java!(i16, jni::sys::jshort);
auto_from_java!(i32, jni::sys::jint);
auto_from_java!(i64, jni::sys::jlong);
auto_from_java!(f32, jni::sys::jfloat);
auto_from_java!(f64, jni::sys::jdouble);
auto_from_java!(JObject<'j>, JObject<'j>);
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'j> FromJava<'j> for bool {

#[inline]
fn from_java(_: &mut jni::JNIEnv, value: Self::From) -> Result<Self, jni::errors::Error> {
Ok(value == 1)
Ok(value != 0)
}
}

Expand Down Expand Up @@ -100,6 +100,55 @@ impl<'j, T: FromJava<'j, From = JObject<'j>>> FromJava<'j> for Vec<T> {
}
}

macro_rules! auto_from_java_primitive_array {
($primitive:ty, $fn:ident) => {
impl<'j> FromJava<'j> for Vec<$primitive> {
type From = JPrimitiveArray<'j, $primitive>;

fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::From) -> Result<Self, jni::errors::Error> {
let len = env.get_array_length(&value)?.max(0) as usize; // should be always safe but TODO
let mut out = vec![<$primitive>::default(); len];
env.$fn(value, 0, &mut out)?;
Ok(out)
}
}
};
}

auto_from_java_primitive_array!(i8, get_byte_array_region);
auto_from_java_primitive_array!(i16, get_short_array_region);
auto_from_java_primitive_array!(i32, get_int_array_region);
auto_from_java_primitive_array!(i64, get_long_array_region);
auto_from_java_primitive_array!(f32, get_float_array_region);
auto_from_java_primitive_array!(f64, get_double_array_region);

impl<'j> FromJava<'j> for Vec<bool> {
type From = JPrimitiveArray<'j, u8>;

fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::From) -> Result<Self, jni::errors::Error> {
let len = env.get_array_length(&value)?.max(0) as usize; // should be always safe but TODO
let mut out = vec![<u8>::default(); len];
env.get_boolean_array_region(value, 0, &mut out)?;
Ok(out.into_iter().map(|x| x != 0).collect())
}
}

impl<'j> FromJava<'j> for Vec<char> {
type From = JPrimitiveArray<'j, u16>;

fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::From) -> Result<Self, jni::errors::Error> {
let len = env.get_array_length(&value)?.max(0) as usize; // should be always safe but TODO
let mut out = vec![<u16>::default(); len];
env.get_char_array_region(value, 0, &mut out)?;
Ok(
out
.into_iter()
.map(|x| char::from_u32(x.into()).unwrap_or_default())
.collect()
)
}
}

#[cfg(feature = "uuid")]
impl<'j> FromJava<'j> for uuid::Uuid {
type From = JObject<'j>;
Expand Down

0 comments on commit d0dbd2d

Please sign in to comment.