1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +02:00

refactor: remove lazy_static and async_once

This commit is contained in:
Jake Stanger 2024-01-07 23:52:55 +00:00
parent c702f6fffa
commit 6f531a5654
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
4 changed files with 20 additions and 35 deletions

7
Cargo.lock generated
View file

@ -239,12 +239,6 @@ dependencies = [
"syn 2.0.48",
]
[[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",

View file

@ -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

View file

@ -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<String, Vec<String>>;
lazy_static! {
static ref DESKTOP_FILES: Mutex<HashMap<PathBuf, DesktopFile>> =
Mutex::new(HashMap::new());
fn desktop_files() -> &'static Mutex<HashMap<PathBuf, DesktopFile>> {
static DESKTOP_FILES: OnceLock<Mutex<HashMap<PathBuf, DesktopFile>>> = 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<HashSet<&'static str>> = 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<Path
/// Finds the correct desktop file using the keys in `DESKTOP_FILES_LOOK_OUT_KEYS`
fn find_desktop_file_by_filedata(app_id: &str, files: &[PathBuf]) -> Option<PathBuf> {
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<DesktopFile> {
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<String> {
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,

View file

@ -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<Runtime> = Arc::new(create_runtime());
}
#[cfg(feature = "ipc")]
lazy_static::lazy_static! {
static ref VARIABLE_MANAGER: Arc<RwLock<VariableManager>> = arc_rw!(VariableManager::new());
}
#[derive(Debug)]
pub struct Ironbar {
bars: Rc<RefCell<Vec<Bar>>>,
@ -193,12 +182,14 @@ impl Ironbar {
/// Gets the current Tokio runtime.
#[must_use]
pub fn runtime() -> Arc<Runtime> {
RUNTIME.clone()
static RUNTIME: OnceLock<Arc<Runtime>> = 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<RwLock<VariableManager>> {
VARIABLE_MANAGER.clone()
static VARIABLE_MANAGER: OnceLock<Arc<RwLock<VariableManager>>> = OnceLock::new();
VARIABLE_MANAGER
.get_or_init(|| arc_rw!(VariableManager::new()))
.clone()
}
/// Gets a clone of a bar by its unique name.