Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std LazyLock instead of once_cell Lazy #6529

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ["Mullvad VPN"]
repository = "https://github.com/mullvad/mullvadvpn-app/"
license = "GPL-3.0"
edition = "2021"
rust-version = "1.77.0"
rust-version = "1.80.0"

[workspace]
resolver = "2"
Expand Down Expand Up @@ -83,7 +83,7 @@ windows-sys = "0.52.0"

chrono = { version = "0.4.26", default-features = false }
clap = { version = "4.4.18", features = ["cargo", "derive"] }
once_cell = "1.13"
once_cell = "1.16"
serde = "1.0.204"
serde_json = "1.0.122"

Expand Down
1 change: 0 additions & 1 deletion android/translations-converter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ workspace = true
[dependencies]
thiserror = { workspace = true }
htmlize = { version = "1.0.2", features = ["unescape"] }
once_cell = { workspace = true }
regex = "1"
serde = { workspace = true, features = ["derive"] }
quick-xml = { version = "0.27.1", features = ["serialize"] }
7 changes: 4 additions & 3 deletions android/translations-converter/src/android/string_value.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Deserializer, Serialize};
use std::{
fmt::{self, Display, Formatter, Write},
ops::Deref,
sync::LazyLock,
};

/// An Android string value
Expand Down Expand Up @@ -32,7 +32,7 @@ impl StringValue {
/// The input XML file might have line breaks inside the string, and they should be collapsed
/// into a single whitespace character.
fn collapse_line_breaks(original: String) -> String {
static LINE_BREAKS: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s*\n\s*").unwrap());
static LINE_BREAKS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s*\n\s*").unwrap());
LINE_BREAKS.replace_all(&original, " ").into_owned()
}

Expand All @@ -43,7 +43,8 @@ impl StringValue {
/// would update the string so that all parameters have indices: `Things are %1$d, %3$s and
/// %4$s`.
fn ensure_parameters_are_indexed(original: String) -> String {
static PARAMETER_INDEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\d+)\$").unwrap());
static PARAMETER_INDEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(\d+)\$").unwrap());

let mut parts = original.split('%');
let mut output = parts.next().unwrap().to_owned();
Expand Down
14 changes: 7 additions & 7 deletions android/translations-converter/src/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use once_cell::sync::Lazy;
use regex::Regex;
use std::sync::LazyLock;

pub trait Normalize {
/// Normalize the string value into a common format.
Expand All @@ -12,9 +12,9 @@ mod android {
use super::*;
use crate::android::StringValue;

static APOSTROPHES: Lazy<Regex> = Lazy::new(|| Regex::new(r"\\'").unwrap());
static DOUBLE_QUOTES: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\\""#).unwrap());
static PARAMETERS: Lazy<Regex> = Lazy::new(|| Regex::new(r"%[0-9]*\$").unwrap());
static APOSTROPHES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\\'").unwrap());
static DOUBLE_QUOTES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"\\""#).unwrap());
static PARAMETERS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"%[0-9]*\$").unwrap());

impl Normalize for StringValue {
fn normalize(&self) -> String {
Expand All @@ -35,9 +35,9 @@ mod gettext {
use super::*;
use crate::gettext::MsgString;

static ESCAPED_SINGLE_QUOTES: Lazy<Regex> = Lazy::new(|| Regex::new(r"\\'").unwrap());
static ESCAPED_DOUBLE_QUOTES: Lazy<Regex> = Lazy::new(|| Regex::new(r#"\\""#).unwrap());
static PARAMETERS: Lazy<Regex> = Lazy::new(|| Regex::new(r"%\([^)]*\)").unwrap());
static ESCAPED_SINGLE_QUOTES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\\'").unwrap());
static ESCAPED_DOUBLE_QUOTES: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"\\""#).unwrap());
static PARAMETERS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"%\([^)]*\)").unwrap());

impl Normalize for MsgString {
fn normalize(&self) -> String {
Expand Down
1 change: 0 additions & 1 deletion mullvad-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ tokio = { workspace = true, features = ["macros", "time", "rt-multi-thread", "ne
tokio-rustls = "0.24.1"
tokio-socks = "0.5.1"
rustls-pemfile = "1.0.3"
once_cell = { workspace = true }

mullvad-fs = { path = "../mullvad-fs" }
mullvad-types = { path = "../mullvad-types" }
Expand Down
4 changes: 2 additions & 2 deletions mullvad-api/src/tls_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use hyper::client::connect::{Connected, Connection};
use once_cell::sync::Lazy;
use std::sync::LazyLock;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio_rustls::{
rustls::{self, ClientConfig, ServerName},
Expand All @@ -25,7 +25,7 @@ where
S: AsyncRead + AsyncWrite + Unpin,
{
pub async fn connect_https(stream: S, domain: &str) -> io::Result<TlsStream<S>> {
static TLS_CONFIG: Lazy<Arc<ClientConfig>> = Lazy::new(|| {
static TLS_CONFIG: LazyLock<Arc<ClientConfig>> = LazyLock::new(|| {
let config = ClientConfig::builder()
.with_safe_default_cipher_suites()
.with_safe_default_kx_groups()
Expand Down
1 change: 0 additions & 1 deletion mullvad-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ thiserror = { workspace = true }
either = "1.11"
fern = { version = "0.6", features = ["colored"] }
futures = "0.3"
once_cell = { workspace = true }
libc = "0.2"
log = { workspace = true }
regex = "1.0"
Expand Down
5 changes: 2 additions & 3 deletions mullvad-daemon/src/account_history.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use mullvad_types::account::AccountToken;
use once_cell::sync::Lazy;
use regex::Regex;
use std::path::Path;
use std::{path::Path, sync::LazyLock};
use talpid_types::ErrorExt;
use tokio::{
fs,
Expand Down Expand Up @@ -32,7 +31,7 @@ pub struct AccountHistory {
token: Option<AccountToken>,
}

static ACCOUNT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[0-9]+$").unwrap());
static ACCOUNT_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[0-9]+$").unwrap());

impl AccountHistory {
pub async fn new(
Expand Down
6 changes: 3 additions & 3 deletions mullvad-daemon/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{Args, Parser};
use once_cell::sync::Lazy;
use std::sync::LazyLock;

static ENV_DESC: Lazy<String> = Lazy::new(|| {
static ENV_DESC: LazyLock<String> = LazyLock::new(|| {
format!(
"ENV:

Expand Down Expand Up @@ -119,7 +119,7 @@ impl From<CommandFlags> for Command {
}

pub fn get_config() -> &'static Config {
static CONFIG: Lazy<Config> = Lazy::new(create_config);
static CONFIG: LazyLock<Config> = LazyLock::new(create_config);
&CONFIG
}

Expand Down
4 changes: 2 additions & 2 deletions mullvad-daemon/src/geoip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;
use futures::join;
use mullvad_api::rest::{Error, RequestServiceHandle};
use mullvad_types::location::{AmIMullvad, GeoIpLocation, LocationEventData};
use once_cell::sync::Lazy;
use std::sync::LazyLock;
use talpid_core::mpsc::Sender;
use talpid_future::retry::{retry_future, ExponentialBackoff, Jittered};
use talpid_types::ErrorExt;
Expand All @@ -19,7 +19,7 @@ use crate::{DaemonEventSender, InternalDaemonEvent};
// production build, a warning will be logged and the env variable *won´t* have
// any effect on the api call. The default host name `am.i.mullvad.net` will
// always be used in release mode.
static MULLVAD_CONNCHECK_HOST: Lazy<String> = Lazy::new(|| {
static MULLVAD_CONNCHECK_HOST: LazyLock<String> = LazyLock::new(|| {
const DEFAULT_CONNCHECK_HOST: &str = "am.i.mullvad.net";
let conncheck_host_var = std::env::var("MULLVAD_CONNCHECK_HOST").ok();
let host = if cfg!(feature = "api-override") {
Expand Down
5 changes: 2 additions & 3 deletions mullvad-daemon/src/migrations/account_history.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::{Error, Result};
use mullvad_types::account::AccountToken;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use std::path::Path;
use std::{path::Path, sync::LazyLock};
use talpid_types::ErrorExt;
use tokio::{
fs::{self, File},
Expand All @@ -17,7 +16,7 @@ use tokio::{

const ACCOUNT_HISTORY_FILE: &str = "account-history.json";

static ACCOUNT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[0-9]+$").unwrap());
static ACCOUNT_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[0-9]+$").unwrap());

pub async fn migrate_location(old_dir: &Path, new_dir: &Path) {
let old_path = old_dir.join(ACCOUNT_HISTORY_FILE);
Expand Down
5 changes: 2 additions & 3 deletions mullvad-daemon/src/system_service.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::cli;
use mullvad_daemon::{runtime::new_multi_thread, DaemonShutdownHandle};
use once_cell::sync::Lazy;
use std::{
env,
ffi::{c_void, OsString},
ptr, slice,
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
mpsc, Arc,
mpsc, Arc, LazyLock,
},
thread,
time::{Duration, Instant},
Expand Down Expand Up @@ -39,7 +38,7 @@ static SERVICE_TYPE: ServiceType = ServiceType::OWN_PROCESS;
const SERVICE_RECOVERY_LAST_RESTART_DELAY: Duration = Duration::from_secs(60 * 10);
const SERVICE_FAILURE_RESET_PERIOD: Duration = Duration::from_secs(60 * 15);

static SERVICE_ACCESS: Lazy<ServiceAccess> = Lazy::new(|| {
static SERVICE_ACCESS: LazyLock<ServiceAccess> = LazyLock::new(|| {
ServiceAccess::QUERY_CONFIG
| ServiceAccess::CHANGE_CONFIG
| ServiceAccess::START
Expand Down
8 changes: 4 additions & 4 deletions mullvad-daemon/src/tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use mullvad_types::{
endpoint::MullvadWireguardEndpoint, location::GeoIpLocation, relay_list::Relay,
settings::TunnelOptions,
};
use once_cell::sync::Lazy;
use std::sync::LazyLock;
use talpid_core::tunnel_state_machine::TunnelParametersGenerator;
#[cfg(not(target_os = "android"))]
use talpid_types::net::{
Expand All @@ -31,9 +31,9 @@ use crate::device::{AccountManagerHandle, Error as DeviceError, PrivateAccountAn
/// "Same IP" functionality. This means all clients have the same in-tunnel IP on these
/// servers. This improves anonymity since the in-tunnel IP will not be unique to a specific
/// peer.
static SAME_IP_V4: Lazy<IpAddr> =
Lazy::new(|| Ipv4Addr::from_str("10.127.255.254").unwrap().into());
static SAME_IP_V6: Lazy<IpAddr> = Lazy::new(|| {
static SAME_IP_V4: LazyLock<IpAddr> =
LazyLock::new(|| Ipv4Addr::from_str("10.127.255.254").unwrap().into());
static SAME_IP_V6: LazyLock<IpAddr> = LazyLock::new(|| {
Ipv6Addr::from_str("fc00:bbbb:bbbb:bb01:ffff:ffff:ffff:ffff")
.unwrap()
.into()
Expand Down
8 changes: 4 additions & 4 deletions mullvad-daemon/src/version_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use futures::{
};
use mullvad_api::{availability::ApiAvailabilityHandle, rest::MullvadRestHandle, AppVersionProxy};
use mullvad_types::version::{AppVersionInfo, ParsedAppVersion};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{
cmp::max,
Expand All @@ -15,6 +14,7 @@ use std::{
path::{Path, PathBuf},
pin::Pin,
str::FromStr,
sync::LazyLock,
time::{Duration, SystemTime},
};
use talpid_core::mpsc::Sender;
Expand All @@ -24,9 +24,9 @@ use tokio::{fs::File, io::AsyncReadExt};

const VERSION_INFO_FILENAME: &str = "version-info.json";

static APP_VERSION: Lazy<ParsedAppVersion> =
Lazy::new(|| ParsedAppVersion::from_str(mullvad_version::VERSION).unwrap());
static IS_DEV_BUILD: Lazy<bool> = Lazy::new(|| APP_VERSION.is_dev());
static APP_VERSION: LazyLock<ParsedAppVersion> =
LazyLock::new(|| ParsedAppVersion::from_str(mullvad_version::VERSION).unwrap());
static IS_DEV_BUILD: LazyLock<bool> = LazyLock::new(|| APP_VERSION.is_dev());

const DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(15);

Expand Down
1 change: 0 additions & 1 deletion mullvad-management-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ log = { workspace = true }

[target.'cfg(unix)'.dependencies]
nix = "0.23"
once_cell = { workspace = true }

[build-dependencies]
tonic-build = { workspace = true, default-features = false, features = ["transport", "prost"] }
6 changes: 3 additions & 3 deletions mullvad-management-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ pub type ManagementServiceClient =
pub use types::management_service_server::{ManagementService, ManagementServiceServer};

#[cfg(unix)]
use once_cell::sync::Lazy;
use std::sync::LazyLock;
#[cfg(unix)]
static MULLVAD_MANAGEMENT_SOCKET_GROUP: Lazy<Option<String>> =
Lazy::new(|| env::var("MULLVAD_MANAGEMENT_SOCKET_GROUP").ok());
static MULLVAD_MANAGEMENT_SOCKET_GROUP: LazyLock<Option<String>> =
LazyLock::new(|| env::var("MULLVAD_MANAGEMENT_SOCKET_GROUP").ok());

pub const CUSTOM_LIST_LIST_NOT_FOUND_DETAILS: &[u8] = b"custom_list_list_not_found";
pub const CUSTOM_LIST_LIST_EXISTS_DETAILS: &[u8] = b"custom_list_list_exists";
Expand Down
1 change: 0 additions & 1 deletion mullvad-problem-report/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ workspace = true
[dependencies]
dirs = "5.0.1"
thiserror = { workspace = true }
once_cell = { workspace = true }
log = { workspace = true }
regex = "1.0"
uuid = { version = "1.4.1", features = ["v4"] }
Expand Down
Loading
Loading