Skip to content

Commit

Permalink
Auto merge of #12616 - Eh2406:lazy_static, r=epage
Browse files Browse the repository at this point in the history
stop using lazy_static

### What does this PR try to resolve?

Remove the dependency on `lazy_static` and replace with `std::sync::OnceLock`.

### How should we test and review this PR?

This was an internal re-factor, and the tests still pass.
I did not test that the intended caching was still working.
  • Loading branch information
bors committed Sep 1, 2023
2 parents d619310 + 16b330b commit 71dafca
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ im-rc = "15.1.0"
indexmap = "2"
itertools = "0.10.0"
jobserver = "0.1.26"
lazy_static = "1.4.0"
lazycell = "1.3.0"
libc = "0.2.147"
libgit2-sys = "0.16.1"
Expand Down
1 change: 0 additions & 1 deletion crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ flate2.workspace = true
git2.workspace = true
glob.workspace = true
itertools.workspace = true
lazy_static.workspace = true
pasetors.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand Down
58 changes: 31 additions & 27 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::os;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::str;
use std::sync::OnceLock;
use std::time::{self, Duration};

use anyhow::{bail, Result};
Expand Down Expand Up @@ -1157,13 +1158,14 @@ impl RustcInfo {
}
}

lazy_static::lazy_static! {
static ref RUSTC_INFO: RustcInfo = RustcInfo::new();
fn rustc_info() -> &'static RustcInfo {
static RUSTC_INFO: OnceLock<RustcInfo> = OnceLock::new();
RUSTC_INFO.get_or_init(RustcInfo::new)
}

/// The rustc host such as `x86_64-unknown-linux-gnu`.
pub fn rustc_host() -> &'static str {
&RUSTC_INFO.host
&rustc_info().host
}

/// The host triple suitable for use in a cargo environment variable (uppercased).
Expand All @@ -1172,7 +1174,7 @@ pub fn rustc_host_env() -> String {
}

pub fn is_nightly() -> bool {
let vv = &RUSTC_INFO.verbose_version;
let vv = &rustc_info().verbose_version;
// CARGO_TEST_DISABLE_NIGHTLY is set in rust-lang/rust's CI so that all
// nightly-only tests are disabled there. Otherwise, it could make it
// difficult to land changes which would need to be made simultaneously in
Expand Down Expand Up @@ -1225,28 +1227,27 @@ pub trait TestEnv: Sized {
if env::var_os("RUSTUP_TOOLCHAIN").is_some() {
// Override the PATH to avoid executing the rustup wrapper thousands
// of times. This makes the testsuite run substantially faster.
lazy_static::lazy_static! {
static ref RUSTC_DIR: PathBuf = {
match ProcessBuilder::new("rustup")
.args(&["which", "rustc"])
.exec_with_output()
{
Ok(output) => {
let s = str::from_utf8(&output.stdout).expect("utf8").trim();
let mut p = PathBuf::from(s);
p.pop();
p
}
Err(e) => {
panic!("RUSTUP_TOOLCHAIN was set, but could not run rustup: {}", e);
}
static RUSTC_DIR: OnceLock<PathBuf> = OnceLock::new();
let rustc_dir = RUSTC_DIR.get_or_init(|| {
match ProcessBuilder::new("rustup")
.args(&["which", "rustc"])
.exec_with_output()
{
Ok(output) => {
let s = str::from_utf8(&output.stdout).expect("utf8").trim();
let mut p = PathBuf::from(s);
p.pop();
p
}
};
}
Err(e) => {
panic!("RUSTUP_TOOLCHAIN was set, but could not run rustup: {}", e);
}
}
});
let path = env::var_os("PATH").unwrap_or_default();
let paths = env::split_paths(&path);
let new_path =
env::join_paths(std::iter::once(RUSTC_DIR.clone()).chain(paths)).unwrap();
env::join_paths(std::iter::once(rustc_dir.clone()).chain(paths)).unwrap();
self = self.env("PATH", new_path);
}

Expand Down Expand Up @@ -1408,11 +1409,14 @@ pub fn is_coarse_mtime() -> bool {
/// Architectures that do not have a modern processor, hardware emulation, etc.
/// This provides a way for those setups to increase the cut off for all the time based test.
pub fn slow_cpu_multiplier(main: u64) -> Duration {
lazy_static::lazy_static! {
static ref SLOW_CPU_MULTIPLIER: u64 =
env::var("CARGO_TEST_SLOW_CPU_MULTIPLIER").ok().and_then(|m| m.parse().ok()).unwrap_or(1);
}
Duration::from_secs(*SLOW_CPU_MULTIPLIER * main)
static SLOW_CPU_MULTIPLIER: OnceLock<u64> = OnceLock::new();
let slow_cpu_multiplier = SLOW_CPU_MULTIPLIER.get_or_init(|| {
env::var("CARGO_TEST_SLOW_CPU_MULTIPLIER")
.ok()
.and_then(|m| m.parse().ok())
.unwrap_or(1)
});
Duration::from_secs(slow_cpu_multiplier * main)
}

#[cfg(windows)]
Expand Down
1 change: 0 additions & 1 deletion crates/resolver-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ publish = false
[dependencies]
cargo.workspace = true
cargo-util.workspace = true
lazy_static.workspace = true
proptest.workspace = true
varisat.workspace = true
11 changes: 6 additions & 5 deletions crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt;
use std::fmt::Write;
use std::rc::Rc;
use std::sync::OnceLock;
use std::task::Poll;
use std::time::Instant;

Expand Down Expand Up @@ -563,11 +564,11 @@ macro_rules! pkg {
}

fn registry_loc() -> SourceId {
lazy_static::lazy_static! {
static ref EXAMPLE_DOT_COM: SourceId =
SourceId::for_registry(&"https://example.com".into_url().unwrap()).unwrap();
}
*EXAMPLE_DOT_COM
static EXAMPLE_DOT_COM: OnceLock<SourceId> = OnceLock::new();
let example_dot = EXAMPLE_DOT_COM.get_or_init(|| {
SourceId::for_registry(&"https://example.com".into_url().unwrap()).unwrap()
});
*example_dot
}

pub fn pkg<T: ToPkgId>(name: T) -> Summary {
Expand Down

0 comments on commit 71dafca

Please sign in to comment.