Skip to content

Commit

Permalink
UUIDを分解する部分を修正し、テストも追加
Browse files Browse the repository at this point in the history
以下のためuuidもアップデートする。
<uuid-rs/uuid#741>
  • Loading branch information
qryxip committed May 24, 2024
1 parent f4fb9b5 commit 4b906cd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ tracing = "0.1.37"
tracing-subscriber = "0.3.16"
typetag = "0.2.5"
url = "2.3.0"
uuid = "1.4.0"
uuid = "1.6.1"
voicevox_core = { path = "crates/voicevox_core" }
windows = "0.43.0"
zip = "0.6.3"
Expand Down
2 changes: 2 additions & 0 deletions crates/voicevox_core_java_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ derive_more.workspace = true
easy-ext.workspace = true
jni.workspace = true
once_cell.workspace = true
pretty_assertions = "1.3.0"
rstest.workspace = true
serde_json = { workspace = true, features = ["preserve_order"] }
tracing = { workspace = true, features = ["log"] }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
Expand Down
46 changes: 37 additions & 9 deletions crates/voicevox_core_java_api/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,47 @@ pub(crate) enum JavaApiError {
#[ext(JNIEnvExt)]
pub(crate) impl JNIEnv<'_> {
fn new_uuid(&mut self, uuid: Uuid) -> jni::errors::Result<JObject<'_>> {
let uuid = uuid.as_u128();
let msbs = i64::try_from(uuid >> 64).expect("128 - 64 = 64").into();
let lsbs = (uuid as i64).into();
self.new_object("java/util/UUID", "(JJ)V", &[msbs, lsbs])
let (msbs, lsbs) = split_uuid(uuid);
self.new_object("java/util/UUID", "(JJ)V", &[msbs.into(), lsbs.into()])
}

fn get_uuid(&mut self, obj: &JObject<'_>) -> jni::errors::Result<Uuid> {
let mut get_bits = |name| {
let v = self.call_method(obj, name, "()J", &[])?.j()?;
Ok::<_, jni::errors::Error>(u128::from(v as u64))
};
let mut get_bits = |method_name| self.call_method(obj, method_name, "()J", &[])?.j();
let msbs = get_bits("getMostSignificantBits")?;
let lsbs = get_bits("getLeastSignificantBits")?;
Ok(Uuid::from_u128((msbs << 64) + lsbs))
Ok(construct_uuid(msbs, lsbs))
}
}

fn split_uuid(uuid: Uuid) -> (i64, i64) {
let uuid = uuid.as_u128();
let msbs = (uuid >> 64) as _;
let lsbs = uuid as _;
(msbs, lsbs)
}

fn construct_uuid(msbs: i64, lsbs: i64) -> Uuid {
return Uuid::from_u128((to_u128(msbs) << 64) + to_u128(lsbs));

fn to_u128(bits: i64) -> u128 {
(bits as u64).into()
}
}

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use rstest::rstest;
use uuid::{uuid, Uuid};

#[rstest]
#[case(uuid!("a1a2a3a4-b1b2-c1c2-d1d2-e1e2e3e4e5e6"))]
#[case(uuid!("00000000-0000-0000-0000-000000000000"))]
#[case(uuid!("00000000-0000-0000-ffff-ffffffffffff"))]
#[case(uuid!("ffffffff-ffff-ffff-0000-000000000000"))]
#[case(uuid!("ffffffff-ffff-ffff-ffff-ffffffffffff"))]
fn uuid_conversion_works(#[case] uuid: Uuid) {
let (msbs, lsbs) = super::split_uuid(uuid);
assert_eq!(uuid, super::construct_uuid(msbs, lsbs));
}
}

0 comments on commit 4b906cd

Please sign in to comment.