Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(deps): replace static_init by LazyLock #808

Merged
merged 3 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 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 @@ -21,7 +21,6 @@ no-self-update = []
dark-light = "2"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
static_init = "^1.0"
fern = { version = "^0", features = ["colored"] }
chrono = { version = "^0.4", default-features = false, features = [
"std",
Expand Down
9 changes: 5 additions & 4 deletions src/core/adb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

use regex::Regex;
use serde::{Deserialize, Serialize};
use static_init::dynamic;
use std::sync::LazyLock;
use std::{collections::HashSet, process::Command};

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -182,9 +182,10 @@ impl PackageId {
/// Creates a package-ID if it's valid according to
/// [this](https://developer.android.com/build/configure-app-module#set-application-id)
pub fn new<S: AsRef<str>>(p_id: S) -> Option<Self> {
#[dynamic]
static RE: Regex = Regex::new(r"^[a-zA-Z][a-zA-Z0-9_]*(?:\.[a-zA-Z][a-zA-Z0-9_]*)+$")
.unwrap_or_else(|_| unreachable!());
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^[a-zA-Z][a-zA-Z0-9_]*(?:\.[a-zA-Z][a-zA-Z0-9_]*)+$")
.unwrap_or_else(|_| unreachable!())
});

let p_id = p_id.as_ref();

Expand Down
10 changes: 5 additions & 5 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::gui::views::settings::Settings;
use crate::CACHE_DIR;
use crate::CONFIG_DIR;
use serde::{Deserialize, Serialize};
use static_init::dynamic;
use std::fs;
use std::path::PathBuf;
use std::sync::LazyLock;

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct Config {
Expand Down Expand Up @@ -51,8 +51,7 @@ impl Default for GeneralSettings {
}
}

#[dynamic]
static CONFIG_FILE: PathBuf = CONFIG_DIR.join("config.toml");
static CONFIG_FILE: LazyLock<PathBuf> = LazyLock::new(|| CONFIG_DIR.join("config.toml"));

impl Config {
pub fn save_changes(settings: &Settings, device_id: &String) {
Expand Down Expand Up @@ -87,10 +86,11 @@ impl Config {
}
}

//write unit tests:
#[allow(clippy::unwrap_used)]
#[allow(clippy::unwrap_used, reason = "")]
#[cfg(test)]
mod tests {
//! Unit tests

use super::*;
use std::path::Path;

Expand Down
11 changes: 6 additions & 5 deletions src/core/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ use crate::core::utils::DisplayablePath;
use crate::gui::widgets::package_row::PackageRow;
use crate::CACHE_DIR;
use serde::{Deserialize, Serialize};
use static_init::dynamic;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::{
fs,
path::{Path, PathBuf},
};

#[dynamic]
pub static BACKUP_DIR: PathBuf = CACHE_DIR.join("backups");
pub static BACKUP_DIR: LazyLock<PathBuf> = LazyLock::new(|| CACHE_DIR.join("backups"));

#[derive(Default, Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct PhoneBackup {
Expand Down
6 changes: 3 additions & 3 deletions src/core/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::gui::{views::list::PackageInfo, widgets::package_row::PackageRow};
use regex::Regex;
use retry::{delay::Fixed, retry, OperationResult};
use serde::{Deserialize, Serialize};
use static_init::dynamic;
use std::process::Command;
use std::sync::LazyLock;

#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
Expand Down Expand Up @@ -307,8 +307,8 @@ pub fn is_protected_user<S: AsRef<str>>(user_id: u16, device_serial: S) -> bool

/// `pm list users` parsed into a vector with extra info
pub fn list_users_parsed(device_serial: &str) -> Vec<User> {
#[dynamic]
static RE: Regex = Regex::new(r"\{([0-9]+)").unwrap_or_else(|_| unreachable!());
static RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\{([0-9]+)").unwrap_or_else(|_| unreachable!()));

AdbCommand::new()
.shell(device_serial)
Expand Down
7 changes: 3 additions & 4 deletions src/core/theme.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dark_light;
use iced::{color, Color};
use static_init::dynamic;
use std::sync::LazyLock;

/*
In-memory caching.
Expand All @@ -11,9 +11,8 @@ Coincidentally, this also ensures consistent colors across the GUI,
at the cost of requiring a restart to update the palette.
(this is just a patch, not a fix)
*/
#[dynamic(lazy)]
pub static OS_COLOR_SCHEME: dark_light::Mode =
dark_light::detect().unwrap_or(dark_light::Mode::Unspecified);
pub static OS_COLOR_SCHEME: LazyLock<dark_light::Mode> =
LazyLock::new(|| dark_light::detect().unwrap_or(dark_light::Mode::Unspecified));

#[derive(Default, Debug, PartialEq, Eq, Copy, Clone)]
/// Color scheme
Expand Down
14 changes: 6 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ use fern::{
FormatCallback,
};
use log::Record;
use static_init::dynamic;
use std::path::PathBuf;
use std::{fmt::Arguments, fs::OpenOptions};
use std::sync::LazyLock;
use std::{fmt::Arguments, fs::OpenOptions, path::PathBuf};

mod core;
mod gui;

#[dynamic]
static CONFIG_DIR: PathBuf = setup_uad_dir(&dirs::config_dir().expect("Can't detect config dir"));

#[dynamic]
static CACHE_DIR: PathBuf = setup_uad_dir(&dirs::cache_dir().expect("Can't detect cache dir"));
static CONFIG_DIR: LazyLock<PathBuf> =
LazyLock::new(|| setup_uad_dir(&dirs::config_dir().expect("Can't detect config dir")));
static CACHE_DIR: LazyLock<PathBuf> =
LazyLock::new(|| setup_uad_dir(&dirs::cache_dir().expect("Can't detect cache dir")));

fn main() -> iced::Result {
setup_logger().expect("setup logging");
Expand Down