Skip to content

Commit

Permalink
chore: Rust 1.79と1.80で追加された機能を利用する (#816)
Browse files Browse the repository at this point in the history
* `once_cell::sync::Lazy` → `std::sync::LazyLock`

* `NonNull`に直接生えたメソッドを使う

* `<[[T; N]]>::as_flattened`

* `if`と`match`でのライフタイム延長

* `associated_type_bounds`
  • Loading branch information
qryxip authored Aug 11, 2024
1 parent bf8cdb8 commit 1e75e4e
Show file tree
Hide file tree
Showing 31 changed files with 96 additions and 116 deletions.
5 changes: 0 additions & 5 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/downloader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ futures-util.workspace = true
indicatif.workspace = true
itertools.workspace = true
octocrab = { workspace = true, default-features = false, features = ["rustls-tls", "stream"] }
once_cell.workspace = true
parse-display.workspace = true
rayon.workspace = true
reqwest = { workspace = true, default-features = false, features = ["rustls-tls", "stream"] }
Expand Down
19 changes: 9 additions & 10 deletions crates/downloader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
future::Future,
io::{self, Cursor, Read},
path::{Path, PathBuf},
sync::Arc,
sync::{Arc, LazyLock},
time::Duration,
};

Expand All @@ -24,7 +24,6 @@ use octocrab::{
},
Octocrab,
};
use once_cell::sync::Lazy;
use rayon::iter::{IntoParallelIterator as _, ParallelIterator as _};
use strum::{Display, IntoStaticStr};
use tokio::task::{JoinError, JoinSet};
Expand All @@ -43,7 +42,7 @@ const DEFAULT_CORE_REPO: &str = "VOICEVOX/voicevox_core";
const DEFAULT_ONNXRUNTIME_BUILDER_REPO: &str = "VOICEVOX/onnxruntime-builder";
const DEFAULT_ADDITIONAL_LIBRARIES_REPO: &str = "VOICEVOX/voicevox_additional_libraries";

static OPEN_JTALK_DIC_URL: Lazy<Url> = Lazy::new(|| {
static OPEN_JTALK_DIC_URL: LazyLock<Url> = LazyLock::new(|| {
"https://jaist.dl.sourceforge.net/project/open-jtalk/Dictionary/open_jtalk_dic-1.11/open_jtalk_dic_utf_8-1.11.tar.gz"
.parse()
.unwrap()
Expand Down Expand Up @@ -449,8 +448,8 @@ fn find_onnxruntime(
) -> anyhow::Result<String> {
macro_rules! selector {
($expr:expr $(,)?) => {{
static SELECTOR: Lazy<scraper::Selector> =
Lazy::new(|| scraper::Selector::parse($expr).expect("should be valid"));
static SELECTOR: LazyLock<scraper::Selector> =
LazyLock::new(|| scraper::Selector::parse($expr).expect("should be valid"));
&SELECTOR
}};
}
Expand Down Expand Up @@ -595,8 +594,8 @@ fn add_progress_bar(

const INTERVAL: Duration = Duration::from_millis(100);

static PROGRESS_STYLE: Lazy<ProgressStyle> =
Lazy::new(|| ProgressStyle::with_template("{prefix}").unwrap());
static PROGRESS_STYLE: LazyLock<ProgressStyle> =
LazyLock::new(|| ProgressStyle::with_template("{prefix}").unwrap());
}

async fn download_and_extract(
Expand All @@ -614,15 +613,15 @@ async fn download_and_extract(
let files = &read_archive(archive, archive_kind, pb.clone()).await?;
return extract(files, stripping, output, pb).await;

static PROGRESS_STYLE1: Lazy<ProgressStyle> = Lazy::new(|| {
static PROGRESS_STYLE1: LazyLock<ProgressStyle> = LazyLock::new(|| {
ProgressStyle::with_template(
"{prefix:55} {bytes:>11} {bytes_per_sec:>13} {elapsed_precise} {bar} {percent:>3}%",
)
.unwrap()
});

static PROGRESS_STYLE2: Lazy<ProgressStyle> =
Lazy::new(|| ProgressStyle::with_template("{prefix:55} {spinner} {msg}").unwrap());
static PROGRESS_STYLE2: LazyLock<ProgressStyle> =
LazyLock::new(|| ProgressStyle::with_template("{prefix:55} {spinner} {msg}").unwrap());

async fn with_style(
pb: ProgressBar,
Expand Down
1 change: 0 additions & 1 deletion crates/test_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ edition.workspace = true

[dependencies]
libloading.workspace = true
once_cell.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true

Expand Down
4 changes: 2 additions & 2 deletions crates/test_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod c_api {
pub const VV_MODELS_ROOT_DIR: &str = super::VV_MODELS_ROOT_DIR;
}

use once_cell::sync::Lazy;
use std::sync::LazyLock;

pub use self::typing::{
DecodeExampleData, DurationExampleData, ExampleData, IntonationExampleData,
Expand All @@ -36,6 +36,6 @@ const EXAMPLE_DATA_JSON: &str = include_str!(concat!(
"/data/example_data.json"
));

pub static EXAMPLE_DATA: Lazy<ExampleData> = Lazy::new(|| {
pub static EXAMPLE_DATA: LazyLock<ExampleData> = LazyLock::new(|| {
serde_json::from_str(EXAMPLE_DATA_JSON).expect("failed to parse example_data.json")
});
1 change: 0 additions & 1 deletion crates/voicevox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ indexmap = { workspace = true, features = ["serde"] }
itertools.workspace = true
jlabel.workspace = true
ndarray.workspace = true
once_cell.workspace = true
open_jtalk.workspace = true
ouroboros.workspace = true
rayon.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions crates/voicevox_core/src/engine/acoustic_feature_extractor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::LazyLock};

use derive_getters::Getters;
use derive_new::new;
use once_cell::sync::Lazy;
use std::collections::HashMap;

#[rustfmt::skip]
const PHONEME_LIST: &[&str] = &[
Expand Down Expand Up @@ -52,7 +52,7 @@ const PHONEME_LIST: &[&str] = &[
"z",
];

static PHONEME_MAP: Lazy<HashMap<&str, i64>> = Lazy::new(|| {
static PHONEME_MAP: LazyLock<HashMap<&str, i64>> = LazyLock::new(|| {
let mut m = HashMap::new();
for (i, s) in PHONEME_LIST.iter().enumerate() {
m.insert(*s, i as i64);
Expand All @@ -70,8 +70,8 @@ pub(crate) struct OjtPhoneme {
}

impl OjtPhoneme {
pub(crate) fn num_phoneme() -> usize {
PHONEME_MAP.len()
pub(crate) const fn num_phoneme() -> usize {
PHONEME_LIST.len() // == PHONEME_MAP.len()
}

fn space_phoneme() -> String {
Expand Down
11 changes: 7 additions & 4 deletions crates/voicevox_core/src/engine/kana_parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::engine::model::{AccentPhrase, Mora};
use crate::engine::mora_list::MORA_LIST_MINIMUM;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::LazyLock;

use crate::engine::{
model::{AccentPhrase, Mora},
mora_list::MORA_LIST_MINIMUM,
};

const UNVOICE_SYMBOL: char = '_';
const ACCENT_SYMBOL: char = '\'';
Expand All @@ -16,7 +19,7 @@ pub(crate) struct KanaParseError(String);

type KanaParseResult<T> = std::result::Result<T, KanaParseError>;

static TEXT2MORA_WITH_UNVOICE: Lazy<HashMap<String, Mora>> = Lazy::new(|| {
static TEXT2MORA_WITH_UNVOICE: LazyLock<HashMap<String, Mora>> = LazyLock::new(|| {
let mut text2mora_with_unvoice = HashMap::new();
for [text, consonant, vowel] in MORA_LIST_MINIMUM {
let consonant = if !consonant.is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions crates/voicevox_core/src/infer/domains/talk.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::BTreeSet;
use std::{collections::BTreeSet, sync::LazyLock};

use enum_map::Enum;
use macros::{InferenceInputSignature, InferenceOperation, InferenceOutputSignature};
use ndarray::{Array0, Array1, Array2};
use once_cell::sync::Lazy;

use crate::StyleType;

Expand All @@ -17,7 +16,8 @@ impl InferenceDomain for TalkDomain {
type Operation = TalkOperation;

fn style_types() -> &'static BTreeSet<StyleType> {
static STYLE_TYPES: Lazy<BTreeSet<StyleType>> = Lazy::new(|| [StyleType::Talk].into());
static STYLE_TYPES: LazyLock<BTreeSet<StyleType>> =
LazyLock::new(|| [StyleType::Talk].into());
&STYLE_TYPES
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/voicevox_core/src/infer/session_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl<R: InferenceRuntime, D: InferenceDomain> InferenceSessionSet<R, D> {
.iter()
.map(|ParamInfo { name, dt, ndim }| {
let brackets = match *ndim {
Some(ndim) => "[]".repeat(ndim),
None => "[]...".to_owned(),
Some(ndim) => &"[]".repeat(ndim),
None => "[]...",
};
format!("{name}: {dt}{brackets}")
})
Expand All @@ -74,8 +74,7 @@ impl<R: InferenceRuntime, D: InferenceDomain> InferenceSessionSet<R, D> {
impl<R: InferenceRuntime, D: InferenceDomain> InferenceSessionSet<R, D> {
pub(crate) fn get<I>(&self) -> InferenceSessionCell<R, I>
where
I: InferenceInputSignature,
I::Signature: InferenceSignature<Domain = D>,
I: InferenceInputSignature<Signature: InferenceSignature<Domain = D>>,
{
InferenceSessionCell {
inner: self.0[I::Signature::OPERATION].clone(),
Expand Down
7 changes: 4 additions & 3 deletions crates/voicevox_core/src/metas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,13 @@ pub enum StyleType {

#[cfg(test)]
mod tests {
use once_cell::sync::Lazy;
use std::sync::LazyLock;

use serde_json::json;

#[test]
fn merge_works() -> anyhow::Result<()> {
static INPUT: Lazy<serde_json::Value> = Lazy::new(|| {
static INPUT: LazyLock<serde_json::Value> = LazyLock::new(|| {
json!([
{
"name": "B",
Expand Down Expand Up @@ -267,7 +268,7 @@ mod tests {
])
});

static EXPECTED: Lazy<serde_json::Value> = Lazy::new(|| {
static EXPECTED: LazyLock<serde_json::Value> = LazyLock::new(|| {
json!([
{
"name": "A",
Expand Down
15 changes: 6 additions & 9 deletions crates/voicevox_core/src/synthesizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ pub(crate) mod blocking {
} = audio_query;

let accent_phrases = if options.enable_interrogative_upspeak {
adjust_interrogative_accent_phrases(accent_phrases)
&adjust_interrogative_accent_phrases(accent_phrases)
} else {
accent_phrases.clone()
accent_phrases
};

let (flatten_moras, phoneme_data_list) = initial_process(&accent_phrases);
let (flatten_moras, phoneme_data_list) = initial_process(accent_phrases);

let mut phoneme_length_list = vec![*pre_phoneme_length];
let mut f0_list = vec![0.];
Expand Down Expand Up @@ -318,7 +318,7 @@ pub(crate) mod blocking {

let (_, _, vowel_indexes) = split_mora(&phoneme_data_list);

let mut phoneme: Vec<Vec<f32>> = Vec::new();
let mut phoneme = Vec::new();
let mut f0: Vec<f32> = Vec::new();
{
const RATE: f32 = 24000. / 256.;
Expand All @@ -335,7 +335,7 @@ pub(crate) mod blocking {
let phoneme_id = phoneme_data_list[i].phoneme_id();

for _ in 0..phoneme_length {
let mut phonemes_vec = vec![0.; OjtPhoneme::num_phoneme()];
let mut phonemes_vec = [0.; OjtPhoneme::num_phoneme()];
phonemes_vec[phoneme_id as usize] = 1.;
phoneme.push(phonemes_vec)
}
Expand All @@ -352,14 +352,11 @@ pub(crate) mod blocking {
}
}

// 2次元のvectorを1次元に変換し、アドレスを連続させる
let flatten_phoneme = phoneme.into_iter().flatten().collect::<Vec<_>>();

let wave = &self.decode(
f0.len(),
OjtPhoneme::num_phoneme(),
&f0,
&flatten_phoneme,
phoneme.as_flattened(),
style_id,
)?;
return Ok(to_wav(wave, audio_query));
Expand Down
6 changes: 3 additions & 3 deletions crates/voicevox_core/src/user_dict/part_of_speech_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::sync::LazyLock;

use crate::UserDictWordType;

Expand Down Expand Up @@ -30,8 +30,8 @@ pub(super) struct PartOfSpeechDetail {
}

// 元データ: https://github.com/VOICEVOX/voicevox_engine/blob/master/voicevox_engine/part_of_speech_data.py
pub(super) static PART_OF_SPEECH_DETAIL: Lazy<HashMap<UserDictWordType, PartOfSpeechDetail>> =
Lazy::new(|| {
pub(super) static PART_OF_SPEECH_DETAIL: LazyLock<HashMap<UserDictWordType, PartOfSpeechDetail>> =
LazyLock::new(|| {
HashMap::from_iter([
(
UserDictWordType::ProperNoun,
Expand Down
16 changes: 9 additions & 7 deletions crates/voicevox_core/src/user_dict/word.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::{ops::RangeToInclusive, sync::LazyLock};

use regex::Regex;
use serde::{de::Error as _, Deserialize, Serialize};

use crate::{
error::ErrorRepr,
result::Result,
user_dict::part_of_speech_data::{
priority2cost, MAX_PRIORITY, MIN_PRIORITY, PART_OF_SPEECH_DETAIL,
},
};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{de::Error as _, Deserialize, Serialize};
use std::ops::RangeToInclusive;

/// ユーザー辞書の単語。
#[derive(Clone, Debug, Serialize)]
Expand Down Expand Up @@ -77,8 +78,9 @@ impl InvalidWordError {

type InvalidWordResult<T> = std::result::Result<T, InvalidWordError>;

static PRONUNCIATION_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[ァ-ヴー]+$").unwrap());
static MORA_REGEX: Lazy<Regex> = Lazy::new(|| {
static PRONUNCIATION_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[ァ-ヴー]+$").unwrap());
static MORA_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(concat!(
"(?:",
"[イ][ェ]|[ヴ][ャュョ]|[トド][ゥ]|[テデ][ィャュョ]|[デ][ェ]|[クグ][ヮ]|", // rule_others
Expand All @@ -89,7 +91,7 @@ static MORA_REGEX: Lazy<Regex> = Lazy::new(|| {
))
.unwrap()
});
static SPACE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\p{Z}").unwrap());
static SPACE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\p{Z}").unwrap());

impl Default for UserDictWord {
fn default() -> Self {
Expand Down
5 changes: 3 additions & 2 deletions crates/voicevox_core/src/voice_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ pub(crate) mod tokio {

#[cfg(test)]
mod tests {
use once_cell::sync::Lazy;
use std::sync::LazyLock;

use rstest::{fixture, rstest};
use serde_json::json;

Expand Down Expand Up @@ -501,7 +502,7 @@ mod tests {
assert_eq!(expected, actual);
}

static TALK_MANIFEST: Lazy<TalkManifest> = Lazy::new(|| TalkManifest {
static TALK_MANIFEST: LazyLock<TalkManifest> = LazyLock::new(|| TalkManifest {
predict_duration_filename: "".to_owned(),
predict_intonation_filename: "".to_owned(),
decode_filename: "".to_owned(),
Expand Down
Loading

0 comments on commit 1e75e4e

Please sign in to comment.