From 66e810015e58ba29e1ffe9ef8b022b402f401814 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Sun, 22 Sep 2024 16:41:11 +0200 Subject: [PATCH] feat: added uuid feature --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e716e7f..d0da809 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,6 +58,7 @@ version = "0.1.3" dependencies = [ "jni", "jni-toolbox-macro 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "uuid", ] [[package]] @@ -156,6 +157,12 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "uuid" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" + [[package]] name = "walkdir" version = "2.5.0" diff --git a/Cargo.toml b/Cargo.toml index 78c600e..dfaee43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ edition = "2021" [dependencies] jni-toolbox-macro = "0.1.3" jni = "0.21" +uuid = { version = "1.10", optional = true } diff --git a/src/lib.rs b/src/lib.rs index 0c9ade2..f8542d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,6 +72,26 @@ impl<'j, T: FromJava<'j, T = jni::objects::JObject<'j>>> FromJava<'j> for Option } } +#[cfg(feature = "uuid")] +impl<'j> FromJava<'j> for uuid::Uuid { + type T = jni::objects::JObject<'j>; + fn from_java(env: &mut jni::JNIEnv<'j>, uuid: Self::T) -> Result { + let lsb = u64::from_ne_bytes( + env.call_method(&uuid, "getLeastSignificantBits", "()J", &[])? + .j()? + .to_ne_bytes() + ); + + let msb = u64::from_ne_bytes( + env.call_method(&uuid, "getMostSignificantBits", "()J", &[])? + .j()? + .to_ne_bytes() + ); + + Ok(uuid::Uuid::from_u64_pair(msb, lsb)) + } +} + pub trait IntoJava<'j> { type T; @@ -142,3 +162,16 @@ impl<'j, T: IntoJava<'j, T = jni::sys::jobject>> IntoJava<'j> for Option { } } } + +#[cfg(feature = "uuid")] +impl<'j> IntoJava<'j> for uuid::Uuid { + type T = jni::sys::jobject; + fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result { + let class = env.find_class("java/util/UUID")?; + let (msb, lsb) = self.as_u64_pair(); + let msb = i64::from_ne_bytes(msb.to_ne_bytes()); + let lsb = i64::from_ne_bytes(lsb.to_ne_bytes()); + env.new_object(&class, "(JJ)V", &[jni::objects::JValueGen::Long(msb), jni::objects::JValueGen::Long(lsb)]) + .map(|j| j.as_raw()) + } +}