From dc073c331645728c079c5c86842e3d854577e215 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 25 Nov 2023 16:13:39 +0000 Subject: [PATCH] Use OnceLock instead of lazy-static in a few places --- Cargo.lock | 2 - abstio/Cargo.toml | 1 - abstio/src/abst_paths.rs | 93 +++++++++++++++++++------------------ map_gui/Cargo.toml | 1 - map_gui/src/tools/labels.rs | 10 ++-- 5 files changed, 51 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b6b0cefcda..6ad0b6e5dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,7 +15,6 @@ dependencies = [ "include_dir", "instant", "js-sys", - "lazy_static", "log", "reqwest", "serde", @@ -2844,7 +2843,6 @@ dependencies = [ "geojson", "geom", "instant", - "lazy_static", "log", "lyon", "map_model", diff --git a/abstio/Cargo.toml b/abstio/Cargo.toml index 16f05235e7..f20f0e097d 100644 --- a/abstio/Cargo.toml +++ b/abstio/Cargo.toml @@ -11,7 +11,6 @@ base64 = "0.21.0" bincode = { workspace = true } fs-err = { workspace = true } instant = { workspace = true } -lazy_static = "1.4.0" log = { workspace = true } reqwest = { version = "0.11.17", default-features=false, features=["rustls-tls"] } serde = { workspace = true } diff --git a/abstio/src/abst_paths.rs b/abstio/src/abst_paths.rs index 4fdf1732d0..d6698d074b 100644 --- a/abstio/src/abst_paths.rs +++ b/abstio/src/abst_paths.rs @@ -4,6 +4,8 @@ // have a Source enum and simplify the API. But we would either have to call Manifest::load // constantly, or plumb it around with a borrow? Or maybe even owned. +use std::sync::OnceLock; + use anyhow::Result; use serde::{Deserialize, Serialize}; @@ -11,57 +13,56 @@ use abstutil::basename; use crate::{file_exists, list_all_objects, Manifest}; -lazy_static::lazy_static! { - static ref ROOT_DIR: String = { - // If you're packaging for a release and need the data directory to be in some fixed - // location: ABST_DATA_DIR=/some/path cargo build ... - if let Some(dir) = option_env!("ABST_DATA_DIR") { - dir.trim_end_matches('/').to_string() - } else if cfg!(target_arch = "wasm32") { - "../data".to_string() - } else if file_exists("data/".to_string()) { - "data".to_string() - } else if file_exists("../data/".to_string()) { - "../data".to_string() - } else if file_exists("../../data/".to_string()) { - "../../data".to_string() - } else if file_exists("../../../data/".to_string()) { - "../../../data".to_string() - } else { - panic!("Can't find the data/ directory"); - } - }; - - static ref ROOT_PLAYER_DIR: String = { - // If you're packaging for a release and want the player's local data directory to be - // $HOME/.abstreet, set ABST_PLAYER_HOME_DIR=1 - if option_env!("ABST_PLAYER_HOME_DIR").is_some() { - match std::env::var("HOME") { - Ok(dir) => format!("{}/.abstreet", dir.trim_end_matches('/')), - Err(err) => panic!("This build of A/B Street stores player data in $HOME/.abstreet, but $HOME isn't set: {}", err), - } - } else if cfg!(target_arch = "wasm32") { - "../data".to_string() - } else if file_exists("data/".to_string()) { - "data".to_string() - } else if file_exists("../data/".to_string()) { - "../data".to_string() - } else if file_exists("../../data/".to_string()) { - "../../data".to_string() - } else if file_exists("../../../data/".to_string()) { - "../../../data".to_string() - } else { - panic!("Can't find the data/ directory"); - } - }; -} +static ROOT_DIR: OnceLock = OnceLock::new(); +static ROOT_PLAYER_DIR: OnceLock = OnceLock::new(); pub fn path>(p: I) -> String { let p = p.as_ref(); if p.starts_with("player/") { - format!("{}/{}", *ROOT_PLAYER_DIR, p) + let dir = ROOT_PLAYER_DIR.get_or_init(|| { + // If you're packaging for a release and want the player's local data directory to be + // $HOME/.abstreet, set ABST_PLAYER_HOME_DIR=1 + if option_env!("ABST_PLAYER_HOME_DIR").is_some() { + match std::env::var("HOME") { + Ok(dir) => format!("{}/.abstreet", dir.trim_end_matches('/')), + Err(err) => panic!("This build of A/B Street stores player data in $HOME/.abstreet, but $HOME isn't set: {}", err), + } + } else if cfg!(target_arch = "wasm32") { + "../data".to_string() + } else if file_exists("data/".to_string()) { + "data".to_string() + } else if file_exists("../data/".to_string()) { + "../data".to_string() + } else if file_exists("../../data/".to_string()) { + "../../data".to_string() + } else if file_exists("../../../data/".to_string()) { + "../../../data".to_string() + } else { + panic!("Can't find the data/ directory"); + } + }); + format!("{dir}/{p}") } else { - format!("{}/{}", *ROOT_DIR, p) + let dir = ROOT_DIR.get_or_init(|| { + // If you're packaging for a release and need the data directory to be in some fixed + // location: ABST_DATA_DIR=/some/path cargo build ... + if let Some(dir) = option_env!("ABST_DATA_DIR") { + dir.trim_end_matches('/').to_string() + } else if cfg!(target_arch = "wasm32") { + "../data".to_string() + } else if file_exists("data/".to_string()) { + "data".to_string() + } else if file_exists("../data/".to_string()) { + "../data".to_string() + } else if file_exists("../../data/".to_string()) { + "../../data".to_string() + } else if file_exists("../../../data/".to_string()) { + "../../../data".to_string() + } else { + panic!("Can't find the data/ directory"); + } + }); + format!("{dir}/{p}") } } diff --git a/map_gui/Cargo.toml b/map_gui/Cargo.toml index e9c82231e0..00e631825b 100644 --- a/map_gui/Cargo.toml +++ b/map_gui/Cargo.toml @@ -23,7 +23,6 @@ futures-channel = { workspace = true } geojson = { workspace = true } geom = { path = "../geom" } instant = { workspace = true } -lazy_static = "1.4.0" log = { workspace = true } lyon = "1.0.1" map_model = { path = "../map_model" } diff --git a/map_gui/src/tools/labels.rs b/map_gui/src/tools/labels.rs index 840a006c72..95356accab 100644 --- a/map_gui/src/tools/labels.rs +++ b/map_gui/src/tools/labels.rs @@ -1,7 +1,7 @@ use std::cell::RefCell; use std::collections::HashMap; +use std::sync::OnceLock; -use lazy_static::lazy_static; use regex::Regex; use abstutil::Timer; @@ -108,6 +108,8 @@ impl DrawRoadLabels { } } +static SIMPLIFY_PATTERNS: OnceLock> = OnceLock::new(); + // TODO Surely somebody has written one of these. fn simplify_name(mut x: String) -> Option { // Skip unnamed roads and highway exits @@ -115,11 +117,7 @@ fn simplify_name(mut x: String) -> Option { return None; } - lazy_static! { - static ref SIMPLIFY_PATTERNS: Vec<(Regex, String)> = simplify_patterns(); - } - - for (search, replace_with) in SIMPLIFY_PATTERNS.iter() { + for (search, replace_with) in SIMPLIFY_PATTERNS.get_or_init(simplify_patterns).iter() { // TODO The string copies are probably avoidable... x = search.replace(&x, replace_with).to_string(); }