diff --git a/Cargo.lock b/Cargo.lock index 4e8ddfbb7..d82928705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -276,7 +276,6 @@ dependencies = [ "indexmap", "itertools", "jobserver", - "lazy_static", "lazycell", "libc", "libgit2-sys", diff --git a/Cargo.toml b/Cargo.toml index 01262c1db..c95ff612c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -137,7 +137,6 @@ im-rc.workspace = true indexmap.workspace = true itertools.workspace = true jobserver.workspace = true -lazy_static.workspace = true lazycell.workspace = true libc.workspace = true libgit2-sys.workspace = true diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs index ef1fddb70..50040e1d4 100644 --- a/crates/cargo-test-support/src/paths.rs +++ b/crates/cargo-test-support/src/paths.rs @@ -1,7 +1,6 @@ use filetime::{self, FileTime}; -use lazy_static::lazy_static; + use std::cell::RefCell; -use std::collections::HashMap; use std::env; use std::fs; use std::io::{self, ErrorKind}; @@ -9,15 +8,11 @@ use std::path::{Path, PathBuf}; use std::process::Command; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Mutex; +use std::sync::OnceLock; static CARGO_INTEGRATION_TEST_DIR: &str = "cit"; -lazy_static! { - // TODO: Use `SyncOnceCell` when stable - static ref GLOBAL_ROOT: Mutex> = Mutex::new(None); - - static ref TEST_ROOTS: Mutex> = Default::default(); -} +static GLOBAL_ROOT: OnceLock>> = OnceLock::new(); /// This is used when running cargo is pre-CARGO_TARGET_TMPDIR /// TODO: Remove when CARGO_TARGET_TMPDIR grows old enough. @@ -31,7 +26,10 @@ fn global_root_legacy() -> PathBuf { } fn set_global_root(tmp_dir: Option<&'static str>) { - let mut lock = GLOBAL_ROOT.lock().unwrap(); + let mut lock = GLOBAL_ROOT + .get_or_init(|| Default::default()) + .lock() + .unwrap(); if lock.is_none() { let mut root = match tmp_dir { Some(tmp_dir) => PathBuf::from(tmp_dir), @@ -44,7 +42,10 @@ fn set_global_root(tmp_dir: Option<&'static str>) { } pub fn global_root() -> PathBuf { - let lock = GLOBAL_ROOT.lock().unwrap(); + let lock = GLOBAL_ROOT + .get_or_init(|| Default::default()) + .lock() + .unwrap(); match lock.as_ref() { Some(p) => p.clone(), None => unreachable!("GLOBAL_ROOT not set yet"), diff --git a/crates/cargo-test-support/src/tools.rs b/crates/cargo-test-support/src/tools.rs index 7c056b6fa..2ce2849ae 100644 --- a/crates/cargo-test-support/src/tools.rs +++ b/crates/cargo-test-support/src/tools.rs @@ -1,20 +1,21 @@ //! Common executables that can be reused by various tests. use crate::{basic_manifest, paths, project, Project}; -use lazy_static::lazy_static; use std::path::{Path, PathBuf}; use std::sync::Mutex; +use std::sync::OnceLock; -lazy_static! { - static ref ECHO_WRAPPER: Mutex> = Mutex::new(None); - static ref ECHO: Mutex> = Mutex::new(None); -} +static ECHO_WRAPPER: OnceLock>> = OnceLock::new(); +static ECHO: OnceLock>> = OnceLock::new(); /// Returns the path to an executable that works as a wrapper around rustc. /// /// The wrapper will echo the command line it was called with to stderr. pub fn echo_wrapper() -> PathBuf { - let mut lock = ECHO_WRAPPER.lock().unwrap(); + let mut lock = ECHO_WRAPPER + .get_or_init(|| Default::default()) + .lock() + .unwrap(); if let Some(path) = &*lock { return path.clone(); } @@ -53,7 +54,7 @@ pub fn echo_wrapper() -> PathBuf { /// /// Do not expect this to be anything fancy. pub fn echo() -> PathBuf { - let mut lock = ECHO.lock().unwrap(); + let mut lock = ECHO.get_or_init(|| Default::default()).lock().unwrap(); if let Some(path) = &*lock { return path.clone(); } diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 946816571..f46a499e4 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -14,16 +14,6 @@ use super::list_commands; use crate::command_prelude::*; use cargo::core::features::HIDDEN; -lazy_static::lazy_static! { - // Maps from commonly known external commands (not builtin to cargo) to their - // description, for the help page. Reserved for external subcommands that are - // core within the rust ecosystem (esp ones that might become internal in the future). - static ref KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS: HashMap<&'static str, &'static str> = HashMap::from([ - ("clippy", "Checks a package to catch common mistakes and improve your Rust code."), - ("fmt", "Formats all bin and lib files of the current crate using rustfmt."), - ]); -} - pub fn main(config: &mut LazyConfig) -> CliResult { let args = cli().try_get_matches()?; @@ -128,15 +118,28 @@ Run with 'cargo -Z [FLAG] [COMMAND]'", } if expanded_args.flag("list") { + // Maps from commonly known external commands (not builtin to cargo) + // to their description, for the help page. Reserved for external + // subcommands that are core within the rust ecosystem (esp ones that + // might become internal in the future). + let known_external_command_descriptions = HashMap::from([ + ( + "clippy", + "Checks a package to catch common mistakes and improve your Rust code.", + ), + ( + "fmt", + "Formats all bin and lib files of the current crate using rustfmt.", + ), + ]); drop_println!(config, "Installed Commands:"); for (name, command) in list_commands(config) { - let known_external_desc = KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS.get(name.as_str()); + let known_external_desc = known_external_command_descriptions.get(name.as_str()); match command { CommandInfo::BuiltIn { about } => { assert!( known_external_desc.is_none(), - "KNOWN_EXTERNAL_COMMANDS shouldn't contain builtin \"{}\"", - name + "known_external_commands shouldn't contain builtin `{name}`", ); let summary = about.unwrap_or_default(); let summary = summary.lines().next().unwrap_or(&summary); // display only the first line diff --git a/src/cargo/core/package_id.rs b/src/cargo/core/package_id.rs index ee31e9c48..7abd545d7 100644 --- a/src/cargo/core/package_id.rs +++ b/src/cargo/core/package_id.rs @@ -5,6 +5,7 @@ use std::hash::Hash; use std::path::Path; use std::ptr; use std::sync::Mutex; +use std::sync::OnceLock; use serde::de; use serde::ser; @@ -13,10 +14,7 @@ use crate::core::source::SourceId; use crate::util::interning::InternedString; use crate::util::{CargoResult, ToSemver}; -lazy_static::lazy_static! { - static ref PACKAGE_ID_CACHE: Mutex> = - Mutex::new(HashSet::new()); -} +static PACKAGE_ID_CACHE: OnceLock>> = OnceLock::new(); /// Identifier for a specific version of a package in a specific source. #[derive(Clone, Copy, Eq, PartialOrd, Ord)] @@ -147,7 +145,10 @@ impl PackageId { version, source_id, }; - let mut cache = PACKAGE_ID_CACHE.lock().unwrap(); + let mut cache = PACKAGE_ID_CACHE + .get_or_init(|| Default::default()) + .lock() + .unwrap(); let inner = cache.get(&inner).cloned().unwrap_or_else(|| { let inner = Box::leak(Box::new(inner)); cache.insert(inner); diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index c369dab16..4064364d5 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -13,11 +13,10 @@ use std::hash::{self, Hash}; use std::path::{Path, PathBuf}; use std::ptr; use std::sync::Mutex; +use std::sync::OnceLock; use url::Url; -lazy_static::lazy_static! { - static ref SOURCE_ID_CACHE: Mutex> = Default::default(); -} +static SOURCE_ID_CACHE: OnceLock>> = OnceLock::new(); /// Unique identifier for a source of packages. /// @@ -118,7 +117,10 @@ impl SourceId { /// Interns the value and returns the wrapped type. fn wrap(inner: SourceIdInner) -> SourceId { - let mut cache = SOURCE_ID_CACHE.lock().unwrap(); + let mut cache = SOURCE_ID_CACHE + .get_or_init(|| Default::default()) + .lock() + .unwrap(); let inner = cache.get(&inner).cloned().unwrap_or_else(|| { let inner = Box::leak(Box::new(inner)); cache.insert(inner); diff --git a/src/cargo/util/interning.rs b/src/cargo/util/interning.rs index bbec12942..584fdf623 100644 --- a/src/cargo/util/interning.rs +++ b/src/cargo/util/interning.rs @@ -10,14 +10,13 @@ use std::path::Path; use std::ptr; use std::str; use std::sync::Mutex; +use std::sync::OnceLock; fn leak(s: String) -> &'static str { Box::leak(s.into_boxed_str()) } -lazy_static::lazy_static! { - static ref STRING_CACHE: Mutex> = Mutex::new(HashSet::new()); -} +static STRING_CACHE: OnceLock>> = OnceLock::new(); #[derive(Clone, Copy)] pub struct InternedString { @@ -64,7 +63,10 @@ impl Eq for InternedString {} impl InternedString { pub fn new(str: &str) -> InternedString { - let mut cache = STRING_CACHE.lock().unwrap(); + let mut cache = STRING_CACHE + .get_or_init(|| Default::default()) + .lock() + .unwrap(); let s = cache.get(str).cloned().unwrap_or_else(|| { let s = leak(str.to_string()); cache.insert(s);