Skip to content

Commit 66e8100

Browse files
committed
feat: added uuid feature
1 parent 006d76e commit 66e8100

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ edition = "2021"
1515
[dependencies]
1616
jni-toolbox-macro = "0.1.3"
1717
jni = "0.21"
18+
uuid = { version = "1.10", optional = true }

src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ impl<'j, T: FromJava<'j, T = jni::objects::JObject<'j>>> FromJava<'j> for Option
7272
}
7373
}
7474

75+
#[cfg(feature = "uuid")]
76+
impl<'j> FromJava<'j> for uuid::Uuid {
77+
type T = jni::objects::JObject<'j>;
78+
fn from_java(env: &mut jni::JNIEnv<'j>, uuid: Self::T) -> Result<Self, jni::errors::Error> {
79+
let lsb = u64::from_ne_bytes(
80+
env.call_method(&uuid, "getLeastSignificantBits", "()J", &[])?
81+
.j()?
82+
.to_ne_bytes()
83+
);
84+
85+
let msb = u64::from_ne_bytes(
86+
env.call_method(&uuid, "getMostSignificantBits", "()J", &[])?
87+
.j()?
88+
.to_ne_bytes()
89+
);
90+
91+
Ok(uuid::Uuid::from_u64_pair(msb, lsb))
92+
}
93+
}
94+
7595
pub trait IntoJava<'j> {
7696
type T;
7797

@@ -142,3 +162,16 @@ impl<'j, T: IntoJava<'j, T = jni::sys::jobject>> IntoJava<'j> for Option<T> {
142162
}
143163
}
144164
}
165+
166+
#[cfg(feature = "uuid")]
167+
impl<'j> IntoJava<'j> for uuid::Uuid {
168+
type T = jni::sys::jobject;
169+
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
170+
let class = env.find_class("java/util/UUID")?;
171+
let (msb, lsb) = self.as_u64_pair();
172+
let msb = i64::from_ne_bytes(msb.to_ne_bytes());
173+
let lsb = i64::from_ne_bytes(lsb.to_ne_bytes());
174+
env.new_object(&class, "(JJ)V", &[jni::objects::JValueGen::Long(msb), jni::objects::JValueGen::Long(lsb)])
175+
.map(|j| j.as_raw())
176+
}
177+
}

0 commit comments

Comments
 (0)