From e104bccfb94e5572c4c138ce59121ed84456d7d0 Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Tue, 29 Oct 2024 17:33:46 +0100 Subject: [PATCH] Drop `once_cell` dependency (#14198) This PR drops the `once_cell` dependency from all Nu crates, replacing uses of the [`Lazy`](https://docs.rs/once_cell/latest/once_cell/sync/struct.Lazy.html) type with its `std` equivalent, [`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html). --- Cargo.lock | 6 ----- Cargo.toml | 1 - crates/nu-cli/Cargo.toml | 1 - crates/nu-cli/src/repl.rs | 7 +++-- crates/nu-command/Cargo.toml | 1 - .../nu-command/src/conversions/into/value.rs | 27 ++++++++++--------- crates/nu-command/src/platform/ansi/ansi_.rs | 8 +++--- crates/nu-command/src/platform/ulimit.rs | 4 +-- crates/nu-command/src/strings/char_.rs | 6 ++--- .../src/strings/encode_decode/decode.rs | 4 +-- crates/nu-command/src/viewers/icons.rs | 4 +-- crates/nu-explore/Cargo.toml | 1 - crates/nu-explore/src/commands/help.rs | 4 +-- crates/nu-system/Cargo.toml | 1 - crates/nu-system/src/windows.rs | 4 +-- crates/nu-table/Cargo.toml | 1 - crates/nu-table/src/util.rs | 12 +++++---- crates/nu-utils/Cargo.toml | 1 - crates/nu-utils/src/quoting.rs | 4 +-- 19 files changed, 44 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b7f9a1fe6..0237c42ca5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3020,7 +3020,6 @@ dependencies = [ "nu-protocol", "nu-test-support", "nu-utils", - "once_cell", "percent-encoding", "reedline", "rstest", @@ -3164,7 +3163,6 @@ dependencies = [ "num-traits", "nuon", "oem_cp", - "once_cell", "open", "os_pipe", "pathdiff", @@ -3257,7 +3255,6 @@ dependencies = [ "nu-protocol", "nu-table", "nu-utils", - "once_cell", "ratatui", "strip-ansi-escapes", "terminal_size", @@ -3477,7 +3474,6 @@ dependencies = [ "mach2", "nix 0.29.0", "ntapi", - "once_cell", "procfs", "sysinfo 0.32.0", "windows", @@ -3493,7 +3489,6 @@ dependencies = [ "nu-engine", "nu-protocol", "nu-utils", - "once_cell", "tabled", "terminal_size", ] @@ -3528,7 +3523,6 @@ dependencies = [ "lscolors", "nix 0.29.0", "num-format", - "once_cell", "serde", "strip-ansi-escapes", "sys-locale", diff --git a/Cargo.toml b/Cargo.toml index 0390bc956b..de6aa22211 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -119,7 +119,6 @@ num-format = "0.4" num-traits = "0.2" oem_cp = "2.0.0" omnipath = "0.1" -once_cell = "1.20" open = "5.3" os_pipe = { version = "1.2", features = ["io_safety"] } pathdiff = "0.2" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index 9529691c24..0379740168 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -38,7 +38,6 @@ is_executable = { workspace = true } log = { workspace = true } miette = { workspace = true, features = ["fancy-no-backtrace"] } lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] } -once_cell = { workspace = true } percent-encoding = { workspace = true } sysinfo = { workspace = true } unicode-segmentation = { workspace = true } diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 1498d0a572..4eb67f4036 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -1358,10 +1358,9 @@ fn run_finaliziation_ansi_sequence( // 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") - }); +static DRIVE_PATH_REGEX: std::sync::LazyLock = std::sync::LazyLock::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 8d6e704ead..d4d31338b3 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -67,7 +67,6 @@ notify-debouncer-full = { workspace = true, default-features = false } num-format = { workspace = true } num-traits = { workspace = true } oem_cp = { workspace = true } -once_cell = { workspace = true } open = { workspace = true } os_pipe = { workspace = true } pathdiff = { workspace = true } diff --git a/crates/nu-command/src/conversions/into/value.rs b/crates/nu-command/src/conversions/into/value.rs index 99b1daf4b7..2d5ae3b17d 100644 --- a/crates/nu-command/src/conversions/into/value.rs +++ b/crates/nu-command/src/conversions/into/value.rs @@ -1,9 +1,9 @@ use crate::parse_date_from_string; use nu_engine::command_prelude::*; use nu_protocol::PipelineIterator; -use once_cell::sync::Lazy; use regex::{Regex, RegexBuilder}; use std::collections::HashSet; +use std::sync::LazyLock; #[derive(Clone)] pub struct IntoValue; @@ -271,8 +271,9 @@ const DATETIME_DMY_PATTERN: &str = r#"(?x) $ "#; -static DATETIME_DMY_RE: Lazy = - Lazy::new(|| Regex::new(DATETIME_DMY_PATTERN).expect("datetime_dmy_pattern should be valid")); +static DATETIME_DMY_RE: LazyLock = LazyLock::new(|| { + Regex::new(DATETIME_DMY_PATTERN).expect("datetime_dmy_pattern should be valid") +}); const DATETIME_YMD_PATTERN: &str = r#"(?x) ^ ['"]? # optional quotes @@ -297,8 +298,9 @@ const DATETIME_YMD_PATTERN: &str = r#"(?x) ['"]? # optional quotes $ "#; -static DATETIME_YMD_RE: Lazy = - Lazy::new(|| Regex::new(DATETIME_YMD_PATTERN).expect("datetime_ymd_pattern should be valid")); +static DATETIME_YMD_RE: LazyLock = LazyLock::new(|| { + Regex::new(DATETIME_YMD_PATTERN).expect("datetime_ymd_pattern should be valid") +}); //2023-03-24 16:44:17.865147299 -05:00 const DATETIME_YMDZ_PATTERN: &str = r#"(?x) ^ @@ -331,23 +333,24 @@ const DATETIME_YMDZ_PATTERN: &str = r#"(?x) ['"]? # optional quotes $ "#; -static DATETIME_YMDZ_RE: Lazy = - Lazy::new(|| Regex::new(DATETIME_YMDZ_PATTERN).expect("datetime_ymdz_pattern should be valid")); +static DATETIME_YMDZ_RE: LazyLock = LazyLock::new(|| { + Regex::new(DATETIME_YMDZ_PATTERN).expect("datetime_ymdz_pattern should be valid") +}); -static FLOAT_RE: Lazy = Lazy::new(|| { +static FLOAT_RE: LazyLock = LazyLock::new(|| { Regex::new(r"^\s*[-+]?((\d*\.\d+)([eE][-+]?\d+)?|inf|NaN|(\d+)[eE][-+]?\d+|\d+\.)$") .expect("float pattern should be valid") }); -static INTEGER_RE: Lazy = - Lazy::new(|| Regex::new(r"^\s*-?(\d+)$").expect("integer pattern should be valid")); +static INTEGER_RE: LazyLock = + LazyLock::new(|| Regex::new(r"^\s*-?(\d+)$").expect("integer pattern should be valid")); -static INTEGER_WITH_DELIMS_RE: Lazy = Lazy::new(|| { +static INTEGER_WITH_DELIMS_RE: LazyLock = LazyLock::new(|| { Regex::new(r"^\s*-?(\d{1,3}([,_]\d{3})+)$") .expect("integer with delimiters pattern should be valid") }); -static BOOLEAN_RE: Lazy = Lazy::new(|| { +static BOOLEAN_RE: LazyLock = LazyLock::new(|| { RegexBuilder::new(r"^\s*(true)$|^(false)$") .case_insensitive(true) .build() diff --git a/crates/nu-command/src/platform/ansi/ansi_.rs b/crates/nu-command/src/platform/ansi/ansi_.rs index a7bb37036f..fe66cb9e15 100644 --- a/crates/nu-command/src/platform/ansi/ansi_.rs +++ b/crates/nu-command/src/platform/ansi/ansi_.rs @@ -1,8 +1,8 @@ use nu_ansi_term::*; use nu_engine::command_prelude::*; use nu_protocol::{engine::StateWorkingSet, Signals}; -use once_cell::sync::Lazy; use std::collections::HashMap; +use std::sync::LazyLock; #[derive(Clone)] pub struct AnsiCommand; @@ -14,7 +14,7 @@ struct AnsiCode { } #[rustfmt::skip] -static CODE_LIST: Lazy> = Lazy::new(|| { vec![ +static CODE_LIST: LazyLock> = LazyLock::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()}, @@ -494,8 +494,8 @@ static CODE_LIST: Lazy> = Lazy::new(|| { vec![ ] }); -static CODE_MAP: Lazy> = - Lazy::new(|| build_ansi_hashmap(&CODE_LIST)); +static CODE_MAP: LazyLock> = + LazyLock::new(|| build_ansi_hashmap(&CODE_LIST)); impl Command for AnsiCommand { fn name(&self) -> &str { diff --git a/crates/nu-command/src/platform/ulimit.rs b/crates/nu-command/src/platform/ulimit.rs index 871633efc4..b908e8b3c1 100644 --- a/crates/nu-command/src/platform/ulimit.rs +++ b/crates/nu-command/src/platform/ulimit.rs @@ -1,7 +1,7 @@ use nix::sys::resource::{rlim_t, Resource, RLIM_INFINITY}; use nu_engine::command_prelude::*; -use once_cell::sync::Lazy; +use std::sync::LazyLock; /// An object contains resource related parameters struct ResourceInfo<'a> { @@ -54,7 +54,7 @@ impl<'a> Default for ResourceInfo<'a> { } } -static RESOURCE_ARRAY: Lazy> = Lazy::new(|| { +static RESOURCE_ARRAY: LazyLock> = LazyLock::new(|| { let resources = [ #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] ( diff --git a/crates/nu-command/src/strings/char_.rs b/crates/nu-command/src/strings/char_.rs index 889efa2c27..ef27d221d8 100644 --- a/crates/nu-command/src/strings/char_.rs +++ b/crates/nu-command/src/strings/char_.rs @@ -2,8 +2,8 @@ use indexmap::{indexmap, IndexMap}; use nu_engine::command_prelude::*; use nu_protocol::Signals; -use once_cell::sync::Lazy; use std::collections::HashSet; +use std::sync::LazyLock; // Character used to separate directories in a Path Environment variable on windows is ";" #[cfg(target_family = "windows")] @@ -15,7 +15,7 @@ const ENV_PATH_SEPARATOR_CHAR: char = ':'; #[derive(Clone)] pub struct Char; -static CHAR_MAP: Lazy> = Lazy::new(|| { +static CHAR_MAP: LazyLock> = LazyLock::new(|| { indexmap! { // These are some regular characters that either can't be used or // it's just easier to use them like this. @@ -150,7 +150,7 @@ static CHAR_MAP: Lazy> = Lazy::new(|| { } }); -static NO_OUTPUT_CHARS: Lazy> = Lazy::new(|| { +static NO_OUTPUT_CHARS: LazyLock> = LazyLock::new(|| { [ // If the character is in the this set, we don't output it to prevent // the broken of `char --list` command table format and alignment. diff --git a/crates/nu-command/src/strings/encode_decode/decode.rs b/crates/nu-command/src/strings/encode_decode/decode.rs index 169667645d..485cc98671 100644 --- a/crates/nu-command/src/strings/encode_decode/decode.rs +++ b/crates/nu-command/src/strings/encode_decode/decode.rs @@ -1,12 +1,12 @@ use nu_engine::command_prelude::*; use oem_cp::decode_string_complete_table; -use once_cell::sync::Lazy; use std::collections::HashMap; +use std::sync::LazyLock; // create a lazycell of all the code_table "Complete" code pages // the commented out code pages are "Incomplete", which means they // are stored as Option and not &[char; 128] -static OEM_DECODE: Lazy> = Lazy::new(|| { +static OEM_DECODE: LazyLock> = LazyLock::new(|| { let mut m = HashMap::new(); m.insert(437, &oem_cp::code_table::DECODING_TABLE_CP437); // m.insert(720, &oem_cp::code_table::DECODING_TABLE_CP720); diff --git a/crates/nu-command/src/viewers/icons.rs b/crates/nu-command/src/viewers/icons.rs index db3adb153c..5ab11ed213 100644 --- a/crates/nu-command/src/viewers/icons.rs +++ b/crates/nu-command/src/viewers/icons.rs @@ -1,5 +1,5 @@ use nu_protocol::{ShellError, Span}; -use once_cell::sync::Lazy; +use std::sync::LazyLock; use std::{collections::HashMap, path::Path}; // Attribution: Thanks exa. Most of this file is taken from around here @@ -84,7 +84,7 @@ impl Icons { // .unwrap_or_default() // } -static MAP_BY_NAME: Lazy> = Lazy::new(|| { +static MAP_BY_NAME: LazyLock> = LazyLock::new(|| { [ (".Trash", '\u{f1f8}'), //  (".atom", '\u{e764}'), //  diff --git a/crates/nu-explore/Cargo.toml b/crates/nu-explore/Cargo.toml index 4a62816379..da3beb68b8 100644 --- a/crates/nu-explore/Cargo.toml +++ b/crates/nu-explore/Cargo.toml @@ -30,7 +30,6 @@ log = { workspace = true } terminal_size = { workspace = true } strip-ansi-escapes = { workspace = true } crossterm = { workspace = true } -once_cell = { workspace = true } ratatui = { workspace = true } ansi-str = { workspace = true } unicode-width = { workspace = true } diff --git a/crates/nu-explore/src/commands/help.rs b/crates/nu-explore/src/commands/help.rs index 721c2924a5..63c24969ec 100644 --- a/crates/nu-explore/src/commands/help.rs +++ b/crates/nu-explore/src/commands/help.rs @@ -7,7 +7,7 @@ use nu_protocol::{ Value, }; -use once_cell::sync::Lazy; +use std::sync::LazyLock; #[derive(Debug, Default, Clone)] pub struct HelpCmd {} @@ -19,7 +19,7 @@ impl HelpCmd { } } -static HELP_MESSAGE: Lazy = Lazy::new(|| { +static HELP_MESSAGE: LazyLock = LazyLock::new(|| { let title = nu_ansi_term::Style::new().bold().underline(); let code = nu_ansi_term::Style::new().bold().fg(Color::Blue); diff --git a/crates/nu-system/Cargo.toml b/crates/nu-system/Cargo.toml index bf83b9155e..4ac5cccb8d 100644 --- a/crates/nu-system/Cargo.toml +++ b/crates/nu-system/Cargo.toml @@ -34,7 +34,6 @@ mach2 = { workspace = true } [target.'cfg(target_os = "windows")'.dependencies] chrono = { workspace = true, default-features = false, features = ["clock"] } ntapi = "0.4" -once_cell = { workspace = true } windows = { workspace = true, features = [ "Wdk_System_SystemServices", "Wdk_System_Threading", diff --git a/crates/nu-system/src/windows.rs b/crates/nu-system/src/windows.rs index cd689b77c4..59e2a77751 100644 --- a/crates/nu-system/src/windows.rs +++ b/crates/nu-system/src/windows.rs @@ -8,7 +8,6 @@ use libc::c_void; use ntapi::ntrtl::RTL_USER_PROCESS_PARAMETERS; use ntapi::ntwow64::{PEB32, RTL_USER_PROCESS_PARAMETERS32}; -use once_cell::sync::Lazy; use std::cell::RefCell; use std::collections::HashMap; use std::ffi::OsString; @@ -17,6 +16,7 @@ use std::os::windows::ffi::OsStringExt; use std::path::PathBuf; use std::ptr; use std::ptr::null_mut; +use std::sync::LazyLock; use std::thread; use std::time::{Duration, Instant}; @@ -694,7 +694,7 @@ unsafe fn get_process_params( )) } -static WINDOWS_8_1_OR_NEWER: Lazy = Lazy::new(|| unsafe { +static WINDOWS_8_1_OR_NEWER: LazyLock = LazyLock::new(|| unsafe { let mut version_info: OSVERSIONINFOEXW = MaybeUninit::zeroed().assume_init(); version_info.dwOSVersionInfoSize = std::mem::size_of::() as u32; diff --git a/crates/nu-table/Cargo.toml b/crates/nu-table/Cargo.toml index 2bce1125f1..b0fdf92273 100644 --- a/crates/nu-table/Cargo.toml +++ b/crates/nu-table/Cargo.toml @@ -19,7 +19,6 @@ nu-utils = { path = "../nu-utils", version = "0.99.2" } nu-engine = { path = "../nu-engine", version = "0.99.2" } nu-color-config = { path = "../nu-color-config", version = "0.99.2" } nu-ansi-term = { workspace = true } -once_cell = { workspace = true } fancy-regex = { workspace = true } tabled = { workspace = true, features = ["ansi"], default-features = false } terminal_size = { workspace = true } diff --git a/crates/nu-table/src/util.rs b/crates/nu-table/src/util.rs index a40d6625be..feb9a59616 100644 --- a/crates/nu-table/src/util.rs +++ b/crates/nu-table/src/util.rs @@ -91,12 +91,14 @@ fn colorize_lead_trail_space( fn colorize_space_one(text: &str, lead: Option>, trail: Option>) -> String { use fancy_regex::Captures; use fancy_regex::Regex; - use once_cell::sync::Lazy; + use std::sync::LazyLock; - static RE_LEADING: Lazy = - Lazy::new(|| Regex::new(r"(?m)(?P^\s+)").expect("error with leading space regex")); - static RE_TRAILING: Lazy = - Lazy::new(|| Regex::new(r"(?m)(?P\s+$)").expect("error with trailing space regex")); + static RE_LEADING: LazyLock = LazyLock::new(|| { + Regex::new(r"(?m)(?P^\s+)").expect("error with leading space regex") + }); + static RE_TRAILING: LazyLock = LazyLock::new(|| { + Regex::new(r"(?m)(?P\s+$)").expect("error with trailing space regex") + }); let mut buf = text.to_owned(); diff --git a/crates/nu-utils/Cargo.toml b/crates/nu-utils/Cargo.toml index d4639805ea..2c2cb2b10d 100644 --- a/crates/nu-utils/Cargo.toml +++ b/crates/nu-utils/Cargo.toml @@ -21,7 +21,6 @@ fancy-regex = { workspace = true } lscolors = { workspace = true, default-features = false, features = ["nu-ansi-term"] } log = { workspace = true } num-format = { workspace = true } -once_cell = { workspace = true } strip-ansi-escapes = { workspace = true } serde = { workspace = true } sys-locale = "0.3" diff --git a/crates/nu-utils/src/quoting.rs b/crates/nu-utils/src/quoting.rs index f194095ee6..baff52caeb 100644 --- a/crates/nu-utils/src/quoting.rs +++ b/crates/nu-utils/src/quoting.rs @@ -1,12 +1,12 @@ use fancy_regex::Regex; -use once_cell::sync::Lazy; +use std::sync::LazyLock; // This hits, in order: // • Any character of []:`{}#'";()|$, // • Any digit (\d) // • Any whitespace (\s) // • Case-insensitive sign-insensitive float "keywords" inf, infinity and nan. -static NEEDS_QUOTING_REGEX: Lazy = Lazy::new(|| { +static NEEDS_QUOTING_REGEX: LazyLock = LazyLock::new(|| { Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\d\s]|(?i)^[+\-]?(inf(inity)?|nan)$"#) .expect("internal error: NEEDS_QUOTING_REGEX didn't compile") });