Skip to content

Commit

Permalink
feat!: Rust APIのnewtypeをopenなstructにし、Rawを消す (#940)
Browse files Browse the repository at this point in the history
#370 から続いている、Rust APIのnewtypeの形を変える。

See-also: https://rust-lang.github.io/api-guidelines/type-safety.html#newtypes-provide-static-distinctions-c-newtype
See-also: #940 (comment)
  • Loading branch information
qryxip authored Jan 25, 2025
1 parent 7ef2cf7 commit ee985ff
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 46 deletions.
7 changes: 2 additions & 5 deletions crates/voicevox_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ pub use self::{
devices::SupportedDevices,
engine::{wav_from_s16le, AccentPhrase, AudioQuery, Mora},
error::{Error, ErrorKind},
metas::{
RawSpeakerVersion, RawStyleId, SpeakerMeta, SpeakerVersion, StyleId, StyleMeta, StyleType,
VoiceModelMeta,
},
metas::{SpeakerMeta, SpeakerVersion, StyleId, StyleMeta, StyleType, VoiceModelMeta},
result::Result,
synthesizer::AccelerationMode,
user_dict::{UserDictWord, UserDictWordType},
version::VERSION,
voice_model::{RawVoiceModelId, VoiceModelId},
voice_model::VoiceModelId,
};
28 changes: 4 additions & 24 deletions crates/voicevox_core/src/metas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ pub fn merge<'a>(metas: impl IntoIterator<Item = &'a SpeakerMeta>) -> Vec<Speake
}
}

/// [`StyleId`]の実体。
///
/// [`StyleId`]: StyleId
pub type RawStyleId = u32;

/// スタイルID。
///
/// VOICEVOXにおける、ある[**話者**(_speaker_)]のある[**スタイル**(_style_)]を指す。
Expand All @@ -62,38 +57,23 @@ pub type RawStyleId = u32;
new,
Debug,
)]
pub struct StyleId(RawStyleId);

impl StyleId {
pub fn raw_id(self) -> RawStyleId {
self.0
}
}
pub struct StyleId(pub u32);

impl Display for StyleId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.raw_id())
write!(f, "{}", self.0)
}
}

/// [`SpeakerVersion`]の実体。
pub type RawSpeakerVersion = String;

/// [**話者**(_speaker_)]のバージョン。
///
/// [**話者**(_speaker_)]: SpeakerMeta
#[derive(PartialEq, Eq, Clone, Ord, PartialOrd, Deserialize, Serialize, new, Debug)]
pub struct SpeakerVersion(RawSpeakerVersion);

impl SpeakerVersion {
pub fn raw_version(&self) -> &RawSpeakerVersion {
&self.0
}
}
pub struct SpeakerVersion(pub String);

impl Display for SpeakerVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.raw_version())
write!(f, "{}", self.0)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl<R: InferenceRuntime> LoadedModels<R> {
.get::<D>()
.as_ref()
.and_then(|(inner_voice_ids, _)| inner_voice_ids.get(&style_id).copied())
.unwrap_or_else(|| InnerVoiceId::new(style_id.raw_id()));
.unwrap_or_else(|| InnerVoiceId::new(style_id.0));

Ok((*model_id, inner_voice_id))
}
Expand Down
13 changes: 1 addition & 12 deletions crates/voicevox_core/src/voice_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ use crate::{
SpeakerMeta, StyleMeta, StyleType, VoiceModelMeta,
};

/// [`VoiceModelId`]の実体。
///
/// [`VoiceModelId`]: VoiceModelId
pub type RawVoiceModelId = Uuid;

pub(crate) type ModelBytesWithInnerVoiceIdsByDomain = inference_domain_map_values!(
for<D> Option<(StyleIdToInnerVoiceId, EnumMap<D::Operation, ModelBytes>)>
);
Expand All @@ -56,13 +51,7 @@ pub(crate) type ModelBytesWithInnerVoiceIdsByDomain = inference_domain_map_value
Debug,
From,
)]
pub struct VoiceModelId(RawVoiceModelId);

impl VoiceModelId {
pub fn raw_voice_model_id(self) -> RawVoiceModelId {
self.0
}
}
pub struct VoiceModelId(pub Uuid);

#[self_referencing]
pub(crate) struct Inner<A: Async> {
Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core_c_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ pub unsafe extern "C" fn voicevox_voice_model_file_id(
output_voice_model_id: NonNull<[u8; 16]>,
) {
init_logger_once();
let id = model.body().id().raw_voice_model_id().into_bytes();
let id = model.body().id().0.into_bytes();
unsafe { output_voice_model_id.write_unaligned(id) };
}

Expand Down
2 changes: 1 addition & 1 deletion crates/voicevox_core_java_api/src/voice_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_blocking_VoiceModelFile
.clone();
let internal = internal.read()?;

let id = env.new_uuid(internal.id().raw_voice_model_id())?;
let id = env.new_uuid(internal.id().0)?;

Ok(id.into_raw())
})
Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core_python_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ mod blocking {
fn open(py: Python<'_>, path: PathBuf) -> PyResult<Self> {
let model = voicevox_core::blocking::VoiceModelFile::open(path).into_py_result(py)?;

let id = crate::convert::to_py_uuid(py, model.id().raw_voice_model_id())?;
let id = crate::convert::to_py_uuid(py, model.id().0)?;
let metas = crate::convert::to_pydantic_voice_model_meta(model.metas(), py)?.into();

let model = Closable::new(model).into();
Expand Down Expand Up @@ -905,7 +905,7 @@ mod asyncio {
let model = voicevox_core::nonblocking::VoiceModelFile::open(path).await;
let (model, id, metas) = Python::with_gil(|py| {
let model = Python::with_gil(|py| model.into_py_result(py))?;
let id = crate::convert::to_py_uuid(py, model.id().raw_voice_model_id())?;
let id = crate::convert::to_py_uuid(py, model.id().0)?;
let metas =
crate::convert::to_pydantic_voice_model_meta(model.metas(), py)?.into();
Ok::<_, PyErr>((model, id, metas))
Expand Down

0 comments on commit ee985ff

Please sign in to comment.