Skip to content

Commit

Permalink
Replace lazy_static with once_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Sciencentistguy authored and sylvestre committed Jul 7, 2022
1 parent 37b754f commit 820767d
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 33 deletions.
4 changes: 1 addition & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ uudoc = [ "zip" ]
[dependencies]
clap = { version = "3.1", features = ["wrap_help", "cargo"] }
clap_complete = "3.1"
once_cell = "1.13.0"
phf = "0.10.1"
lazy_static = { version="1.3" }
selinux = { version="0.2", optional = true }
textwrap = { version="0.15", features=["terminal_size"] }
uucore = { version=">=0.0.11", package="uucore", path="src/uucore" }
selinux = { version="0.2", optional = true }
zip = { version = "0.6.0", optional=true, default_features=false, features=["deflate"] }
# * uutils
uu_test = { optional=true, version="0.0.14", package="uu_test", path="src/uu/test" }
Expand Down
3 changes: 0 additions & 3 deletions src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ once_cell = "1.13.0"
atty = "0.2"
selinux = { version="0.2", optional = true }

[target.'cfg(unix)'.dependencies]
lazy_static = "1.4.0"

[[bin]]
name = "ls"
path = "src/main.rs"
Expand Down
13 changes: 4 additions & 9 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

#[macro_use]
extern crate uucore;
#[cfg(unix)]
#[macro_use]
extern crate lazy_static;

use clap::{crate_version, Arg, Command};
use glob::Pattern;
Expand Down Expand Up @@ -2251,16 +2248,16 @@ 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;
#[cfg(unix)]
use std::sync::Mutex;
#[cfg(unix)]
use uucore::entries;
use uucore::quoting_style;

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

let mut uid_cache = UID_CACHE.lock().unwrap();
uid_cache
Expand All @@ -2280,9 +2277,7 @@ fn display_uname(metadata: &Metadata, config: &Config) -> String {

#[cfg(all(unix, not(target_os = "redox")))]
fn cached_gid2grp(gid: u32) -> String {
lazy_static! {
static ref GID_CACHE: Mutex<HashMap<u32, String>> = Mutex::new(HashMap::new());
}
static GID_CACHE: Lazy<Mutex<HashMap<u32, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));

let mut gid_cache = GID_CACHE.lock().unwrap();
gid_cache
Expand Down
2 changes: 1 addition & 1 deletion src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ nix = { version = "0.24.1", optional = true, default-features = false, features

[dev-dependencies]
clap = "3.1"
lazy_static = "1.3"
once_cell = "1.13"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["errhandlingapi", "fileapi", "handleapi", "winerror"] }
Expand Down
6 changes: 2 additions & 4 deletions src/uucore/src/lib/mods/backup_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ mod tests {
use std::env;
// Required to instantiate mutex in shared context
use clap::Command;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use std::sync::Mutex;

// The mutex is required here as by default all tests are run as separate
Expand All @@ -456,9 +456,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.
lazy_static! {
static ref TEST_MUTEX: Mutex<()> = Mutex::new(());
}
static TEST_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

// Environment variable for "VERSION_CONTROL"
static ENV_VERSION_CONTROL: &str = "VERSION_CONTROL";
Expand Down
5 changes: 2 additions & 3 deletions tests/by-util/test_chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::common::util::*;
use std::fs::{metadata, set_permissions, OpenOptions, Permissions};
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::sync::Mutex;
use once_cell::sync::Lazy;

extern crate libc;
use uucore::mode::strip_minus_from_mode;
Expand All @@ -11,9 +12,7 @@ use self::libc::umask;
static TEST_FILE: &str = "file";
static REFERENCE_FILE: &str = "reference";
static REFERENCE_PERMS: u32 = 0o247;
lazy_static! {
static ref UMASK_MUTEX: Mutex<()> = Mutex::new(());
}
static UMASK_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

struct TestCase {
args: Vec<&'static str>,
Expand Down
6 changes: 3 additions & 3 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use std::path::Path;
#[cfg(not(windows))]
use std::path::PathBuf;
#[cfg(not(windows))]
use once_cell::sync::Lazy;
#[cfg(not(windows))]
use std::sync::Mutex;
use std::thread::sleep;
use std::time::Duration;

#[cfg(not(windows))]
lazy_static! {
static ref UMASK_MUTEX: Mutex<()> = Mutex::new(());
}
static UMASK_MUTEX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));

const LONG_ARGS: &[&str] = &[
"-l",
Expand Down
5 changes: 0 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#[macro_use]
mod common;

#[allow(unused_imports)]
#[cfg(unix)]
#[macro_use]
extern crate lazy_static;

#[cfg(unix)]
extern crate rust_users;

Expand Down

0 comments on commit 820767d

Please sign in to comment.