Skip to content

Commit

Permalink
refactor: remove lazy_static and async_once
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Jan 8, 2024
1 parent 5ee41a8 commit 94792a1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 35 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions src/desktop_file.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
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};

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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
22 changes: 8 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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>>>,
Expand Down Expand Up @@ -193,20 +182,25 @@ 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)
}

/// Gets the `Ironvar` manager singleton.
#[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.
Expand Down

0 comments on commit 94792a1

Please sign in to comment.