feat: added uuid feature

This commit is contained in:
zaaarf 2024-09-22 16:41:11 +02:00
parent 006d76eea9
commit 66e810015e
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
3 changed files with 41 additions and 0 deletions

7
Cargo.lock generated
View file

@ -58,6 +58,7 @@ version = "0.1.3"
dependencies = [ dependencies = [
"jni", "jni",
"jni-toolbox-macro 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "jni-toolbox-macro 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid",
] ]
[[package]] [[package]]
@ -156,6 +157,12 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "uuid"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314"
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.5.0" version = "2.5.0"

View file

@ -15,3 +15,4 @@ edition = "2021"
[dependencies] [dependencies]
jni-toolbox-macro = "0.1.3" jni-toolbox-macro = "0.1.3"
jni = "0.21" jni = "0.21"
uuid = { version = "1.10", optional = true }

View file

@ -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<Self, jni::errors::Error> {
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> { pub trait IntoJava<'j> {
type T; type T;
@ -142,3 +162,16 @@ impl<'j, T: IntoJava<'j, T = jni::sys::jobject>> IntoJava<'j> for Option<T> {
} }
} }
} }
#[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<Self::T, jni::errors::Error> {
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())
}
}