Skip to content

Commit

Permalink
feat: added uuid feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaarf committed Sep 22, 2024
1 parent 006d76e commit 66e8100
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ edition = "2021"
[dependencies]
jni-toolbox-macro = "0.1.3"
jni = "0.21"
uuid = { version = "1.10", optional = true }
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
type T;

Expand Down Expand Up @@ -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())
}
}

0 comments on commit 66e8100

Please sign in to comment.