Skip to content

Commit

Permalink
Remove once_cell dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Yue Fei authored and rami3l committed Jul 31, 2024
1 parent 03b7c56 commit d5209e1
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 50 deletions.
61 changes: 45 additions & 16 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ git-testament = "0.2"
home = "0.5.4"
itertools = "0.13"
libc = "0.2"
once_cell.workspace = true
opener = "0.7.0"
# `openssl` is used by `curl` or `reqwest` backend although it isn't imported by rustup: this
# allows controlling the vendoring status without exposing the presence of the download crate.
Expand Down Expand Up @@ -124,7 +123,6 @@ tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
trycmd = "0.15.0"

[build-dependencies]
once_cell.workspace = true
platforms.workspace = true
regex = "1"

Expand All @@ -134,7 +132,6 @@ members = ["download"]
[workspace.dependencies]
anyhow = "1.0.69"
fs_at = "0.2.1"
once_cell = "1.18.0"
opentelemetry = "0.24"
opentelemetry-otlp = "0.17"
opentelemetry_sdk = { version = "0.24", features = ["rt-tokio"] }
Expand Down
4 changes: 1 addition & 3 deletions download/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ license = "MIT OR Apache-2.0"
default = ["reqwest-backend", "reqwest-rustls-tls", "reqwest-native-tls"]
curl-backend = ["curl"]
reqwest-backend = ["reqwest", "env_proxy"]
reqwest-native-tls = ["reqwest/native-tls", "dep:once_cell"]
reqwest-native-tls = ["reqwest/native-tls"]
reqwest-rustls-tls = [
"reqwest/rustls-tls-manual-roots-no-provider",
"dep:rustls",
"dep:rustls-platform-verifier",
"dep:once_cell",
]

[dependencies]
anyhow.workspace = true
curl = { version = "0.4.44", optional = true }
env_proxy = { version = "0.4.1", optional = true }
once_cell = { workspace = true, optional = true }
reqwest = { version = "0.12", default-features = false, features = ["blocking", "gzip", "socks", "stream"], optional = true }
rustls = { version = "0.23", optional = true, default-features = false, features = ["logging", "ring", "tls12"] }
rustls-platform-verifier = { version = "0.3", optional = true }
Expand Down
8 changes: 4 additions & 4 deletions download/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,11 @@ pub mod reqwest_be {
use std::io;
#[cfg(feature = "reqwest-rustls-tls")]
use std::sync::Arc;
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
use std::sync::LazyLock;
use std::time::Duration;

use anyhow::{anyhow, Context, Result};
#[cfg(any(feature = "reqwest-rustls-tls", feature = "reqwest-native-tls"))]
use once_cell::sync::Lazy;
use reqwest::{header, Client, ClientBuilder, Proxy, Response};
#[cfg(feature = "reqwest-rustls-tls")]
use rustls::crypto::ring;
Expand Down Expand Up @@ -354,7 +354,7 @@ pub mod reqwest_be {
}

#[cfg(feature = "reqwest-rustls-tls")]
static CLIENT_RUSTLS_TLS: Lazy<Client> = Lazy::new(|| {
static CLIENT_RUSTLS_TLS: LazyLock<Client> = LazyLock::new(|| {
let catcher = || {
client_generic()
.use_preconfigured_tls(
Expand All @@ -377,7 +377,7 @@ pub mod reqwest_be {
});

#[cfg(feature = "reqwest-native-tls")]
static CLIENT_DEFAULT_TLS: Lazy<Client> = Lazy::new(|| {
static CLIENT_DEFAULT_TLS: LazyLock<Client> = LazyLock::new(|| {
let catcher = || {
client_generic()
.user_agent(super::REQWEST_DEFAULT_TLS_USER_AGENT)
Expand Down
4 changes: 2 additions & 2 deletions download/tests/read-proxy-env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use std::env::{remove_var, set_var};
use std::error::Error;
use std::net::TcpListener;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::LazyLock;
use std::thread;
use std::time::Duration;

use env_proxy::for_url;
use once_cell::sync::Lazy;
use reqwest::{Client, Proxy};
use tokio::sync::Mutex;
use url::Url;

static SERIALISE_TESTS: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
static SERIALISE_TESTS: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

fn scrub_env() {
remove_var("http_proxy");
Expand Down
5 changes: 2 additions & 3 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use std::fs;
use std::io::ErrorKind;
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, LazyLock, Mutex};
use std::{cmp, env};

use anyhow::{anyhow, Context, Result};
use git_testament::{git_testament, render_testament};
use once_cell::sync::Lazy;
use tracing::{debug, error, info, trace, warn};

use super::self_update;
Expand Down Expand Up @@ -529,7 +528,7 @@ pub(crate) fn version() -> &'static str {
// Because we trust our `stable` branch given the careful release
// process, we mark it trusted here so that our version numbers look
// right when built from CI before the tag is pushed
static RENDERED: Lazy<String> = Lazy::new(|| render_testament!(TESTAMENT, "stable"));
static RENDERED: LazyLock<String> = LazyLock::new(|| render_testament!(TESTAMENT, "stable"));
&RENDERED
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use std::io;
use std::path::PathBuf;
use std::sync::LazyLock;

use once_cell::sync::Lazy;
use regex::Regex;
use strsim::damerau_levenshtein;
use thiserror::Error as ThisError;
Expand All @@ -24,7 +24,7 @@ pub enum CLIError {
fn maybe_suggest_toolchain(bad_name: &str) -> String {
let bad_name = &bad_name.to_ascii_lowercase();
static VALID_CHANNELS: &[&str] = &["stable", "beta", "nightly"];
static NUMBERED: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[0-9]+\.[0-9]+$").unwrap());
static NUMBERED: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[0-9]+\.[0-9]+$").unwrap());
if NUMBERED.is_match(bad_name) {
return format!(". Toolchain numbers tend to have three parts, e.g. {bad_name}.0");
}
Expand Down
7 changes: 4 additions & 3 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,11 +1046,12 @@ fn get_new_rustup_version(path: &Path) -> Option<String> {
}

fn parse_new_rustup_version(version: String) -> String {
use once_cell::sync::Lazy;
use std::sync::LazyLock;

use regex::Regex;

static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[0-9]+.[0-9]+.[0-9]+[0-9a-zA-Z-]*").unwrap());
static RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"[0-9]+.[0-9]+.[0-9]+[0-9a-zA-Z-]*").unwrap());

let capture = RE.captures(&version);
let matched_version = match capture {
Expand Down
7 changes: 4 additions & 3 deletions src/dist/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Installation from a Rust distribution server
use std::{collections::HashSet, env, fmt, io::Write, ops::Deref, path::Path, str::FromStr};
use std::{
collections::HashSet, env, fmt, io::Write, ops::Deref, path::Path, str::FromStr, sync::LazyLock,
};

use anyhow::{anyhow, bail, Context, Result};
use chrono::NaiveDate;
use clap::{builder::PossibleValue, ValueEnum};
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use thiserror::Error as ThisError;
Expand Down Expand Up @@ -272,7 +273,7 @@ impl FromStr for ParsedToolchainDesc {
fn from_str(desc: &str) -> Result<Self> {
// Note this regex gives you a guaranteed match of the channel (1)
// and an optional match of the date (2) and target (3)
static TOOLCHAIN_CHANNEL_RE: Lazy<Regex> = Lazy::new(|| {
static TOOLCHAIN_CHANNEL_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(&format!(
r"^({})(?:-([0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}}))?(?:-(.+))?$",
// The channel patterns we support
Expand Down
5 changes: 3 additions & 2 deletions src/dist/triple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use once_cell::sync::Lazy;
use std::sync::LazyLock;

use regex::Regex;

pub mod known;
Expand All @@ -24,7 +25,7 @@ impl PartialTargetTriple {
// we can count on all triple components being
// delineated by it.
let name = format!("-{name}");
static RE: Lazy<Regex> = Lazy::new(|| {
static RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(&format!(
r"^(?:-({}))?(?:-({}))?(?:-({}))?$",
known::LIST_ARCHS.join("|"),
Expand Down
11 changes: 5 additions & 6 deletions src/test/mock/clitools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ use std::{
ops::{Deref, DerefMut},
path::{Path, PathBuf},
process::Command,
sync::{Arc, RwLock, RwLockWriteGuard},
sync::{Arc, LazyLock, RwLock, RwLockWriteGuard},
time::Instant,
};

use enum_map::{enum_map, Enum, EnumMap};
use once_cell::sync::Lazy;
use tempfile::TempDir;
use url::Url;

Expand Down Expand Up @@ -116,8 +115,8 @@ pub static MULTI_ARCH1: &str = "i686-unknown-linux-gnu";
#[cfg(not(target_pointer_width = "64"))]
pub static MULTI_ARCH1: &str = "x86_64-unknown-linux-gnu";

static CONST_TEST_STATE: Lazy<ConstState> =
Lazy::new(|| ConstState::new(const_dist_dir().unwrap()));
static CONST_TEST_STATE: LazyLock<ConstState> =
LazyLock::new(|| ConstState::new(const_dist_dir().unwrap()));

/// Const test state - test dirs that can be reused across tests.
struct ConstState {
Expand All @@ -132,7 +131,7 @@ struct ConstState {
/// environments. In doing this we can ensure that new test environment creation
/// does not result in ETXTBSY because the FDs in question happen to be in
/// newly `fork()`d but not yet `exec()`d subprocesses of other tests.
pub static CMD_LOCK: Lazy<RwLock<usize>> = Lazy::new(|| RwLock::new(0));
pub static CMD_LOCK: LazyLock<RwLock<usize>> = LazyLock::new(|| RwLock::new(0));

impl ConstState {
fn new(const_dist_dir: tempfile::TempDir) -> Self {
Expand Down Expand Up @@ -1583,7 +1582,7 @@ fn build_combined_installer(components: &[&MockInstallerBuilder]) -> MockInstall
/// and then we store some associated files next to it which indicate
/// the version/version hash information.
fn mock_bin(name: &str, version: &str, version_hash: &str) -> Vec<MockFile> {
static MOCK_BIN: Lazy<Arc<Vec<u8>>> = Lazy::new(|| {
static MOCK_BIN: LazyLock<Arc<Vec<u8>>> = LazyLock::new(|| {
// Create a temp directory to hold the source and the output
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
let source_path = tempdir.path().join("in.rs");
Expand Down
Loading

0 comments on commit d5209e1

Please sign in to comment.