Skip to content

Commit

Permalink
Migrate away from once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Dec 17, 2024
1 parent 4732193 commit f14612a
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 54 deletions.
3 changes: 1 addition & 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ indicatif = { version = "0.17.8", features = ["rayon"] }
intl-memoizer = "0.5.2"
itertools = "0.13.0"
log = "0.4.22"
once_cell = "1.19.0"
opener = "0.7.2"
rayon = "1.10.0"
regashii = "0.2.0"
Expand Down
32 changes: 19 additions & 13 deletions src/gui/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,25 @@ pub fn text<'a>(content: impl iced::widget::text::IntoFragment<'a>) -> Text<'a>
}

pub mod id {
use once_cell::sync::Lazy;

pub static BACKUP_SCROLL: Lazy<iced::widget::scrollable::Id> = Lazy::new(iced::widget::scrollable::Id::unique);
pub static RESTORE_SCROLL: Lazy<iced::widget::scrollable::Id> = Lazy::new(iced::widget::scrollable::Id::unique);
pub static CUSTOM_GAMES_SCROLL: Lazy<iced::widget::scrollable::Id> =
Lazy::new(iced::widget::scrollable::Id::unique);
pub static OTHER_SCROLL: Lazy<iced::widget::scrollable::Id> = Lazy::new(iced::widget::scrollable::Id::unique);
pub static MODAL_SCROLL: Lazy<iced::widget::scrollable::Id> = Lazy::new(iced::widget::scrollable::Id::unique);

pub static BACKUP_SEARCH: Lazy<iced::widget::text_input::Id> = Lazy::new(iced::widget::text_input::Id::unique);
pub static RESTORE_SEARCH: Lazy<iced::widget::text_input::Id> = Lazy::new(iced::widget::text_input::Id::unique);
pub static CUSTOM_GAMES_SEARCH: Lazy<iced::widget::text_input::Id> =
Lazy::new(iced::widget::text_input::Id::unique);
use std::sync::LazyLock;

pub static BACKUP_SCROLL: LazyLock<iced::widget::scrollable::Id> =
LazyLock::new(iced::widget::scrollable::Id::unique);
pub static RESTORE_SCROLL: LazyLock<iced::widget::scrollable::Id> =
LazyLock::new(iced::widget::scrollable::Id::unique);
pub static CUSTOM_GAMES_SCROLL: LazyLock<iced::widget::scrollable::Id> =
LazyLock::new(iced::widget::scrollable::Id::unique);
pub static OTHER_SCROLL: LazyLock<iced::widget::scrollable::Id> =
LazyLock::new(iced::widget::scrollable::Id::unique);
pub static MODAL_SCROLL: LazyLock<iced::widget::scrollable::Id> =
LazyLock::new(iced::widget::scrollable::Id::unique);

pub static BACKUP_SEARCH: LazyLock<iced::widget::text_input::Id> =
LazyLock::new(iced::widget::text_input::Id::unique);
pub static RESTORE_SEARCH: LazyLock<iced::widget::text_input::Id> =
LazyLock::new(iced::widget::text_input::Id::unique);
pub static CUSTOM_GAMES_SEARCH: LazyLock<iced::widget::text_input::Id> =
LazyLock::new(iced::widget::text_input::Id::unique);

pub fn backup_scroll() -> iced::widget::scrollable::Id {
(*BACKUP_SCROLL).clone()
Expand Down
11 changes: 5 additions & 6 deletions src/lang.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::sync::Mutex;
use std::sync::{LazyLock, Mutex};

use fluent::{bundle::FluentBundle, FluentArgs, FluentResource};
use intl_memoizer::concurrent::IntlLangMemoizer;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use unic_langid::LanguageIdentifier;

Expand Down Expand Up @@ -235,7 +234,7 @@ pub struct Translator {}

static LANGUAGE: Mutex<Language> = Mutex::new(Language::English);

static BUNDLE: Lazy<Mutex<FluentBundle<FluentResource, IntlLangMemoizer>>> = Lazy::new(|| {
static BUNDLE: LazyLock<Mutex<FluentBundle<FluentResource, IntlLangMemoizer>>> = LazyLock::new(|| {
let ftl = include_str!("../lang/en-US.ftl").to_owned();
let res = FluentResource::try_new(ftl).expect("Failed to parse Fluent file content.");

Expand Down Expand Up @@ -286,9 +285,9 @@ fn set_language(language: Language) {
*last_language = language;
}

static RE_EXTRA_SPACES: Lazy<Regex> = Lazy::new(|| Regex::new(r"([^\r\n ]) {2,}").unwrap());
static RE_EXTRA_LINES: Lazy<Regex> = Lazy::new(|| Regex::new(r"([^\r\n ])[\r\n]([^\r\n ])").unwrap());
static RE_EXTRA_PARAGRAPHS: Lazy<Regex> = Lazy::new(|| Regex::new(r"([^\r\n ])[\r\n]{2,}([^\r\n ])").unwrap());
static RE_EXTRA_SPACES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"([^\r\n ]) {2,}").unwrap());
static RE_EXTRA_LINES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"([^\r\n ])[\r\n]([^\r\n ])").unwrap());
static RE_EXTRA_PARAGRAPHS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"([^\r\n ])[\r\n]{2,}([^\r\n ])").unwrap());

fn translate(id: &str) -> String {
translate_args(id, &FluentArgs::new())
Expand Down
19 changes: 9 additions & 10 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::sync::{Arc, Mutex};
use std::sync::{Arc, LazyLock, Mutex};

use filetime::FileTime;
use itertools::Itertools;
use once_cell::sync::Lazy;

use crate::{
prelude::{AnyError, SKIP},
Expand Down Expand Up @@ -39,23 +38,23 @@ impl CommonPath {
Some(path?.to_string_lossy().to_string())
}

static CONFIG: Lazy<Option<String>> = Lazy::new(|| check_dir(dirs::config_dir()));
static DATA: Lazy<Option<String>> = Lazy::new(|| check_dir(dirs::data_dir()));
static DATA_LOCAL: Lazy<Option<String>> = Lazy::new(|| check_dir(dirs::data_local_dir()));
static DOCUMENT: Lazy<Option<String>> = Lazy::new(|| check_dir(dirs::document_dir()));
static HOME: Lazy<Option<String>> = Lazy::new(|| check_dir(dirs::home_dir()));
static PUBLIC: Lazy<Option<String>> = Lazy::new(|| check_dir(dirs::public_dir()));
static CONFIG: LazyLock<Option<String>> = LazyLock::new(|| check_dir(dirs::config_dir()));
static DATA: LazyLock<Option<String>> = LazyLock::new(|| check_dir(dirs::data_dir()));
static DATA_LOCAL: LazyLock<Option<String>> = LazyLock::new(|| check_dir(dirs::data_local_dir()));
static DOCUMENT: LazyLock<Option<String>> = LazyLock::new(|| check_dir(dirs::document_dir()));
static HOME: LazyLock<Option<String>> = LazyLock::new(|| check_dir(dirs::home_dir()));
static PUBLIC: LazyLock<Option<String>> = LazyLock::new(|| check_dir(dirs::public_dir()));

#[cfg(windows)]
static DATA_LOCAL_LOW: Lazy<Option<String>> = Lazy::new(|| {
static DATA_LOCAL_LOW: LazyLock<Option<String>> = LazyLock::new(|| {
known_folders::get_known_folder_path(known_folders::KnownFolder::LocalAppDataLow)
.map(|x| x.to_string_lossy().trim_end_matches(['/', '\\']).to_string())
});
#[cfg(not(windows))]
static DATA_LOCAL_LOW: Option<String> = None;

#[cfg(windows)]
static SAVED_GAMES: Lazy<Option<String>> = Lazy::new(|| {
static SAVED_GAMES: LazyLock<Option<String>> = LazyLock::new(|| {
known_folders::get_known_folder_path(known_folders::KnownFolder::SavedGames)
.map(|x| x.to_string_lossy().trim_end_matches(['/', '\\']).to_string())
});
Expand Down
20 changes: 10 additions & 10 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use std::{
num::NonZeroUsize,
path::PathBuf,
sync::{atomic::AtomicBool, Arc, Mutex},
sync::{atomic::AtomicBool, Arc, LazyLock, Mutex},
};

use itertools::Itertools;
use once_cell::sync::Lazy;

pub use crate::path::StrictPath;
use crate::{path::CommonPath, resource::manifest::Os};

pub static VERSION: Lazy<&'static str> =
Lazy::new(|| option_env!("LUDUSAVI_VERSION").unwrap_or(env!("CARGO_PKG_VERSION")));
pub static USER_AGENT: Lazy<String> = Lazy::new(|| format!("ludusavi/{}", *VERSION));
pub static VERSION: LazyLock<&'static str> =
LazyLock::new(|| option_env!("LUDUSAVI_VERSION").unwrap_or(env!("CARGO_PKG_VERSION")));
pub static USER_AGENT: LazyLock<String> = LazyLock::new(|| format!("ludusavi/{}", *VERSION));
pub static VARIANT: Option<&'static str> = option_env!("LUDUSAVI_VARIANT");
pub static CANONICAL_VERSION: Lazy<(u32, u32, u32)> = Lazy::new(|| {
pub static CANONICAL_VERSION: LazyLock<(u32, u32, u32)> = LazyLock::new(|| {
let version_parts: Vec<u32> = env!("CARGO_PKG_VERSION")
.split('.')
.map(|x| x.parse().unwrap_or(0))
Expand All @@ -35,11 +34,12 @@ pub const LINUX_APP_ID: &str = "com.mtkennerly.ludusavi";
const PORTABLE_FLAG_FILE_NAME: &str = "ludusavi.portable";
pub const INVALID_FILE_CHARS: &[char] = &['\\', '/', ':', '*', '?', '"', '<', '>', '|', '\0'];

pub static STEAM_DECK: Lazy<bool> =
Lazy::new(|| Os::HOST == Os::Linux && StrictPath::new("/home/deck".to_string()).exists());
pub static OS_USERNAME: Lazy<String> = Lazy::new(whoami::username);
pub static STEAM_DECK: LazyLock<bool> =
LazyLock::new(|| Os::HOST == Os::Linux && StrictPath::new("/home/deck".to_string()).exists());
pub static OS_USERNAME: LazyLock<String> = LazyLock::new(whoami::username);

pub static AVAILABLE_PARALELLISM: Lazy<Option<NonZeroUsize>> = Lazy::new(|| std::thread::available_parallelism().ok());
pub static AVAILABLE_PARALELLISM: LazyLock<Option<NonZeroUsize>> =
LazyLock::new(|| std::thread::available_parallelism().ok());

pub static CONFIG_DIR: Mutex<Option<PathBuf>> = Mutex::new(None);
static HANDLER_SIGINT: Mutex<Option<signal_hook::SigId>> = Mutex::new(None);
Expand Down
8 changes: 5 additions & 3 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ mod saves;
mod steam;
mod title;

use std::collections::{HashMap, HashSet};
use std::{
collections::{HashMap, HashSet},
sync::LazyLock,
};

use once_cell::sync::Lazy;
use regex::Regex;

#[allow(unused)]
Expand Down Expand Up @@ -399,7 +401,7 @@ pub fn parse_paths(
.replace(p::HOME, home));
}

static VIRTUALIZED: Lazy<Regex> = Lazy::new(|| {
static VIRTUALIZED: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"^C:[\\/](Program Files|Program Files \(x86\)|Windows|ProgramData)[\\/]"#).unwrap()
});
let expanded: HashSet<_> = paths
Expand Down
20 changes: 11 additions & 9 deletions src/scan/title.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::{BTreeSet, HashMap};
use std::{
collections::{BTreeSet, HashMap},
sync::LazyLock,
};

use once_cell::sync::Lazy;
use regex::Regex;

use crate::{
Expand All @@ -9,16 +11,16 @@ use crate::{
};

/// This covers any edition that is clearly separated by punctuation.
static RE_EDITION_PUNCTUATED: Lazy<Regex> = Lazy::new(|| Regex::new(r#"[™®©:-] .+ edition$"#).unwrap());
static RE_EDITION_PUNCTUATED: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"[™®©:-] .+ edition$"#).unwrap());
/// This covers specific, known editions that are not separated by punctuation.
static RE_EDITION_KNOWN: Lazy<Regex> = Lazy::new(|| Regex::new(r#" (game of the year) edition$"#).unwrap());
static RE_EDITION_KNOWN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" (game of the year) edition$"#).unwrap());
/// This covers any single-word editions that are not separated by punctuation.
/// We can't assume more than one word because it may be part of the main title.
static RE_EDITION_SHORT: Lazy<Regex> = Lazy::new(|| Regex::new(r#" [^ ]+ edition$"#).unwrap());
static RE_YEAR_SUFFIX: Lazy<Regex> = Lazy::new(|| Regex::new(r" \(\d+\)$").unwrap());
static RE_SYMBOLS_GAP: Lazy<Regex> = Lazy::new(|| Regex::new(r#"[™®©:-]"#).unwrap());
static RE_SYMBOLS_NO_GAP: Lazy<Regex> = Lazy::new(|| Regex::new(r#"['"‘’“”]"#).unwrap());
static RE_SPACES: Lazy<Regex> = Lazy::new(|| Regex::new(r#" {2,}"#).unwrap());
static RE_EDITION_SHORT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" [^ ]+ edition$"#).unwrap());
static RE_YEAR_SUFFIX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r" \(\d+\)$").unwrap());
static RE_SYMBOLS_GAP: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"[™®©:-]"#).unwrap());
static RE_SYMBOLS_NO_GAP: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"['"‘’“”]"#).unwrap());
static RE_SPACES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#" {2,}"#).unwrap());

pub fn normalize_title(title: &str) -> String {
let normalized = title.to_lowercase();
Expand Down

0 comments on commit f14612a

Please sign in to comment.