Skip to content

Commit 867dfc7

Browse files
committed
Replace usage of lazy_static with once_cell
1 parent 9f4857e commit 867dfc7

File tree

11 files changed

+91
-112
lines changed

11 files changed

+91
-112
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ otel = [
3838
]
3939

4040
# Exports code dependent on private interfaces for the integration test suite
41-
test = ["dep:once_cell", "dep:walkdir"]
41+
test = ["dep:walkdir"]
4242

4343
# Sorted by alphabetic order
4444
[dependencies]
@@ -56,9 +56,8 @@ flate2 = "1"
5656
fs_at.workspace = true
5757
git-testament = "0.2"
5858
home = "0.5.4"
59-
lazy_static.workspace = true
6059
libc = "0.2"
61-
once_cell = { workspace = true, optional = true }
60+
once_cell.workspace = true
6261
opener = "0.6.0"
6362
# `openssl` is used by `curl` or `reqwest` backend although it isn't imported by rustup: this
6463
# allows controlling the vendoring status without exposing the presence of the download crate.
@@ -140,14 +139,13 @@ version = "0.48.0"
140139

141140
[dev-dependencies]
142141
enum-map = "2.5.0"
143-
once_cell.workspace = true
144142
proptest.workspace = true
145143
rustup-macros.workspace = true
146144
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
147145
trycmd = "0.14.13"
148146

149147
[build-dependencies]
150-
lazy_static = "1"
148+
once_cell.workspace = true
151149
regex = "1"
152150

153151
[workspace]
@@ -158,7 +156,6 @@ anyhow = "1.0.69"
158156
derivative = "2.2.0"
159157
enum_dispatch = "0.3.11"
160158
fs_at = "0.1.6"
161-
lazy_static = "1"
162159
once_cell = "1.18.0"
163160
opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
164161
opentelemetry-otlp = { version = "0.13.0" }

download/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ license = "MIT OR Apache-2.0"
99
default = ["reqwest-backend", "reqwest-rustls-tls", "reqwest-default-tls"]
1010

1111
curl-backend = ["curl"]
12-
reqwest-backend = ["reqwest", "env_proxy", "lazy_static"]
12+
reqwest-backend = ["reqwest", "env_proxy"]
1313
reqwest-default-tls = ["reqwest/default-tls"]
1414
reqwest-rustls-tls = ["reqwest/rustls-tls-native-roots"]
1515

1616
[dependencies]
1717
anyhow.workspace = true
1818
curl = { version = "0.4.44", optional = true }
1919
env_proxy = { version = "0.4.1", optional = true }
20-
lazy_static = { workspace = true, optional = true }
20+
once_cell.workspace = true
2121
reqwest = { version = "0.11", default-features = false, features = ["blocking", "gzip", "socks"], optional = true }
2222
thiserror.workspace = true
2323
url.workspace = true

download/src/lib.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub mod reqwest_be {
270270

271271
use anyhow::{anyhow, Context, Result};
272272
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-default-tls"))]
273-
use lazy_static::lazy_static;
273+
use once_cell::sync::Lazy;
274274
use reqwest::blocking::{Client, ClientBuilder, Response};
275275
use reqwest::{header, Proxy};
276276
use url::Url;
@@ -324,40 +324,32 @@ pub mod reqwest_be {
324324
.proxy(Proxy::custom(env_proxy))
325325
.timeout(Duration::from_secs(30))
326326
}
327+
327328
#[cfg(feature = "reqwest-rustls-tls")]
328-
lazy_static! {
329-
static ref CLIENT_RUSTLS_TLS: Client = {
330-
let catcher = || {
331-
client_generic().use_rustls_tls()
332-
.build()
333-
};
329+
static CLIENT_RUSTLS_TLS: Lazy<Client> = Lazy::new(|| {
330+
let catcher = || client_generic().use_rustls_tls().build();
331+
332+
// woah, an unwrap?!
333+
// It's OK. This is the same as what is happening in curl.
334+
//
335+
// The curl::Easy::new() internally assert!s that the initialized
336+
// Easy is not null. Inside reqwest, the errors here would be from
337+
// the TLS library returning a null pointer as well.
338+
catcher().unwrap()
339+
});
334340

335-
// woah, an unwrap?!
336-
// It's OK. This is the same as what is happening in curl.
337-
//
338-
// The curl::Easy::new() internally assert!s that the initialized
339-
// Easy is not null. Inside reqwest, the errors here would be from
340-
// the TLS library returning a null pointer as well.
341-
catcher().unwrap()
342-
};
343-
}
344341
#[cfg(feature = "reqwest-default-tls")]
345-
lazy_static! {
346-
static ref CLIENT_DEFAULT_TLS: Client = {
347-
let catcher = || {
348-
client_generic()
349-
.build()
350-
};
342+
static CLIENT_DEFAULT_TLS: Lazy<Client> = Lazy::new(|| {
343+
let catcher = || client_generic().build();
351344

352-
// woah, an unwrap?!
353-
// It's OK. This is the same as what is happening in curl.
354-
//
355-
// The curl::Easy::new() internally assert!s that the initialized
356-
// Easy is not null. Inside reqwest, the errors here would be from
357-
// the TLS library returning a null pointer as well.
358-
catcher().unwrap()
359-
};
360-
}
345+
// woah, an unwrap?!
346+
// It's OK. This is the same as what is happening in curl.
347+
//
348+
// The curl::Easy::new() internally assert!s that the initialized
349+
// Easy is not null. Inside reqwest, the errors here would be from
350+
// the TLS library returning a null pointer as well.
351+
catcher().unwrap()
352+
});
361353

362354
fn env_proxy(url: &Url) -> Option<Url> {
363355
env_proxy::for_url(url).to_url()

src/cli/common.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{cmp, env};
1010

1111
use anyhow::{anyhow, Context, Result};
1212
use git_testament::{git_testament, render_testament};
13-
use lazy_static::lazy_static;
13+
use once_cell::sync::Lazy;
1414

1515
use super::self_update;
1616
use crate::cli::download_tracker::DownloadTracker;
@@ -566,12 +566,10 @@ pub(crate) fn list_overrides(cfg: &Cfg) -> Result<utils::ExitCode> {
566566
git_testament!(TESTAMENT);
567567

568568
pub(crate) fn version() -> &'static str {
569-
lazy_static! {
570-
// Because we trust our `stable` branch given the careful release
571-
// process, we mark it trusted here so that our version numbers look
572-
// right when built from CI before the tag is pushed
573-
static ref RENDERED: String = render_testament!(TESTAMENT, "stable");
574-
}
569+
// Because we trust our `stable` branch given the careful release
570+
// process, we mark it trusted here so that our version numbers look
571+
// right when built from CI before the tag is pushed
572+
static RENDERED: Lazy<String> = Lazy::new(|| render_testament!(TESTAMENT, "stable"));
575573
&RENDERED
576574
}
577575

src/cli/errors.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::io;
55
use std::path::PathBuf;
66

7-
use lazy_static::lazy_static;
7+
use once_cell::sync::Lazy;
88
use regex::Regex;
99
use strsim::damerau_levenshtein;
1010
use thiserror::Error as ThisError;
@@ -24,10 +24,7 @@ pub enum CLIError {
2424
fn maybe_suggest_toolchain(bad_name: &str) -> String {
2525
let bad_name = &bad_name.to_ascii_lowercase();
2626
static VALID_CHANNELS: &[&str] = &["stable", "beta", "nightly"];
27-
lazy_static! {
28-
static ref NUMBERED: Regex = Regex::new(r"^\d+\.\d+$").unwrap();
29-
}
30-
27+
static NUMBERED: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\d+\.\d+$").unwrap());
3128
if NUMBERED.is_match(bad_name) {
3229
return format!(". Toolchain numbers tend to have three parts, e.g. {bad_name}.0");
3330
}

src/cli/self_update.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,12 +1139,10 @@ fn get_new_rustup_version(path: &Path) -> Option<String> {
11391139
}
11401140

11411141
fn parse_new_rustup_version(version: String) -> String {
1142-
use lazy_static::lazy_static;
1142+
use once_cell::sync::Lazy;
11431143
use regex::Regex;
11441144

1145-
lazy_static! {
1146-
static ref RE: Regex = Regex::new(r"\d+.\d+.\d+[0-9a-zA-Z-]*").unwrap();
1147-
}
1145+
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\d+.\d+.\d+[0-9a-zA-Z-]*").unwrap());
11481146

11491147
let capture = RE.captures(&version);
11501148
let matched_version = match capture {

src/dist/dist.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::str::FromStr;
99

1010
use anyhow::{anyhow, bail, Context, Result};
1111
use chrono::NaiveDate;
12-
use lazy_static::lazy_static;
12+
use once_cell::sync::Lazy;
1313
use regex::Regex;
1414
use thiserror::Error as ThisError;
1515

@@ -182,15 +182,15 @@ static TRIPLE_MIPS64_UNKNOWN_LINUX_GNUABI64: &str = "mips64el-unknown-linux-gnua
182182
impl FromStr for ParsedToolchainDesc {
183183
type Err = anyhow::Error;
184184
fn from_str(desc: &str) -> Result<Self> {
185-
lazy_static! {
186-
static ref TOOLCHAIN_CHANNEL_PATTERN: String = format!(
185+
// Note this regex gives you a guaranteed match of the channel (1)
186+
// and an optional match of the date (2) and target (3)
187+
static TOOLCHAIN_CHANNEL_RE: Lazy<Regex> = Lazy::new(|| {
188+
Regex::new(&format!(
187189
r"^({})(?:-(\d{{4}}-\d{{2}}-\d{{2}}))?(?:-(.+))?$",
188190
TOOLCHAIN_CHANNELS.join("|")
189-
);
190-
// Note this regex gives you a guaranteed match of the channel (1)
191-
// and an optional match of the date (2) and target (3)
192-
static ref TOOLCHAIN_CHANNEL_RE: Regex = Regex::new(&TOOLCHAIN_CHANNEL_PATTERN).unwrap();
193-
}
191+
))
192+
.unwrap()
193+
});
194194

195195
let d = TOOLCHAIN_CHANNEL_RE.captures(desc).map(|c| {
196196
fn fn_map(s: &str) -> Option<String> {
@@ -603,9 +603,8 @@ impl ToolchainDesc {
603603
/// date field is empty.
604604
pub(crate) fn is_tracking(&self) -> bool {
605605
let channels = ["nightly", "beta", "stable"];
606-
lazy_static! {
607-
static ref TRACKING_VERSION: Regex = Regex::new(r"^\d{1}\.\d{1,3}$").unwrap();
608-
}
606+
static TRACKING_VERSION: Lazy<Regex> =
607+
Lazy::new(|| Regex::new(r"^\d{1}\.\d{1,3}$").unwrap());
609608
(channels.iter().any(|x| *x == self.channel) || TRACKING_VERSION.is_match(&self.channel))
610609
&& self.date.is_none()
611610
}

src/dist/triple.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lazy_static::lazy_static;
1+
use once_cell::sync::Lazy;
22
use regex::Regex;
33

44
// These lists contain the targets known to rustup, and used to build
@@ -68,15 +68,16 @@ impl PartialTargetTriple {
6868
// we can count on all triple components being
6969
// delineated by it.
7070
let name = format!("-{name}");
71-
lazy_static! {
72-
static ref PATTERN: String = format!(
71+
static RE: Lazy<Regex> = Lazy::new(|| {
72+
Regex::new(&format!(
7373
r"^(?:-({}))?(?:-({}))?(?:-({}))?$",
7474
LIST_ARCHS.join("|"),
7575
LIST_OSES.join("|"),
7676
LIST_ENVS.join("|")
77-
);
78-
static ref RE: Regex = Regex::new(&PATTERN).unwrap();
79-
}
77+
))
78+
.unwrap()
79+
});
80+
8081
RE.captures(&name).map(|c| {
8182
fn fn_map(s: &str) -> Option<String> {
8283
if s.is_empty() {

src/test/mock/clitools.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::{
1515
};
1616

1717
use enum_map::{enum_map, Enum, EnumMap};
18-
use lazy_static::lazy_static;
1918
use once_cell::sync::Lazy;
2019
use url::Url;
2120

@@ -1501,39 +1500,39 @@ fn build_combined_installer(components: &[&MockInstallerBuilder]) -> MockInstall
15011500
/// and then we store some associated files next to it which indicate
15021501
/// the version/version hash information.
15031502
fn mock_bin(name: &str, version: &str, version_hash: &str) -> Vec<MockFile> {
1504-
lazy_static! {
1505-
static ref MOCK_BIN: Arc<Vec<u8>> = {
1506-
// Create a temp directory to hold the source and the output
1507-
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
1508-
let source_path = tempdir.path().join("in.rs");
1509-
let dest_path = tempdir.path().join(format!("out{EXE_SUFFIX}"));
1510-
1511-
// Write the source
1512-
let source = include_bytes!("mock_bin_src.rs");
1513-
fs::write(&source_path, &source[..]).unwrap();
1514-
1515-
// Create the executable
1516-
let status = Command::new("rustc")
1517-
.arg(&source_path)
1518-
.arg("-C").arg("panic=abort")
1519-
.arg("-O")
1520-
.arg("-o").arg(&dest_path)
1521-
.status()
1522-
.unwrap();
1523-
assert!(status.success());
1524-
assert!(dest_path.exists());
1525-
1526-
// Remove debug info from std/core which included in every programs,
1527-
// otherwise we just ignore the return result here
1528-
if cfg!(unix) {
1529-
drop(Command::new("strip").arg(&dest_path).status());
1530-
}
1503+
static MOCK_BIN: Lazy<Arc<Vec<u8>>> = Lazy::new(|| {
1504+
// Create a temp directory to hold the source and the output
1505+
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
1506+
let source_path = tempdir.path().join("in.rs");
1507+
let dest_path = tempdir.path().join(format!("out{EXE_SUFFIX}"));
1508+
1509+
// Write the source
1510+
let source = include_bytes!("mock_bin_src.rs");
1511+
fs::write(&source_path, &source[..]).unwrap();
1512+
1513+
// Create the executable
1514+
let status = Command::new("rustc")
1515+
.arg(&source_path)
1516+
.arg("-C")
1517+
.arg("panic=abort")
1518+
.arg("-O")
1519+
.arg("-o")
1520+
.arg(&dest_path)
1521+
.status()
1522+
.unwrap();
1523+
assert!(status.success());
1524+
assert!(dest_path.exists());
15311525

1532-
// Now load it into memory
1533-
let buf = fs::read(dest_path).unwrap();
1534-
Arc::new(buf)
1535-
};
1536-
}
1526+
// Remove debug info from std/core which included in every programs,
1527+
// otherwise we just ignore the return result here
1528+
if cfg!(unix) {
1529+
drop(Command::new("strip").arg(&dest_path).status());
1530+
}
1531+
1532+
// Now load it into memory
1533+
let buf = fs::read(dest_path).unwrap();
1534+
Arc::new(buf)
1535+
});
15371536

15381537
let name = format!("bin/{name}{EXE_SUFFIX}");
15391538
vec![

0 commit comments

Comments
 (0)