Skip to content

Commit

Permalink
Remove once_cell dependency & use LazyLock
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Mar 5, 2025
1 parent 0f9b36b commit e177f7a
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 27 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }
Expand Down
1 change: 0 additions & 1 deletion src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
8 changes: 5 additions & 3 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -3066,7 +3066,8 @@ use uucore::fs::FileInformation;

#[cfg(unix)]
fn cached_uid2usr(uid: u32) -> String {
static UID_CACHE: Lazy<Mutex<HashMap<u32, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));
static UID_CACHE: LazyLock<Mutex<HashMap<u32, String>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));

let mut uid_cache = UID_CACHE.lock().unwrap();
uid_cache
Expand All @@ -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<Mutex<HashMap<u32, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));
static GID_CACHE: LazyLock<Mutex<HashMap<u32, String>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));

let mut gid_cache = GID_CACHE.lock().unwrap();
gid_cache
Expand Down
2 changes: 0 additions & 2 deletions src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions src/uucore/src/lib/features/backup_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,16 +485,15 @@ 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
// specific to processes (and thus shared among threads), data races *will*
// 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<Mutex<()>> = Lazy::new(|| Mutex::new(()));
static TEST_MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

// Environment variable for "VERSION_CONTROL"
static ENV_VERSION_CONTROL: &str = "VERSION_CONTROL";
Expand Down
6 changes: 2 additions & 4 deletions src/uucore/src/lib/features/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<https://man7.org/linux/man-pages/man3/getgrouplist.3.html>`
Expand Down Expand Up @@ -276,7 +274,7 @@ pub trait Locate<K> {
// 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<Mutex<()>> = Lazy::new(|| Mutex::new(()));
static PW_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

macro_rules! f {
($fnam:ident, $fid:ident, $t:ident, $st:ident) => {
Expand Down
2 changes: 1 addition & 1 deletion src/uucore/src/lib/features/utmpx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<()>> = Lazy::new(|| Mutex::new(()));
static LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

/// Iterator of login records
pub struct UtmpxIter {
Expand Down
13 changes: 5 additions & 8 deletions src/uucore/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/rust-lang/rust/blob/8ac1525e091d3db28e67adcbbd6db1e1deaa37fb/src/libstd/sys/unix/stack_overflow.rs#L71-L92> for details.
Expand Down Expand Up @@ -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<Vec<OsString>> = Lazy::new(|| wild::args_os().collect());
static ARGV: LazyLock<Vec<OsString>> = LazyLock::new(|| wild::args_os().collect());

static UTIL_NAME: Lazy<String> = Lazy::new(|| {
static UTIL_NAME: LazyLock<String> = 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;
Expand All @@ -209,7 +206,7 @@ pub fn util_name() -> &'static str {
&UTIL_NAME
}

static EXECUTION_PHRASE: Lazy<String> = Lazy::new(|| {
static EXECUTION_PHRASE: LazyLock<String> = LazyLock::new(|| {
if get_utility_is_second_arg() {
ARGV.iter()
.take(2)
Expand Down

0 comments on commit e177f7a

Please sign in to comment.