From d6acbe4875f53b1375fb7a9a46f195b5788489cf Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sun, 7 Jan 2024 23:52:55 +0000 Subject: [PATCH] refactor: remove `lazy_static` and `async_once` --- Cargo.lock | 7 ------- Cargo.toml | 3 --- src/desktop_file.rs | 23 ++++++++++++----------- src/main.rs | 22 ++++++++-------------- 4 files changed, 20 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 97e1b3e8..94995425 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -239,12 +239,6 @@ dependencies = [ "syn 2.0.28", ] -[[package]] -name = "async_once" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ce4f10ea3abcd6617873bae9f91d1c5332b4a778bd9ce34d0cd517474c1de82" - [[package]] name = "atk" version = "0.18.0" @@ -1643,7 +1637,6 @@ checksum = "12b6ee2129af8d4fb011108c73d99a1b83a85977f23b82460c0ae2e25bb4b57f" name = "ironbar" version = "0.14.0-pre" dependencies = [ - "async_once", "cfg-if", "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 3d19df47..1baf3bae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,9 +94,6 @@ smithay-client-toolkit = { version = "0.18.0", default-features = false, feature ] } universal-config = { version = "0.4.3", default_features = false } ctrlc = "3.4.2" - -lazy_static = "1.4.0" -async_once = "0.2.6" cfg-if = "1.0.0" # cli diff --git a/src/desktop_file.rs b/src/desktop_file.rs index 75dfcf0a..2d10bd08 100644 --- a/src/desktop_file.rs +++ b/src/desktop_file.rs @@ -1,9 +1,8 @@ -use lazy_static::lazy_static; use std::collections::{HashMap, HashSet}; use std::env; use std::fs; use std::path::{Path, PathBuf}; -use std::sync::Mutex; +use std::sync::{Mutex, OnceLock}; use tracing::warn; use walkdir::{DirEntry, WalkDir}; @@ -11,13 +10,15 @@ use crate::lock; type DesktopFile = HashMap>; -lazy_static! { - static ref DESKTOP_FILES: Mutex> = - Mutex::new(HashMap::new()); +fn desktop_files() -> &'static Mutex> { + static DESKTOP_FILES: OnceLock>> = OnceLock::new(); + DESKTOP_FILES.get_or_init(|| Mutex::new(HashMap::new())) +} - /// These are the keys that in the cache - static ref DESKTOP_FILES_LOOK_OUT_KEYS: HashSet<&'static str> = - HashSet::from(["Name", "StartupWMClass", "Exec", "Icon"]); +fn desktop_files_look_out_keys() -> &'static HashSet<&'static str> { + static DESKTOP_FILES_LOOK_OUT_KEYS: OnceLock> = OnceLock::new(); + DESKTOP_FILES_LOOK_OUT_KEYS + .get_or_init(|| HashSet::from(["Name", "StartupWMClass", "Exec", "Icon"])) } /// Finds directories that should contain `.desktop` files @@ -104,7 +105,7 @@ fn find_desktop_file_by_filename(app_id: &str, files: &[PathBuf]) -> Option Option { let app_id = &app_id.to_lowercase(); - let mut desktop_files_cache = lock!(DESKTOP_FILES); + let mut desktop_files_cache = lock!(desktop_files()); let files = files .iter() @@ -171,7 +172,7 @@ fn parse_desktop_file(path: &Path) -> Option { let key = key.trim(); let value = value.trim(); - if DESKTOP_FILES_LOOK_OUT_KEYS.contains(key) { + if desktop_files_look_out_keys().contains(key) { Some((key, value)) } else { None @@ -193,7 +194,7 @@ pub fn get_desktop_icon_name(app_id: &str) -> Option { return None; }; - let mut desktop_files_cache = lock!(DESKTOP_FILES); + let mut desktop_files_cache = lock!(desktop_files()); let desktop_file = match desktop_files_cache.get(&path) { Some(desktop_file) => desktop_file, diff --git a/src/main.rs b/src/main.rs index 311e1112..a22197d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::rc::Rc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; #[cfg(feature = "ipc")] use std::sync::RwLock; -use std::sync::{mpsc, Arc}; +use std::sync::{mpsc, Arc, OnceLock}; use cfg_if::cfg_if; #[cfg(feature = "cli")] @@ -89,17 +89,6 @@ fn run_with_args() { } } -static COUNTER: AtomicUsize = AtomicUsize::new(1); - -lazy_static::lazy_static! { - static ref RUNTIME: Arc = Arc::new(create_runtime()); -} - -#[cfg(feature = "ipc")] -lazy_static::lazy_static! { - static ref VARIABLE_MANAGER: Arc> = arc_rw!(VariableManager::new()); -} - #[derive(Debug)] pub struct Ironbar { bars: Rc>>, @@ -193,12 +182,14 @@ impl Ironbar { /// Gets the current Tokio runtime. #[must_use] pub fn runtime() -> Arc { - RUNTIME.clone() + static RUNTIME: OnceLock> = OnceLock::new(); + RUNTIME.get_or_init(|| Arc::new(create_runtime())).clone() } /// Gets a `usize` ID value that is unique to the entire Ironbar instance. /// This is just a static `AtomicUsize` that increments every time this function is called. pub fn unique_id() -> usize { + static COUNTER: AtomicUsize = AtomicUsize::new(1); COUNTER.fetch_add(1, Ordering::Relaxed) } @@ -206,7 +197,10 @@ impl Ironbar { #[cfg(feature = "ipc")] #[must_use] pub fn variable_manager() -> Arc> { - VARIABLE_MANAGER.clone() + static VARIABLE_MANAGER: OnceLock>> = OnceLock::new(); + VARIABLE_MANAGER + .get_or_init(|| arc_rw!(VariableManager::new())) + .clone() } /// Gets a clone of a bar by its unique name.