Skip to content

Commit

Permalink
feat!: [Rust] SupportedDevices::to_jsonを封印 (#974)
Browse files Browse the repository at this point in the history
他言語との兼ね合いとかを考えると面倒になってきたため。
  • Loading branch information
qryxip authored Feb 7, 2025
1 parent e76856b commit 249bf66
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions crates/voicevox_core/src/__internal/interop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod raii;

pub use crate::{
convert::ToJsonValue,
metas::merge as merge_metas,
synthesizer::{
blocking::PerformInference, DEFAULT_CPU_NUM_THREADS, DEFAULT_ENABLE_INTERROGATIVE_UPSPEAK,
Expand Down
7 changes: 7 additions & 0 deletions crates/voicevox_core/src/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub trait ToJsonValue {
fn to_json_value(&self) -> serde_json::Value;

fn to_json(&self) -> String {
self.to_json_value().to_string()
}
}
6 changes: 5 additions & 1 deletion crates/voicevox_core/src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
use derive_more::BitAnd;
use serde::Serialize;

use crate::convert::ToJsonValue;

pub(crate) fn test_gpus(
gpus: impl IntoIterator<Item = GpuSpec>,
inference_rt_name: &'static str,
Expand Down Expand Up @@ -124,8 +126,10 @@ impl SupportedDevices {
} else {
panic!("either `load-onnxruntime` or `link-onnxruntime` must be enabled");
};
}

pub fn to_json(self) -> serde_json::Value {
impl ToJsonValue for SupportedDevices {
fn to_json_value(&self) -> serde_json::Value {
serde_json::to_value(self).expect("should not fail")
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/voicevox_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const _: () = {
};

mod asyncs;
mod convert;
mod core;
mod devices;
/// cbindgen:ignore
Expand Down
17 changes: 6 additions & 11 deletions crates/voicevox_core_c_api/src/compatible_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use std::{
use libc::c_int;

use tracing::warn;
use voicevox_core::{StyleId, VoiceModelId, __internal::interop::PerformInference as _};
use voicevox_core::{
StyleId, VoiceModelId,
__internal::interop::{PerformInference as _, ToJsonValue as _},
};

use crate::{helpers::display_error, init_logger_once};

Expand Down Expand Up @@ -218,16 +221,8 @@ pub extern "C" fn supported_devices() -> *const c_char {
init_logger_once();
return SUPPORTED_DEVICES.as_ptr();

static SUPPORTED_DEVICES: LazyLock<CString> = LazyLock::new(|| {
CString::new(
ONNXRUNTIME
.supported_devices()
.unwrap()
.to_json()
.to_string(),
)
.unwrap()
});
static SUPPORTED_DEVICES: LazyLock<CString> =
LazyLock::new(|| CString::new(ONNXRUNTIME.supported_devices().unwrap().to_json()).unwrap());
}

/// # Safety
Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use std::sync::Once;
use tracing_subscriber::fmt::format::Writer;
use tracing_subscriber::EnvFilter;
use uuid::Uuid;
use voicevox_core::__internal::interop::ToJsonValue as _;
use voicevox_core::{AccentPhrase, AudioQuery, StyleId, UserDictWord};

fn init_logger_once() {
Expand Down Expand Up @@ -703,8 +704,7 @@ pub unsafe extern "C" fn voicevox_onnxruntime_create_supported_devices_json(
) -> VoicevoxResultCode {
init_logger_once();
into_result_code_with_error((|| {
let supported_devices =
CString::new(onnxruntime.0.supported_devices()?.to_json().to_string()).unwrap();
let supported_devices = CString::new(onnxruntime.0.supported_devices()?.to_json()).unwrap();
output_supported_devices_json.write_unaligned(
C_STRING_DROP_CHECKER
.whitelist(supported_devices)
Expand Down
3 changes: 2 additions & 1 deletion crates/voicevox_core_java_api/src/onnxruntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use jni::{
sys::jobject,
JNIEnv,
};
use voicevox_core::__internal::interop::ToJsonValue as _;

use crate::{common::throw_if_err, object};

Expand Down Expand Up @@ -56,7 +57,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_blocking_Onnxruntime_rs
)?;
let devices = this.supported_devices()?;

assert!(match devices.to_json() {
assert!(match devices.to_json_value() {
serde_json::Value::Object(o) => o.len() == 3, // `cpu`, `cuda`, `dml`
_ => false,
});
Expand Down
3 changes: 2 additions & 1 deletion crates/voicevox_core_python_api/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde_json::json;
use uuid::Uuid;
use voicevox_core::{
AccelerationMode, AccentPhrase, AudioQuery, StyleId, SupportedDevices, VoiceModelMeta,
__internal::interop::ToJsonValue as _,
};

use crate::{
Expand Down Expand Up @@ -280,7 +281,7 @@ pub(crate) impl<T> voicevox_core::Result<T> {
#[ext(SupportedDevicesExt)]
impl SupportedDevices {
pub(crate) fn to_py(self, py: Python<'_>) -> PyResult<&PyAny> {
assert!(match self.to_json() {
assert!(match self.to_json_value() {
serde_json::Value::Object(o) => o.len() == 3, // `cpu`, `cuda`, `dml`
_ => false,
});
Expand Down

0 comments on commit 249bf66

Please sign in to comment.