From c3c41a61b02882c990d679a8fb89a5d4b733e059 Mon Sep 17 00:00:00 2001 From: Jaffar Ashoor <80398773+pinjeff@users.noreply.github.com> Date: Sat, 17 Dec 2022 21:30:04 +0300 Subject: [PATCH] replace lazy_static with once_cell (#7502) replacing the dependence on `lazy_static` with `once_cell`, this will ensure that variables are initialized when needed instead of startup time. --- Cargo.lock | 7 +- crates/nu-cli/Cargo.toml | 2 +- crates/nu-cli/src/repl.rs | 13 ++- crates/nu-command/Cargo.toml | 1 - crates/nu-command/src/platform/ansi/ansi_.rs | 18 ++-- crates/nu-command/src/strings/char_.rs | 10 +-- crates/nu-command/src/viewers/icons.rs | 87 +++++++++---------- crates/nu-json/Cargo.toml | 2 - crates/nu-json/src/ser.rs | 20 ----- crates/nu-test-support/Cargo.toml | 2 +- crates/nu-test-support/src/locale_override.rs | 6 +- 11 files changed, 69 insertions(+), 99 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11db0f1928..3539866838 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2540,7 +2540,6 @@ dependencies = [ "fancy-regex", "fuzzy-matcher", "is_executable", - "lazy_static", "log", "miette", "nu-ansi-term", @@ -2553,6 +2552,7 @@ dependencies = [ "nu-table", "nu-test-support", "nu-utils", + "once_cell", "percent-encoding", "reedline", "rstest", @@ -2606,7 +2606,6 @@ dependencies = [ "indexmap", "is-root", "itertools", - "lazy_static", "libc", "log", "lscolors", @@ -2715,8 +2714,6 @@ dependencies = [ name = "nu-json" version = "0.72.2" dependencies = [ - "fancy-regex", - "lazy_static", "linked-hash-map", "num-traits", "serde", @@ -2843,11 +2840,11 @@ version = "0.72.2" dependencies = [ "getset", "hamcrest2", - "lazy_static", "nu-glob", "nu-path", "nu-utils", "num-format", + "once_cell", "tempfile", ] diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index d082be6cd6..f48f6253c3 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -29,7 +29,7 @@ crossterm = "0.24.0" fancy-regex = "0.10.0" fuzzy-matcher = "0.3.7" is_executable = "1.0.1" -lazy_static = "1.4.0" +once_cell = "1.16.0" log = "0.4" miette = { version = "5.1.0", features = ["fancy-no-backtrace"] } percent-encoding = "2" diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 283a96c072..aef3a56195 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -5,8 +5,6 @@ use crate::{ util::{eval_source, get_guaranteed_cwd, report_error, report_error_new}, NuHighlighter, NuValidator, NushellPrompt, }; -use fancy_regex::Regex; -use lazy_static::lazy_static; use log::{info, trace, warn}; use miette::{IntoDiagnostic, Result}; use nu_color_config::StyleComputer; @@ -1011,11 +1009,12 @@ fn run_ansi_sequence(seq: &str) -> Result<(), ShellError> { }) } -lazy_static! { - // Absolute paths with a drive letter, like 'C:', 'D:\', 'E:\foo' - static ref DRIVE_PATH_REGEX: Regex = - Regex::new(r"^[a-zA-Z]:[/\\]?").expect("Internal error: regex creation"); -} +// Absolute paths with a drive letter, like 'C:', 'D:\', 'E:\foo' +#[cfg(windows)] +static DRIVE_PATH_REGEX: once_cell::sync::Lazy = + once_cell::sync::Lazy::new(|| { + fancy_regex::Regex::new(r"^[a-zA-Z]:[/\\]?").expect("Internal error: regex creation") + }); // A best-effort "does this string look kinda like a path?" function to determine whether to auto-cd fn looks_like_path(orig: &str) -> bool { diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index 81f4aa13ef..98ee7fcae0 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -54,7 +54,6 @@ indexmap = { version="1.7", features=["serde-1"] } Inflector = "0.11" is-root = "0.1.2" itertools = "0.10.0" -lazy_static = "1.4.0" log = "0.4.14" lscolors = { version = "0.12.0", features = ["crossterm"], default-features = false } md5 = { package = "md-5", version = "0.10.0" } diff --git a/crates/nu-command/src/platform/ansi/ansi_.rs b/crates/nu-command/src/platform/ansi/ansi_.rs index e5392f279e..f69f0028ea 100644 --- a/crates/nu-command/src/platform/ansi/ansi_.rs +++ b/crates/nu-command/src/platform/ansi/ansi_.rs @@ -1,10 +1,10 @@ -use lazy_static::lazy_static; use nu_ansi_term::*; use nu_engine::CallExt; use nu_protocol::{ ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, }; +use once_cell::sync::Lazy; use std::collections::HashMap; #[derive(Clone)] @@ -16,8 +16,8 @@ struct AnsiCode { code: String, } -lazy_static! { - static ref CODE_LIST: Vec = vec!{ +#[rustfmt::skip] +static CODE_LIST: Lazy> = Lazy::new(|| { vec![ AnsiCode{ short_name: Some("g"), long_name: "green", code: Color::Green.prefix().to_string()}, AnsiCode{ short_name: Some("gb"), long_name: "green_bold", code: Color::Green.bold().prefix().to_string()}, AnsiCode{ short_name: Some("gu"), long_name: "green_underline", code: Color::Green.underline().prefix().to_string()}, @@ -476,10 +476,12 @@ lazy_static! { // Returns terminal size like "[;R" where r is rows and c is columns // This should work assuming your terminal is not greater than 999x999 - AnsiCode{ short_name: None, long_name:"size", code: "\x1b[s\x1b[999;999H\x1b[6n\x1b[u".to_string()},}; + AnsiCode{ short_name: None, long_name:"size", code: "\x1b[s\x1b[999;999H\x1b[6n\x1b[u".to_string()} + ] +}); - static ref CODE_MAP: HashMap<&'static str, &'static str > = build_ansi_hashmap(&CODE_LIST); -} +static CODE_MAP: Lazy> = + Lazy::new(|| build_ansi_hashmap(&CODE_LIST)); impl Command for AnsiCommand { fn name(&self) -> &str { @@ -783,10 +785,10 @@ fn generate_ansi_code_list( .into_pipeline_data(engine_state.ctrlc.clone())); } -fn build_ansi_hashmap(v: &'static [AnsiCode]) -> HashMap<&'static str, &'static str> { +fn build_ansi_hashmap(v: &[AnsiCode]) -> HashMap<&str, &str> { let mut result = HashMap::new(); for code in v.iter() { - let value: &'static str = &code.code; + let value: &str = &code.code; if let Some(sn) = code.short_name { result.insert(sn, value); } diff --git a/crates/nu-command/src/strings/char_.rs b/crates/nu-command/src/strings/char_.rs index be3b6944f7..89561b3d8a 100644 --- a/crates/nu-command/src/strings/char_.rs +++ b/crates/nu-command/src/strings/char_.rs @@ -1,11 +1,11 @@ use indexmap::indexmap; use indexmap::map::IndexMap; -use lazy_static::lazy_static; use nu_engine::CallExt; use nu_protocol::{ ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, }; +use once_cell::sync::Lazy; // Character used to separate directories in a Path Environment variable on windows is ";" #[cfg(target_family = "windows")] @@ -17,8 +17,8 @@ const ENV_PATH_SEPARATOR_CHAR: char = ':'; #[derive(Clone)] pub struct Char; -lazy_static! { - static ref CHAR_MAP: IndexMap<&'static str, String> = indexmap! { +static CHAR_MAP: Lazy> = Lazy::new(|| { + indexmap! { // These are some regular characters that either can't be used or // it's just easier to use them like this. @@ -146,8 +146,8 @@ lazy_static! { "unit_separator" => '\x1f'.to_string(), "unit_sep" => '\x1f'.to_string(), "us" => '\x1f'.to_string(), - }; -} + } +}); impl Command for Char { fn name(&self) -> &str { diff --git a/crates/nu-command/src/viewers/icons.rs b/crates/nu-command/src/viewers/icons.rs index 3cf684e1a4..dca79a3d83 100644 --- a/crates/nu-command/src/viewers/icons.rs +++ b/crates/nu-command/src/viewers/icons.rs @@ -1,5 +1,5 @@ -use lazy_static::lazy_static; use nu_protocol::{ShellError, Span}; +use once_cell::sync::Lazy; use std::collections::HashMap; use std::path::Path; @@ -85,50 +85,47 @@ impl Icons { // .unwrap_or_default() // } -lazy_static! { - static ref MAP_BY_NAME: HashMap<&'static str, char> = { - let mut m = HashMap::new(); - m.insert(".Trash", '\u{f1f8}'); //  - m.insert(".atom", '\u{e764}'); //  - m.insert(".bashprofile", '\u{e615}'); //  - m.insert(".bashrc", '\u{f489}'); //  - m.insert(".git", '\u{f1d3}'); //  - m.insert(".gitattributes", '\u{f1d3}'); //  - m.insert(".gitconfig", '\u{f1d3}'); //  - m.insert(".github", '\u{f408}'); //  - m.insert(".gitignore", '\u{f1d3}'); //  - m.insert(".gitmodules", '\u{f1d3}'); //  - m.insert(".rvm", '\u{e21e}'); //  - m.insert(".vimrc", '\u{e62b}'); //  - m.insert(".vscode", '\u{e70c}'); //  - m.insert(".zshrc", '\u{f489}'); //  - m.insert("Cargo.lock", '\u{e7a8}'); //  - m.insert("bin", '\u{e5fc}'); //  - m.insert("config", '\u{e5fc}'); //  - m.insert("docker-compose.yml", '\u{f308}'); //  - m.insert("Dockerfile", '\u{f308}'); //  - m.insert("ds_store", '\u{f179}'); //  - m.insert("gitignore_global", '\u{f1d3}'); //  - m.insert("gradle", '\u{e70e}'); //  - m.insert("gruntfile.coffee", '\u{e611}'); //  - m.insert("gruntfile.js", '\u{e611}'); //  - m.insert("gruntfile.ls", '\u{e611}'); //  - m.insert("gulpfile.coffee", '\u{e610}'); //  - m.insert("gulpfile.js", '\u{e610}'); //  - m.insert("gulpfile.ls", '\u{e610}'); //  - m.insert("hidden", '\u{f023}'); //  - m.insert("include", '\u{e5fc}'); //  - m.insert("lib", '\u{f121}'); //  - m.insert("localized", '\u{f179}'); //  - m.insert("Makefile", '\u{e779}'); //  - m.insert("node_modules", '\u{e718}'); //  - m.insert("npmignore", '\u{e71e}'); //  - m.insert("rubydoc", '\u{e73b}'); //  - m.insert("yarn.lock", '\u{e718}'); //  - - m - }; -} +static MAP_BY_NAME: Lazy> = Lazy::new(|| { + HashMap::from([ + (".Trash", '\u{f1f8}'), //  + (".atom", '\u{e764}'), //  + (".bashprofile", '\u{e615}'), //  + (".bashrc", '\u{f489}'), //  + (".git", '\u{f1d3}'), //  + (".gitattributes", '\u{f1d3}'), //  + (".gitconfig", '\u{f1d3}'), //  + (".github", '\u{f408}'), //  + (".gitignore", '\u{f1d3}'), //  + (".gitmodules", '\u{f1d3}'), //  + (".rvm", '\u{e21e}'), //  + (".vimrc", '\u{e62b}'), //  + (".vscode", '\u{e70c}'), //  + (".zshrc", '\u{f489}'), //  + ("Cargo.lock", '\u{e7a8}'), //  + ("bin", '\u{e5fc}'), //  + ("config", '\u{e5fc}'), //  + ("docker-compose.yml", '\u{f308}'), //  + ("Dockerfile", '\u{f308}'), //  + ("ds_store", '\u{f179}'), //  + ("gitignore_global", '\u{f1d3}'), //  + ("gradle", '\u{e70e}'), //  + ("gruntfile.coffee", '\u{e611}'), //  + ("gruntfile.js", '\u{e611}'), //  + ("gruntfile.ls", '\u{e611}'), //  + ("gulpfile.coffee", '\u{e610}'), //  + ("gulpfile.js", '\u{e610}'), //  + ("gulpfile.ls", '\u{e610}'), //  + ("hidden", '\u{f023}'), //  + ("include", '\u{e5fc}'), //  + ("lib", '\u{f121}'), //  + ("localized", '\u{f179}'), //  + ("Makefile", '\u{e779}'), //  + ("node_modules", '\u{e718}'), //  + ("npmignore", '\u{e71e}'), //  + ("rubydoc", '\u{e73b}'), //  + ("yarn.lock", '\u{e718}'), //  + ]) +}); pub fn icon_for_file(file_path: &Path, span: Span) -> Result { let extensions = Box::new(FileExtensions); diff --git a/crates/nu-json/Cargo.toml b/crates/nu-json/Cargo.toml index bdd36e3ad5..8f2e317a6b 100644 --- a/crates/nu-json/Cargo.toml +++ b/crates/nu-json/Cargo.toml @@ -14,8 +14,6 @@ preserve_order = ["linked-hash-map", "linked-hash-map/serde_impl"] default = ["preserve_order"] [dependencies] -fancy-regex = "0.10.0" -lazy_static = "1" linked-hash-map = { version="0.5", optional=true } num-traits = "0.2.14" serde = "1.0" diff --git a/crates/nu-json/src/ser.rs b/crates/nu-json/src/ser.rs index 8f9396cf85..b61e481ae8 100644 --- a/crates/nu-json/src/ser.rs +++ b/crates/nu-json/src/ser.rs @@ -10,12 +10,6 @@ use std::num::FpCategory; use super::error::{Error, ErrorCode, Result}; use serde::ser; -//use super::util::ParseNumber; - -use fancy_regex::Regex; - -use lazy_static::lazy_static; - /// A structure for serializing Rust values into Hjson. pub struct Serializer { writer: W, @@ -839,15 +833,6 @@ where W: io::Write, F: Formatter, { - lazy_static! { - // NEEDS_ESCAPE tests if the string can be written without escapes - static ref NEEDS_ESCAPE: Regex = Regex::new("[\\\\\"\x00-\x1f\x7f-\u{9f}\u{00ad}\u{0600}-\u{0604}\u{070f}\u{17b4}\u{17b5}\u{200c}-\u{200f}\u{2028}-\u{202f}\u{2060}-\u{206f}\u{feff}\u{fff0}-\u{ffff}]").expect("Internal error: json parsing"); - // NEEDS_QUOTES tests if the string can be written as a quoteless string (includes needsEscape but without \\ and \") - static ref NEEDS_QUOTES: Regex = Regex::new("^\\s|^\"|^'''|^#|^/\\*|^//|^\\{|^\\}|^\\[|^\\]|^:|^,|\\s$|[\x00-\x1f\x7f-\u{9f}\u{00ad}\u{0600}-\u{0604}\u{070f}\u{17b4}\u{17b5}\u{200c}-\u{200f}\u{2028}-\u{202f}\u{2060}-\u{206f}\u{feff}\u{fff0}-\u{ffff}]").expect("Internal error: json parsing"); - // starts with a keyword and optionally is followed by a comment - static ref STARTS_WITH_KEYWORD: Regex = Regex::new(r#"^(true|false|null)\s*((,|\]|\}|#|//|/\*).*)?$"#).expect("Internal error: json parsing"); - } - if value.is_empty() { formatter.start_value(wr)?; return escape_bytes(wr, value.as_bytes()); @@ -863,11 +848,6 @@ pub fn escape_key(wr: &mut W, value: &str) -> Result<()> where W: io::Write, { - lazy_static! { - static ref NEEDS_ESCAPE_NAME: Regex = - Regex::new(r#"[,\{\[\}\]\s:#"]|//|/\*|'''|^$"#).expect("Internal error: json parsing"); - } - escape_bytes(wr, value.as_bytes()).map_err(From::from) } diff --git a/crates/nu-test-support/Cargo.toml b/crates/nu-test-support/Cargo.toml index 09fbe53ee4..c29890c77f 100644 --- a/crates/nu-test-support/Cargo.toml +++ b/crates/nu-test-support/Cargo.toml @@ -14,7 +14,7 @@ doctest = false nu-path = { path="../nu-path", version = "0.72.2" } nu-glob = { path = "../nu-glob", version = "0.72.2" } nu-utils = { path="../nu-utils", version = "0.72.2" } -lazy_static = "1.4.0" +once_cell = "1.16.0" num-format = "0.4.3" getset = "0.1.1" diff --git a/crates/nu-test-support/src/locale_override.rs b/crates/nu-test-support/src/locale_override.rs index 953c58063a..7aa0b320f5 100644 --- a/crates/nu-test-support/src/locale_override.rs +++ b/crates/nu-test-support/src/locale_override.rs @@ -2,12 +2,10 @@ use std::sync::{Arc, Mutex}; -use lazy_static::lazy_static; use nu_utils::locale::LOCALE_OVERRIDE_ENV_VAR; +use once_cell::sync::Lazy; -lazy_static! { - static ref LOCALE_OVERRIDE_MUTEX: Arc> = Arc::new(Mutex::new(())); -} +static LOCALE_OVERRIDE_MUTEX: Lazy>> = Lazy::new(Default::default); /// Run a closure in a fake locale environment. ///