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.
This commit is contained in:
Jaffar Ashoor 2022-12-17 21:30:04 +03:00 committed by GitHub
parent 90849a067f
commit c3c41a61b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 69 additions and 99 deletions

7
Cargo.lock generated
View file

@ -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",
]

View file

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

View file

@ -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<fancy_regex::Regex> =
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 {

View file

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

View file

@ -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<AnsiCode> = vec!{
#[rustfmt::skip]
static CODE_LIST: Lazy<Vec<AnsiCode>> = 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>;<c>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<HashMap<&'static str, &'static str>> =
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);
}

View file

@ -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<IndexMap<&'static str, String>> = 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 {

View file

@ -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<HashMap<&'static str, char>> = 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<char, ShellError> {
let extensions = Box::new(FileExtensions);

View file

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

View file

@ -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<W, F> {
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<W>(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)
}

View file

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

View file

@ -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<Mutex<()>> = Arc::new(Mutex::new(()));
}
static LOCALE_OVERRIDE_MUTEX: Lazy<Arc<Mutex<()>>> = Lazy::new(Default::default);
/// Run a closure in a fake locale environment.
///