From c7c8cf1da79040f0fb16fe5df90122b3b275b52f Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Fri, 20 Sep 2024 07:02:22 +0900 Subject: [PATCH] chore: minor refactor (#833) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 単体でPRを作るほどではない細かいリファクタを行う。 --- crates/voicevox_core/Cargo.toml | 3 -- .../src/engine/acoustic_feature_extractor.rs | 12 ++---- crates/voicevox_core/src/engine/open_jtalk.rs | 37 +++++++++---------- crates/voicevox_core/src/infer/domains.rs | 4 -- crates/voicevox_core/src/synthesizer.rs | 5 ++- crates/voicevox_core/src/voice_model.rs | 22 +---------- crates/voicevox_core_c_api/build.rs | 3 +- crates/voicevox_core_c_api/src/lib.rs | 22 ++++------- .../tests/e2e/snapshots.toml | 5 +-- .../tests/e2e/testcases/user_dict_load.rs | 2 +- crates/voicevox_core_java_api/src/logger.rs | 5 +-- .../python/test/test_nop.py | 8 ---- .../python/voicevox_core/_load_dlls.py | 3 +- 13 files changed, 42 insertions(+), 89 deletions(-) delete mode 100644 crates/voicevox_core_python_api/python/test/test_nop.py diff --git a/crates/voicevox_core/Cargo.toml b/crates/voicevox_core/Cargo.toml index ba508ba44..d5fb28321 100644 --- a/crates/voicevox_core/Cargo.toml +++ b/crates/voicevox_core/Cargo.toml @@ -67,8 +67,5 @@ humansize.workspace = true windows = { workspace = true, features = ["Win32_Foundation", "Win32_Graphics_Dxgi"] } [lints.rust] -# FIXME: `unsafe impl Send`のもあるが、以下2つのマージにより消える予定 -# * https://github.com/VOICEVOX/voicevox_core/pull/725 -# * https://github.com/VOICEVOX/voicevox_core/pull/772 unsafe_code = "allow" # WindowsのGPU情報表示に、Win32を利用 rust_2018_idioms = "warn" diff --git a/crates/voicevox_core/src/engine/acoustic_feature_extractor.rs b/crates/voicevox_core/src/engine/acoustic_feature_extractor.rs index ca2da4697..02b66f903 100644 --- a/crates/voicevox_core/src/engine/acoustic_feature_extractor.rs +++ b/crates/voicevox_core/src/engine/acoustic_feature_extractor.rs @@ -63,9 +63,6 @@ static PHONEME_MAP: LazyLock> = LazyLock::new(|| { #[derive(Debug, Clone, PartialEq, new, Default, Getters)] pub(crate) struct OjtPhoneme { phoneme: String, - // FIXME: derive-getters(多分)が警告を覆い隠しているが、以下の二つは使っていないはず - start: f32, - end: f32, } impl OjtPhoneme { @@ -113,8 +110,8 @@ mod tests { fn base_hello_hiho() -> Vec { STR_HELLO_HIHO .split_whitespace() - .enumerate() - .map(|(i, s)| OjtPhoneme::new(s.into(), i as f32, (i + 1) as f32)) + .map(ToOwned::to_owned) + .map(OjtPhoneme::new) .collect() } @@ -154,9 +151,8 @@ mod tests { } #[rstest] - #[case(ojt_hello_hiho(), 9, OjtPhoneme::new("a".into(), 9., 10.), true)] - #[case(ojt_hello_hiho(), 9, OjtPhoneme::new("k".into(), 9., 10.), false)] - #[case(ojt_hello_hiho(), 9, OjtPhoneme::new("a".into(), 10., 11.), false)] + #[case(ojt_hello_hiho(), 9, OjtPhoneme::new("a".into()), true)] + #[case(ojt_hello_hiho(), 9, OjtPhoneme::new("k".into()), false)] fn test_ojt_phoneme_equality( #[case] ojt_phonemes: Vec, #[case] index: usize, diff --git a/crates/voicevox_core/src/engine/open_jtalk.rs b/crates/voicevox_core/src/engine/open_jtalk.rs index f27e9b0a6..3cad9c99a 100644 --- a/crates/voicevox_core/src/engine/open_jtalk.rs +++ b/crates/voicevox_core/src/engine/open_jtalk.rs @@ -48,26 +48,23 @@ pub(crate) mod blocking { pub fn new(open_jtalk_dict_dir: impl AsRef) -> crate::result::Result { let dict_dir = open_jtalk_dict_dir.as_ref().to_owned(); - // FIXME: この`{}`はGitのdiffを抑えるためだけに存在 - { - let mut resources = Resources { - mecab: ManagedResource::initialize(), - njd: ManagedResource::initialize(), - jpcommon: ManagedResource::initialize(), - }; - - // FIXME: 「システム辞書を読もうとしたけど読めなかった」というエラーをちゃんと用意する - resources - .mecab - .load(&*dict_dir) - .inspect_err(|e| tracing::error!("{e:?}")) - .map_err(|_| ErrorRepr::NotLoadedOpenjtalkDict)?; - - Ok(Self(Arc::new(Inner { - resources: Mutex::new(resources), - dict_dir, - }))) - } + let mut resources = Resources { + mecab: ManagedResource::initialize(), + njd: ManagedResource::initialize(), + jpcommon: ManagedResource::initialize(), + }; + + // FIXME: 「システム辞書を読もうとしたけど読めなかった」というエラーをちゃんと用意する + resources + .mecab + .load(&*dict_dir) + .inspect_err(|e| tracing::error!("{e:?}")) + .map_err(|_| ErrorRepr::NotLoadedOpenjtalkDict)?; + + Ok(Self(Arc::new(Inner { + resources: Mutex::new(resources), + dict_dir, + }))) } /// ユーザー辞書を設定する。 diff --git a/crates/voicevox_core/src/infer/domains.rs b/crates/voicevox_core/src/infer/domains.rs index 5225f2ec3..72e1e0886 100644 --- a/crates/voicevox_core/src/infer/domains.rs +++ b/crates/voicevox_core/src/infer/domains.rs @@ -63,7 +63,3 @@ pub(crate) trait InferenceDomainMapValues { impl InferenceDomainMapValues for (T,) { type Talk = T; } - -impl InferenceDomainMapValues for [A] { - type Talk = A; -} diff --git a/crates/voicevox_core/src/synthesizer.rs b/crates/voicevox_core/src/synthesizer.rs index 045a2d9ea..ee960cd1a 100644 --- a/crates/voicevox_core/src/synthesizer.rs +++ b/crates/voicevox_core/src/synthesizer.rs @@ -1079,8 +1079,9 @@ pub(crate) mod blocking { OjtPhoneme::convert( phoneme_str_list .iter() - .enumerate() - .map(|(i, s)| OjtPhoneme::new(s.as_ref().to_string(), i as f32, i as f32 + 1.)) + .map(AsRef::as_ref) + .map(ToOwned::to_owned) + .map(OjtPhoneme::new) .collect::>() .as_slice(), ) diff --git a/crates/voicevox_core/src/voice_model.rs b/crates/voicevox_core/src/voice_model.rs index cf2c0f078..ef105fae6 100644 --- a/crates/voicevox_core/src/voice_model.rs +++ b/crates/voicevox_core/src/voice_model.rs @@ -509,7 +509,7 @@ pub(crate) mod nonblocking { #[cfg(test)] mod tests { - use rstest::{fixture, rstest}; + use rstest::rstest; use serde_json::json; use crate::{ @@ -556,26 +556,6 @@ mod tests { assert_eq!(expected, actual); } - // FIXME: これ使ってないのでは? - #[fixture] - fn talk_speaker() -> SpeakerMeta { - serde_json::from_value(json!({ - "name": "dummy", - "styles": [ - { - "id": 0, - "name": "style1", - "type": "talk", - "order": 0 - } - ], - "version": "0.0.1", - "speaker_uuid": "574bc678-8370-44be-b941-08e46e7b47d7", - "order": 0 - })) - .unwrap() - } - fn speaker(style_types: &'static [StyleType]) -> SpeakerMeta { let styles = style_types .iter() diff --git a/crates/voicevox_core_c_api/build.rs b/crates/voicevox_core_c_api/build.rs index 6b0934882..28eb7c04c 100644 --- a/crates/voicevox_core_c_api/build.rs +++ b/crates/voicevox_core_c_api/build.rs @@ -1,4 +1,5 @@ -// TODO: voicevox_onnxruntimeになったらやめる +// TODO: #802 の時点でiOS以外不要になっているはずなので、このbuild.rsは丸ごと消す +// (iOSのためにbuild_util/make_ios_xcframework.bashの修正は必要) fn main() { #[cfg(target_os = "linux")] println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN"); diff --git a/crates/voicevox_core_c_api/src/lib.rs b/crates/voicevox_core_c_api/src/lib.rs index 63f344553..a5c440146 100644 --- a/crates/voicevox_core_c_api/src/lib.rs +++ b/crates/voicevox_core_c_api/src/lib.rs @@ -59,8 +59,7 @@ fn init_logger_once() { && anstyle_query::windows::enable_ansi_colors().unwrap_or(true) }; - // FIXME: `try_init` → `init` (subscriberは他に存在しないはずなので) - let _ = tracing_subscriber::fmt() + tracing_subscriber::fmt() .with_env_filter(if env::var_os(EnvFilter::DEFAULT_ENV).is_some() { EnvFilter::from_default_env() } else { @@ -69,7 +68,7 @@ fn init_logger_once() { .with_timer(local_time as fn(&mut Writer<'_>) -> _) .with_ansi(ansi) .with_writer(out) - .try_init(); + .init(); }); fn local_time(wtr: &mut Writer<'_>) -> fmt::Result { @@ -83,13 +82,6 @@ fn init_logger_once() { } } -/* - * Cの関数として公開するための型や関数を定義するこれらの実装はvoicevox_core/publish.rsに定義してある対応する関数にある - * この関数ではvoicevox_core/publish.rsにある対応する関数の呼び出しと、その戻り値をCの形式に変換する処理のみとする - * これはC文脈の処理と実装をわけるためと、内部実装の変更がAPIに影響を与えにくくするためである - * voicevox_core/publish.rsにある対応する関数とはこのファイルに定義してある公開関数からvoicevoxプレフィックスを取り除いた名前の関数である - */ - // TODO: https://github.com/mozilla/cbindgen/issues/927 //#[cfg(feature = "load-onnxruntime")] //pub const VOICEVOX_ONNXRUNTIME_LIB_NAME: &CStr = ..; @@ -392,10 +384,12 @@ pub extern "C" fn voicevox_get_version() -> *const c_char { init_logger_once(); return C_STRING_DROP_CHECKER.blacklist(VERSION).as_ptr(); - // FIXME: 実行時チェックにすることでこの`unsafe`は削れるはず - const VERSION: &CStr = unsafe { - // SAFETY: The package version is a SemVer, so it should not contain '\0' - CStr::from_bytes_with_nul_unchecked(concat!(env!("CARGO_PKG_VERSION"), '\0').as_bytes()) + const VERSION: &CStr = if let Ok(version) = + CStr::from_bytes_with_nul(concat!(env!("CARGO_PKG_VERSION"), '\0').as_bytes()) + { + version + } else { + panic!("`$CARGO_PKG_VERSION` should be a SemVer, so it should not contain `\\0`"); }; } diff --git a/crates/voicevox_core_c_api/tests/e2e/snapshots.toml b/crates/voicevox_core_c_api/tests/e2e/snapshots.toml index 17ccd61f8..eb5911c7e 100644 --- a/crates/voicevox_core_c_api/tests/e2e/snapshots.toml +++ b/crates/voicevox_core_c_api/tests/e2e/snapshots.toml @@ -88,7 +88,7 @@ result_messages.22 = "ユーザー辞書に単語が見つかりませんでし result_messages.23 = "OpenJTalkのユーザー辞書の設定に失敗しました" result_messages.24 = "ユーザー辞書の単語のバリデーションに失敗しました" result_messages.25 = "UUIDの変換に失敗しました" -# FIXME: 26, 27, 28が抜けている +result_messages.28 = "モデルの形式が不正です" result_messages.29 = "推論ライブラリのロードまたは初期化ができませんでした" stderr = ''' {timestamp} INFO ort: Loaded ONNX Runtime dylib with version '{onnxruntime_version}' @@ -180,8 +180,7 @@ stderr.unix = ''' {timestamp} INFO voicevox_core::synthesizer::blocking: CPUを利用します ''' -# FIXME: "user_dict_load"のはず -[user_dict] +[user_dict_load] stderr.windows = ''' {timestamp} INFO ort: Loaded ONNX Runtime dylib with version '{onnxruntime_version}' {windows-video-cards} diff --git a/crates/voicevox_core_c_api/tests/e2e/testcases/user_dict_load.rs b/crates/voicevox_core_c_api/tests/e2e/testcases/user_dict_load.rs index d044962ae..620610ed9 100644 --- a/crates/voicevox_core_c_api/tests/e2e/testcases/user_dict_load.rs +++ b/crates/voicevox_core_c_api/tests/e2e/testcases/user_dict_load.rs @@ -144,7 +144,7 @@ impl assert_cdylib::TestCase for TestCase { } } -static SNAPSHOTS: LazyLock = snapshots::section!(user_dict); +static SNAPSHOTS: LazyLock = snapshots::section!(user_dict_load); #[derive(Deserialize)] struct Snapshots { diff --git a/crates/voicevox_core_java_api/src/logger.rs b/crates/voicevox_core_java_api/src/logger.rs index 30545725e..d7feb6d65 100644 --- a/crates/voicevox_core_java_api/src/logger.rs +++ b/crates/voicevox_core_java_api/src/logger.rs @@ -25,8 +25,7 @@ extern "system" fn Java_jp_hiroshiba_voicevoxcore_Dll_00024LoggerInitializer_ini }; use tracing_subscriber::{fmt::format::Writer, EnvFilter}; - // FIXME: `try_init` → `init` (subscriberは他に存在しないはずなので) - let _ = tracing_subscriber::fmt() + tracing_subscriber::fmt() .with_env_filter(if env::var_os(EnvFilter::DEFAULT_ENV).is_some() { EnvFilter::from_default_env() } else { @@ -36,7 +35,7 @@ extern "system" fn Java_jp_hiroshiba_voicevoxcore_Dll_00024LoggerInitializer_ini .with_timer(local_time as fn(&mut Writer<'_>) -> _) .with_ansi(out().is_terminal() && env_allows_ansi()) .with_writer(out) - .try_init(); + .init(); fn local_time(wtr: &mut Writer<'_>) -> fmt::Result { // ローカル時刻で表示はするが、そのフォーマットはtracing-subscriber本来のものに近いようにする。 diff --git a/crates/voicevox_core_python_api/python/test/test_nop.py b/crates/voicevox_core_python_api/python/test/test_nop.py deleted file mode 100644 index 43956957d..000000000 --- a/crates/voicevox_core_python_api/python/test/test_nop.py +++ /dev/null @@ -1,8 +0,0 @@ -# FIXME: ちゃんとしたテストを用意する - -import conftest # noqa: F401 -import voicevox_core # noqa: F401 - - -def test_nop() -> None: - pass diff --git a/crates/voicevox_core_python_api/python/voicevox_core/_load_dlls.py b/crates/voicevox_core_python_api/python/voicevox_core/_load_dlls.py index 0f9e3d034..2b90b9988 100644 --- a/crates/voicevox_core_python_api/python/voicevox_core/_load_dlls.py +++ b/crates/voicevox_core_python_api/python/voicevox_core/_load_dlls.py @@ -1,4 +1,5 @@ -# TODO: voicevox_onnxruntimeになったらやめる +# TODO: ここは #803 の時点でさほど必要性が無くなっているはずなので、(ドキュメントでの案内 +# はした上で)やめる import glob import platform from ctypes import CDLL