mirror of
https://github.com/rust-lang/cargo
synced 2024-08-28 03:39:31 +00:00
refactor: replace some usages of lazy_static
with OnceLock
* Some usages still wait for `LazyCell`. * `TEST_ROOTS` is no longer used. Removed.
This commit is contained in:
parent
1f581e79cf
commit
fa7cc198aa
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -276,7 +276,6 @@ dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"itertools",
|
"itertools",
|
||||||
"jobserver",
|
"jobserver",
|
||||||
"lazy_static",
|
|
||||||
"lazycell",
|
"lazycell",
|
||||||
"libc",
|
"libc",
|
||||||
"libgit2-sys",
|
"libgit2-sys",
|
||||||
|
|
|
@ -137,7 +137,6 @@ im-rc.workspace = true
|
||||||
indexmap.workspace = true
|
indexmap.workspace = true
|
||||||
itertools.workspace = true
|
itertools.workspace = true
|
||||||
jobserver.workspace = true
|
jobserver.workspace = true
|
||||||
lazy_static.workspace = true
|
|
||||||
lazycell.workspace = true
|
lazycell.workspace = true
|
||||||
libc.workspace = true
|
libc.workspace = true
|
||||||
libgit2-sys.workspace = true
|
libgit2-sys.workspace = true
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use filetime::{self, FileTime};
|
use filetime::{self, FileTime};
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{self, ErrorKind};
|
use std::io::{self, ErrorKind};
|
||||||
|
@ -9,15 +8,11 @@ use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
static CARGO_INTEGRATION_TEST_DIR: &str = "cit";
|
static CARGO_INTEGRATION_TEST_DIR: &str = "cit";
|
||||||
|
|
||||||
lazy_static! {
|
static GLOBAL_ROOT: OnceLock<Mutex<Option<PathBuf>>> = OnceLock::new();
|
||||||
// TODO: Use `SyncOnceCell` when stable
|
|
||||||
static ref GLOBAL_ROOT: Mutex<Option<PathBuf>> = Mutex::new(None);
|
|
||||||
|
|
||||||
static ref TEST_ROOTS: Mutex<HashMap<String, PathBuf>> = Default::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This is used when running cargo is pre-CARGO_TARGET_TMPDIR
|
/// This is used when running cargo is pre-CARGO_TARGET_TMPDIR
|
||||||
/// TODO: Remove when CARGO_TARGET_TMPDIR grows old enough.
|
/// 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>) {
|
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() {
|
if lock.is_none() {
|
||||||
let mut root = match tmp_dir {
|
let mut root = match tmp_dir {
|
||||||
Some(tmp_dir) => PathBuf::from(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 {
|
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() {
|
match lock.as_ref() {
|
||||||
Some(p) => p.clone(),
|
Some(p) => p.clone(),
|
||||||
None => unreachable!("GLOBAL_ROOT not set yet"),
|
None => unreachable!("GLOBAL_ROOT not set yet"),
|
||||||
|
|
|
@ -1,20 +1,21 @@
|
||||||
//! Common executables that can be reused by various tests.
|
//! Common executables that can be reused by various tests.
|
||||||
|
|
||||||
use crate::{basic_manifest, paths, project, Project};
|
use crate::{basic_manifest, paths, project, Project};
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
lazy_static! {
|
static ECHO_WRAPPER: OnceLock<Mutex<Option<PathBuf>>> = OnceLock::new();
|
||||||
static ref ECHO_WRAPPER: Mutex<Option<PathBuf>> = Mutex::new(None);
|
static ECHO: OnceLock<Mutex<Option<PathBuf>>> = OnceLock::new();
|
||||||
static ref ECHO: Mutex<Option<PathBuf>> = Mutex::new(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the path to an executable that works as a wrapper around rustc.
|
/// 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.
|
/// The wrapper will echo the command line it was called with to stderr.
|
||||||
pub fn echo_wrapper() -> PathBuf {
|
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 {
|
if let Some(path) = &*lock {
|
||||||
return path.clone();
|
return path.clone();
|
||||||
}
|
}
|
||||||
|
@ -53,7 +54,7 @@ pub fn echo_wrapper() -> PathBuf {
|
||||||
///
|
///
|
||||||
/// Do not expect this to be anything fancy.
|
/// Do not expect this to be anything fancy.
|
||||||
pub fn echo() -> PathBuf {
|
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 {
|
if let Some(path) = &*lock {
|
||||||
return path.clone();
|
return path.clone();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,16 +14,6 @@ use super::list_commands;
|
||||||
use crate::command_prelude::*;
|
use crate::command_prelude::*;
|
||||||
use cargo::core::features::HIDDEN;
|
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 {
|
pub fn main(config: &mut LazyConfig) -> CliResult {
|
||||||
let args = cli().try_get_matches()?;
|
let args = cli().try_get_matches()?;
|
||||||
|
|
||||||
|
@ -128,15 +118,28 @@ Run with 'cargo -Z [FLAG] [COMMAND]'",
|
||||||
}
|
}
|
||||||
|
|
||||||
if expanded_args.flag("list") {
|
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:");
|
drop_println!(config, "Installed Commands:");
|
||||||
for (name, command) in list_commands(config) {
|
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 {
|
match command {
|
||||||
CommandInfo::BuiltIn { about } => {
|
CommandInfo::BuiltIn { about } => {
|
||||||
assert!(
|
assert!(
|
||||||
known_external_desc.is_none(),
|
known_external_desc.is_none(),
|
||||||
"KNOWN_EXTERNAL_COMMANDS shouldn't contain builtin \"{}\"",
|
"known_external_commands shouldn't contain builtin `{name}`",
|
||||||
name
|
|
||||||
);
|
);
|
||||||
let summary = about.unwrap_or_default();
|
let summary = about.unwrap_or_default();
|
||||||
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
|
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
|
||||||
|
|
|
@ -5,6 +5,7 @@ use std::hash::Hash;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
use serde::de;
|
use serde::de;
|
||||||
use serde::ser;
|
use serde::ser;
|
||||||
|
@ -13,10 +14,7 @@ use crate::core::source::SourceId;
|
||||||
use crate::util::interning::InternedString;
|
use crate::util::interning::InternedString;
|
||||||
use crate::util::{CargoResult, ToSemver};
|
use crate::util::{CargoResult, ToSemver};
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
static PACKAGE_ID_CACHE: OnceLock<Mutex<HashSet<&'static PackageIdInner>>> = OnceLock::new();
|
||||||
static ref PACKAGE_ID_CACHE: Mutex<HashSet<&'static PackageIdInner>> =
|
|
||||||
Mutex::new(HashSet::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Identifier for a specific version of a package in a specific source.
|
/// Identifier for a specific version of a package in a specific source.
|
||||||
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
|
||||||
|
@ -147,7 +145,10 @@ impl PackageId {
|
||||||
version,
|
version,
|
||||||
source_id,
|
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 = cache.get(&inner).cloned().unwrap_or_else(|| {
|
||||||
let inner = Box::leak(Box::new(inner));
|
let inner = Box::leak(Box::new(inner));
|
||||||
cache.insert(inner);
|
cache.insert(inner);
|
||||||
|
|
|
@ -13,11 +13,10 @@ use std::hash::{self, Hash};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::sync::OnceLock;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
static SOURCE_ID_CACHE: OnceLock<Mutex<HashSet<&'static SourceIdInner>>> = OnceLock::new();
|
||||||
static ref SOURCE_ID_CACHE: Mutex<HashSet<&'static SourceIdInner>> = Default::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Unique identifier for a source of packages.
|
/// Unique identifier for a source of packages.
|
||||||
///
|
///
|
||||||
|
@ -118,7 +117,10 @@ impl SourceId {
|
||||||
|
|
||||||
/// Interns the value and returns the wrapped type.
|
/// Interns the value and returns the wrapped type.
|
||||||
fn wrap(inner: SourceIdInner) -> SourceId {
|
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 = cache.get(&inner).cloned().unwrap_or_else(|| {
|
||||||
let inner = Box::leak(Box::new(inner));
|
let inner = Box::leak(Box::new(inner));
|
||||||
cache.insert(inner);
|
cache.insert(inner);
|
||||||
|
|
|
@ -10,14 +10,13 @@ use std::path::Path;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
fn leak(s: String) -> &'static str {
|
fn leak(s: String) -> &'static str {
|
||||||
Box::leak(s.into_boxed_str())
|
Box::leak(s.into_boxed_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
|
||||||
static ref STRING_CACHE: Mutex<HashSet<&'static str>> = Mutex::new(HashSet::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct InternedString {
|
pub struct InternedString {
|
||||||
|
@ -64,7 +63,10 @@ impl Eq for InternedString {}
|
||||||
|
|
||||||
impl InternedString {
|
impl InternedString {
|
||||||
pub fn new(str: &str) -> 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 = cache.get(str).cloned().unwrap_or_else(|| {
|
||||||
let s = leak(str.to_string());
|
let s = leak(str.to_string());
|
||||||
cache.insert(s);
|
cache.insert(s);
|
||||||
|
|
Loading…
Reference in a new issue