From 59fa701ab5956a5de5a2f4463fa1cc20fa9419d5 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sat, 2 Apr 2022 11:01:58 +0200 Subject: [PATCH] lazy_static => once_cell --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cmds.rs | 19 ++++++++----------- src/fmt_replace.rs | 13 +++++++------ src/tree.rs | 32 +++++++++++++++++++------------- src/util.rs | 23 ++++++++++++----------- 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc79fb7..307d034 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -554,8 +554,8 @@ dependencies = [ "clap", "directories", "env_logger", - "lazy_static", "log", + "once_cell", "rand", "regex", "rt-format", diff --git a/Cargo.toml b/Cargo.toml index f7bd171..dc47626 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ swayipc = "3.0.0" toml = "0.5.8" directories = "4.0" regex = "1.5.5" -lazy_static = "1.4.0" rand = "0.8.4" rt-format = "0.3.0" log = "0.4" @@ -25,6 +24,7 @@ env_logger = { version = "0.9.0", default-features = false, features = ["termcol swaybar-types = "3.0.0" chrono = "0.4" sysinfo = "0.23" +once_cell = "1.10.0" [profile.release] lto = "thin" diff --git a/src/cmds.rs b/src/cmds.rs index 2f3a25a..eb09575 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -21,8 +21,9 @@ use crate::tree as t; use crate::tree::NodeMethods; use crate::util; use crate::util::DisplayFormat; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use rand::prelude::SliceRandom; +use regex::Regex; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::sync::atomic; @@ -537,10 +538,8 @@ pub fn focus_win_if_not_focused( } } -lazy_static! { - static ref DIGIT_AND_NAME: regex::Regex = - regex::Regex::new(r"^(\d):(.*)").unwrap(); -} +static DIGIT_AND_NAME: Lazy = + Lazy::new(|| Regex::new(r"^(\d):(.*)").unwrap()); fn create_workspace(ws_name: &str) { if DIGIT_AND_NAME.is_match(ws_name) { @@ -550,12 +549,10 @@ fn create_workspace(ws_name: &str) { } } -lazy_static! { - static ref SPECIAL_WORKSPACE: regex::Regex = - regex::Regex::new(r"^#*w:(.*)").unwrap(); - static ref SPECIAL_SWAY: regex::Regex = - regex::Regex::new(r"^#*s:(.*)").unwrap(); -} +static SPECIAL_WORKSPACE: Lazy = + Lazy::new(|| Regex::new(r"^#*w:(.*)").unwrap()); +static SPECIAL_SWAY: Lazy = + Lazy::new(|| Regex::new(r"^#*s:(.*)").unwrap()); fn chop_workspace_shortcut(input: &str) -> &str { match SPECIAL_WORKSPACE.captures(input) { diff --git a/src/fmt_replace.rs b/src/fmt_replace.rs index 38f4e67..971fc01 100644 --- a/src/fmt_replace.rs +++ b/src/fmt_replace.rs @@ -13,14 +13,15 @@ // You should have received a copy of the GNU General Public License along with // this program. If not, see . -use lazy_static::lazy_static; +use once_cell::sync::Lazy; +use regex::Regex; -lazy_static! { - pub static ref PLACEHOLDER_RX: regex::Regex = regex::Regex::new( - r"\{(?P[^}:]+)(?::(?P\{[^}]*\})(?P[^}]*))?\}" +pub static PLACEHOLDER_RX: Lazy = Lazy::new(|| { + Regex::new( + r"\{(?P[^}:]+)(?::(?P\{[^}]*\})(?P[^}]*))?\}", ) - .unwrap(); -} + .unwrap() +}); pub fn maybe_html_escape(do_it: bool, text: String) -> String { if do_it { diff --git a/src/tree.rs b/src/tree.rs index f706763..68262fa 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -18,25 +18,33 @@ use crate::config; use crate::util; use crate::util::DisplayFormat; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; +use regex::Regex; use serde::{Deserialize, Serialize}; use std::cell::RefCell; use std::cmp; use std::collections::HashMap; use std::rc::Rc; +use std::sync::Mutex; use swayipc as s; +static SWAY_IPC_CONNECTION: Lazy>> = + Lazy::new(|| { + Mutex::new(RefCell::new( + s::Connection::new().expect("Could not open sway IPC connection."), + )) + }); + pub fn get_root_node(include_scratchpad: bool) -> s::Node { - match s::Connection::new() { - Ok(mut con) => { - let mut root = con.get_tree().expect("Got no root node"); - if !include_scratchpad { - root.nodes.retain(|o| !o.is_scratchpad()); - } - root - } + let mut root = match SWAY_IPC_CONNECTION.lock() { + Ok(cell) => cell.borrow_mut().get_tree().expect("Couldn't get tree"), Err(err) => panic!("{}", err), + }; + + if !include_scratchpad { + root.nodes.retain(|o| !o.is_scratchpad()); } + root } /// Immutable Node Iterator @@ -441,10 +449,8 @@ pub fn get_tree<'a>( } } -lazy_static! { - static ref APP_NAME_AND_VERSION_RX: regex::Regex = - regex::Regex::new("(.+)(-[0-9.]+)").unwrap(); -} +static APP_NAME_AND_VERSION_RX: Lazy = + Lazy::new(|| Regex::new("(.+)(-[0-9.]+)").unwrap()); fn format_marks(marks: &[String]) -> String { if marks.is_empty() { diff --git a/src/util.rs b/src/util.rs index bf286c3..7309609 100644 --- a/src/util.rs +++ b/src/util.rs @@ -15,12 +15,15 @@ //! Utility functions including selection between choices using a menu program. +use once_cell::sync::Lazy; +use regex::Regex; + use crate::config as cfg; -use lazy_static::lazy_static; use std::collections::HashMap; use std::io::{BufRead, Write}; use std::path as p; use std::process as proc; +use std::sync::Mutex; pub fn get_swayr_socket_path() -> String { // We prefer checking the env variable instead of @@ -120,12 +123,10 @@ fn find_icon(icon_name: &str, icon_dirs: &[String]) -> Option> { None } -lazy_static! { - static ref WM_CLASS_OR_ICON_RX: regex::Regex = - regex::Regex::new(r"(StartupWMClass|Icon)=(.+)").unwrap(); - static ref REV_DOMAIN_NAME_RX: regex::Regex = - regex::Regex::new(r"^(?:[a-zA-Z0-9-]+\.)+([a-zA-Z0-9-]+)$").unwrap(); -} +static WM_CLASS_OR_ICON_RX: Lazy = + Lazy::new(|| Regex::new(r"(StartupWMClass|Icon)=(.+)").unwrap()); +static REV_DOMAIN_NAME_RX: Lazy = + Lazy::new(|| Regex::new(r"^(?:[a-zA-Z0-9-]+\.)+([a-zA-Z0-9-]+)$").unwrap()); fn get_app_id_to_icon_map( icon_dirs: &[String], @@ -195,10 +196,10 @@ fn get_app_id_to_icon_map( map } -lazy_static! { - static ref APP_ID_TO_ICON_MAP: std::sync::Mutex>>> = - std::sync::Mutex::new(None); -} +// Well, this type definition is pretty useless since it's only used in +// get_icon anyway but clippy suggested it... +type AppIdToIconMap = Lazy>>>>; +static APP_ID_TO_ICON_MAP: AppIdToIconMap = Lazy::new(|| Mutex::new(None)); pub fn get_icon(app_id: &str, icon_dirs: &[String]) -> Option> { let mut opt = APP_ID_TO_ICON_MAP.lock().unwrap();