Skip to content

Commit

Permalink
Replace some use of lazy_static with once_cell
Browse files Browse the repository at this point in the history
To align more with the upcoming standardizations within the Rust
ecosystem which started with the release of `1.70.0` and the inevitable
deprecation of `lazy_static`:
rust-lang-nursery/lazy-static.rs#214
  • Loading branch information
MarkusPettersson98 committed Jun 22, 2023
1 parent 3400972 commit 1de2d66
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 216 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion mullvad-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ err-derive = "0.3.1"
fern = { version = "0.6", features = ["colored"] }
futures = "0.3"
ipnetwork = "0.16"
lazy_static = "1.0"
once_cell = "1.13"
libc = "0.2"
log = "0.4"
parking_lot = "0.12.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,4 +1,5 @@
use mullvad_types::account::AccountToken;
use once_cell::sync::Lazy;
use regex::Regex;
use std::path::Path;
use talpid_types::ErrorExt;
Expand Down Expand Up @@ -32,9 +33,7 @@ pub struct AccountHistory {
token: Option<AccountToken>,
}

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

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

lazy_static::lazy_static! {
static ref ENV_DESC: String = format!(
static ENV_DESC: Lazy<String> = Lazy::new(|| {
format!(
"ENV:
MULLVAD_RESOURCE_DIR Resource directory (i.e used to locate a root CA certificate)
Expand All @@ -18,8 +19,9 @@ lazy_static::lazy_static! {
mullvad_paths::get_default_settings_dir().map(|dir| dir.display().to_string()).unwrap_or_else(|_| "N/A".to_string()),
mullvad_paths::get_default_cache_dir().map(|dir| dir.display().to_string()).unwrap_or_else(|_| "N/A".to_string()),
mullvad_paths::get_default_log_dir().map(|dir| dir.display().to_string()).unwrap_or_else(|_| "N/A".to_string()),
mullvad_paths::get_default_rpc_socket_path().display());
}
mullvad_paths::get_default_rpc_socket_path().display()
)
});

#[derive(Debug, Parser)]
#[command(author, version = mullvad_version::VERSION, about, long_about = None, after_help = &*ENV_DESC)]
Expand Down Expand Up @@ -70,9 +72,7 @@ pub struct Config {
}

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

Expand Down
35 changes: 17 additions & 18 deletions mullvad-daemon/src/geoip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use mullvad_api::{
rest::{Error, RequestServiceHandle},
};
use mullvad_types::location::{AmIMullvad, GeoIpLocation};
use once_cell::sync::Lazy;
use talpid_types::ErrorExt;

// Define the Mullvad connection checking api endpoint.
Expand All @@ -15,27 +16,25 @@ use talpid_types::ErrorExt;
// 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.
lazy_static::lazy_static! {
static ref MULLVAD_CONNCHECK_HOST: String = {
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") {
match conncheck_host_var.as_deref() {
Some(host) => {
log::debug!("Overriding conncheck endpoint. Using {}", &host);
host
},
None => DEFAULT_CONNCHECK_HOST,
static MULLVAD_CONNCHECK_HOST: Lazy<String> = Lazy::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") {
match conncheck_host_var.as_deref() {
Some(host) => {
log::debug!("Overriding conncheck endpoint. Using {}", &host);
host
}
} else {
if conncheck_host_var.is_some() {
log::warn!("These variables are ignored in production builds: MULLVAD_CONNCHECK_HOST");
};
DEFAULT_CONNCHECK_HOST
None => DEFAULT_CONNCHECK_HOST,
}
} else {
if conncheck_host_var.is_some() {
log::warn!("These variables are ignored in production builds: MULLVAD_CONNCHECK_HOST");
};
host.to_string()
DEFAULT_CONNCHECK_HOST
};
}
host.to_string()
});

pub async fn send_location_request(
request_sender: RequestServiceHandle,
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,5 +1,6 @@
use super::{Error, Result};
use mullvad_types::account::AccountToken;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::Deserialize;
use std::path::Path;
Expand All @@ -16,9 +17,7 @@ use tokio::{

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

lazy_static::lazy_static! {
static ref ACCOUNT_REGEX: Regex = Regex::new(r"^[0-9]+$").unwrap();
}
static ACCOUNT_REGEX: Lazy<Regex> = Lazy::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
13 changes: 7 additions & 6 deletions mullvad-daemon/src/system_service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::cli;
use libc::c_void;
use mullvad_daemon::{runtime::new_runtime_builder, DaemonShutdownHandle};
use once_cell::sync::Lazy;
use std::{
env,
ffi::OsString,
Expand Down Expand Up @@ -39,12 +40,12 @@ 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);

lazy_static::lazy_static! {
static ref SERVICE_ACCESS: ServiceAccess = ServiceAccess::QUERY_CONFIG
| ServiceAccess::CHANGE_CONFIG
| ServiceAccess::START
| ServiceAccess::DELETE;
}
static SERVICE_ACCESS: Lazy<ServiceAccess> = Lazy::new(|| {
ServiceAccess::QUERY_CONFIG
| ServiceAccess::CHANGE_CONFIG
| ServiceAccess::START
| ServiceAccess::DELETE
});

pub fn run() -> Result<(), String> {
// Start the service dispatcher.
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,6 +6,7 @@ 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::{
future::Future,
Expand All @@ -20,10 +21,9 @@ use tokio::fs::{self, File};

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

lazy_static::lazy_static! {
static ref APP_VERSION: ParsedAppVersion = ParsedAppVersion::from_str(mullvad_version::VERSION).unwrap();
static ref IS_DEV_BUILD: bool = APP_VERSION.is_dev();
}
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());

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

Expand Down
2 changes: 1 addition & 1 deletion mullvad-management-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ log = "0.4"

[target.'cfg(unix)'.dependencies]
nix = "0.23"
lazy_static = "1.0"
once_cell = "1.13"

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

#[cfg(unix)]
lazy_static::lazy_static! {
static ref MULLVAD_MANAGEMENT_SOCKET_GROUP: Option<String> = env::var("MULLVAD_MANAGEMENT_SOCKET_GROUP")
.ok();
}
use once_cell::sync::Lazy;
#[cfg(unix)]
static MULLVAD_MANAGEMENT_SOCKET_GROUP: Lazy<Option<String>> =
Lazy::new(|| env::var("MULLVAD_MANAGEMENT_SOCKET_GROUP").ok());

#[derive(err_derive::Error, Debug)]
#[error(no_from)]
Expand Down
2 changes: 1 addition & 1 deletion mullvad-problem-report/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ publish = false
[dependencies]
dirs-next = "2.0"
err-derive = "0.3.1"
lazy_static = "1.0"
once_cell = "1.13"
log = "0.4"
regex = "1.0"
uuid = { version = "0.8", features = ["v4"] }
Expand Down
40 changes: 17 additions & 23 deletions mullvad-problem-report/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(rust_2018_idioms)]

use lazy_static::lazy_static;
use mullvad_api::proxy::ApiConnectionMode;
use once_cell::sync::Lazy;
use regex::Regex;
use std::{
borrow::Cow,
Expand Down Expand Up @@ -413,9 +413,7 @@ impl ProblemReport {
}

fn redact_account_number(input: &str) -> Cow<'_, str> {
lazy_static! {
static ref RE: Regex = Regex::new("\\d{16}").unwrap();
}
static RE: Lazy<Regex> = Lazy::new(|| Regex::new("\\d{16}").unwrap());
RE.replace_all(input, "[REDACTED ACCOUNT NUMBER]")
}

Expand All @@ -424,29 +422,25 @@ impl ProblemReport {
}

fn redact_network_info(input: &str) -> Cow<'_, str> {
lazy_static! {
static ref RE: Regex = {
let boundary = "[^0-9a-zA-Z.:]";
let combined_pattern = format!(
"(?P<start>^|{})(?:{}|{}|{})",
boundary,
build_ipv4_regex(),
build_ipv6_regex(),
build_mac_regex(),
);
Regex::new(&combined_pattern).unwrap()
};
}
static RE: Lazy<Regex> = Lazy::new(|| {
let boundary = "[^0-9a-zA-Z.:]";
let combined_pattern = format!(
"(?P<start>^|{})(?:{}|{}|{})",
boundary,
build_ipv4_regex(),
build_ipv6_regex(),
build_mac_regex(),
);
Regex::new(&combined_pattern).unwrap()
});
RE.replace_all(input, "$start[REDACTED]")
}

fn redact_guids(input: &str) -> Cow<'_, str> {
lazy_static! {
static ref RE: Regex = Regex::new(
r#"(?i)\{?[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}?"#
)
.unwrap();
}
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"(?i)\{?[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}\}?"#)
.unwrap()
});
RE.replace_all(input, "[REDACTED]")
}

Expand Down
2 changes: 1 addition & 1 deletion mullvad-relay-selector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ mullvad-api = { path = "../mullvad-api" }
mullvad-types = { path = "../mullvad-types" }

[dev-dependencies]
lazy_static = "1.0"
once_cell = "1.13"
Loading

0 comments on commit 1de2d66

Please sign in to comment.