From e177f7a6b092ef4cc6b3ae4edace2cd40a06c3da Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Wed, 5 Mar 2025 11:02:56 +0100 Subject: [PATCH] Remove once_cell dependency & use LazyLock --- Cargo.lock | 3 --- Cargo.toml | 2 -- src/uu/ls/Cargo.toml | 1 - src/uu/ls/src/ls.rs | 8 +++++--- src/uucore/Cargo.toml | 2 -- src/uucore/src/lib/features/backup_control.rs | 5 ++--- src/uucore/src/lib/features/entries.rs | 6 ++---- src/uucore/src/lib/features/utmpx.rs | 2 +- src/uucore/src/lib/lib.rs | 13 +++++-------- 9 files changed, 15 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9586171b32f..577a2fe0aaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -474,7 +474,6 @@ dependencies = [ "libc", "nix", "num-prime", - "once_cell", "phf", "phf_codegen", "pretty_assertions", @@ -2899,7 +2898,6 @@ dependencies = [ "hostname", "lscolors", "number_prefix", - "once_cell", "selinux", "terminal_size", "uucore", @@ -3509,7 +3507,6 @@ dependencies = [ "memchr", "nix", "number_prefix", - "once_cell", "os_display", "regex", "sha1", diff --git a/Cargo.toml b/Cargo.toml index 8f42ddec502..3bc350ac937 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -314,7 +314,6 @@ num-bigint = "0.4.4" num-prime = "0.4.4" num-traits = "0.2.19" number_prefix = "0.4" -once_cell = "1.19.0" onig = { version = "~6.4", default-features = false } parse_datetime = "0.8.0" phf = "0.11.2" @@ -366,7 +365,6 @@ uu_base32 = { version = "0.0.29", path = "src/uu/base32" } [dependencies] clap = { workspace = true } -once_cell = { workspace = true } uucore = { workspace = true } clap_complete = { workspace = true } clap_mangen = { workspace = true } diff --git a/src/uu/ls/Cargo.toml b/src/uu/ls/Cargo.toml index a21f178542a..ce29d829c50 100644 --- a/src/uu/ls/Cargo.toml +++ b/src/uu/ls/Cargo.toml @@ -24,7 +24,6 @@ glob = { workspace = true } hostname = { workspace = true } lscolors = { workspace = true } number_prefix = { workspace = true } -once_cell = { workspace = true } selinux = { workspace = true, optional = true } terminal_size = { workspace = true } uucore = { workspace = true, features = [ diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index 680fe94ab4c..5c021aa5cb0 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -3057,7 +3057,7 @@ fn get_inode(metadata: &Metadata) -> String { // Currently getpwuid is `linux` target only. If it's broken out into // a posix-compliant attribute this can be updated... #[cfg(unix)] -use once_cell::sync::Lazy; +use std::sync::LazyLock; #[cfg(unix)] use std::sync::Mutex; #[cfg(unix)] @@ -3066,7 +3066,8 @@ use uucore::fs::FileInformation; #[cfg(unix)] fn cached_uid2usr(uid: u32) -> String { - static UID_CACHE: Lazy>> = Lazy::new(|| Mutex::new(HashMap::new())); + static UID_CACHE: LazyLock>> = + LazyLock::new(|| Mutex::new(HashMap::new())); let mut uid_cache = UID_CACHE.lock().unwrap(); uid_cache @@ -3086,7 +3087,8 @@ fn display_uname(metadata: &Metadata, config: &Config) -> String { #[cfg(all(unix, not(target_os = "redox")))] fn cached_gid2grp(gid: u32) -> String { - static GID_CACHE: Lazy>> = Lazy::new(|| Mutex::new(HashMap::new())); + static GID_CACHE: LazyLock>> = + LazyLock::new(|| Mutex::new(HashMap::new())); let mut gid_cache = GID_CACHE.lock().unwrap(); gid_cache diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index e2633ad969a..d2c69f5e39c 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -45,7 +45,6 @@ data-encoding = { version = "2.6", optional = true } data-encoding-macro = { version = "0.1.15", optional = true } z85 = { version = "3.0.5", optional = true } libc = { workspace = true, optional = true } -once_cell = { workspace = true } os_display = "0.1.3" digest = { workspace = true, optional = true } @@ -68,7 +67,6 @@ xattr = { workspace = true, optional = true } [dev-dependencies] clap = { workspace = true } -once_cell = { workspace = true } tempfile = { workspace = true } [target.'cfg(target_os = "windows")'.dependencies] diff --git a/src/uucore/src/lib/features/backup_control.rs b/src/uucore/src/lib/features/backup_control.rs index 591f57f95f4..03793a50bd9 100644 --- a/src/uucore/src/lib/features/backup_control.rs +++ b/src/uucore/src/lib/features/backup_control.rs @@ -485,8 +485,7 @@ mod tests { use super::*; // Required to instantiate mutex in shared context use clap::Command; - use once_cell::sync::Lazy; - use std::sync::Mutex; + use std::sync::{LazyLock, Mutex}; // The mutex is required here as by default all tests are run as separate // threads under the same parent process. As environment variables are @@ -494,7 +493,7 @@ mod tests { // occur if no precautions are taken. Thus we have all tests that rely on // environment variables lock this empty mutex to ensure they don't access // it concurrently. - static TEST_MUTEX: Lazy> = Lazy::new(|| Mutex::new(())); + static TEST_MUTEX: LazyLock> = LazyLock::new(|| Mutex::new(())); // Environment variable for "VERSION_CONTROL" static ENV_VERSION_CONTROL: &str = "VERSION_CONTROL"; diff --git a/src/uucore/src/lib/features/entries.rs b/src/uucore/src/lib/features/entries.rs index f3d1232eb59..56f96786669 100644 --- a/src/uucore/src/lib/features/entries.rs +++ b/src/uucore/src/lib/features/entries.rs @@ -43,9 +43,7 @@ use std::io::Error as IOError; use std::io::ErrorKind; use std::io::Result as IOResult; use std::ptr; -use std::sync::Mutex; - -use once_cell::sync::Lazy; +use std::sync::{LazyLock, Mutex}; extern "C" { /// From: `` @@ -276,7 +274,7 @@ pub trait Locate { // to, so we must copy all the data we want before releasing the lock. // (Technically we must also ensure that the raw functions aren't being called // anywhere else in the program.) -static PW_LOCK: Lazy> = Lazy::new(|| Mutex::new(())); +static PW_LOCK: LazyLock> = LazyLock::new(|| Mutex::new(())); macro_rules! f { ($fnam:ident, $fid:ident, $t:ident, $st:ident) => { diff --git a/src/uucore/src/lib/features/utmpx.rs b/src/uucore/src/lib/features/utmpx.rs index c8e77ce4c46..b66bfd329d5 100644 --- a/src/uucore/src/lib/features/utmpx.rs +++ b/src/uucore/src/lib/features/utmpx.rs @@ -304,7 +304,7 @@ impl Utmpx { // I believe the only technical memory unsafety that could happen is a data // race while copying the data out of the pointer returned by getutxent(), but // ordinary race conditions are also very much possible. -static LOCK: Lazy> = Lazy::new(|| Mutex::new(())); +static LOCK: LazyLock> = LazyLock::new(|| Mutex::new(())); /// Iterator of login records pub struct UtmpxIter { diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index c2ff84b08e0..df51425e5c5 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -115,16 +115,13 @@ use nix::sys::signal::{ sigaction, SaFlags, SigAction, SigHandler::SigDfl, SigSet, Signal::SIGBUS, Signal::SIGSEGV, }; use std::borrow::Cow; -use std::ffi::OsStr; -use std::ffi::OsString; +use std::ffi::{OsStr, OsString}; use std::io::{BufRead, BufReader}; use std::iter; #[cfg(unix)] use std::os::unix::ffi::{OsStrExt, OsStringExt}; use std::str; -use std::sync::atomic::Ordering; - -use once_cell::sync::Lazy; +use std::sync::{atomic::Ordering, LazyLock}; /// Disables the custom signal handlers installed by Rust for stack-overflow handling. With those custom signal handlers processes ignore the first SIGBUS and SIGSEGV signal they receive. /// See for details. @@ -194,9 +191,9 @@ pub fn set_utility_is_second_arg() { // args_os() can be expensive to call, it copies all of argv before iterating. // So if we want only the first arg or so it's overkill. We cache it. -static ARGV: Lazy> = Lazy::new(|| wild::args_os().collect()); +static ARGV: LazyLock> = LazyLock::new(|| wild::args_os().collect()); -static UTIL_NAME: Lazy = Lazy::new(|| { +static UTIL_NAME: LazyLock = LazyLock::new(|| { let base_index = usize::from(get_utility_is_second_arg()); let is_man = usize::from(ARGV[base_index].eq("manpage")); let argv_index = base_index + is_man; @@ -209,7 +206,7 @@ pub fn util_name() -> &'static str { &UTIL_NAME } -static EXECUTION_PHRASE: Lazy = Lazy::new(|| { +static EXECUTION_PHRASE: LazyLock = LazyLock::new(|| { if get_utility_is_second_arg() { ARGV.iter() .take(2)