Skip to content

Commit

Permalink
Use OnceLock instead of lazy-static in a few places
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Nov 25, 2023
1 parent e143bc4 commit dc073c3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 56 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion abstio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
93 changes: 47 additions & 46 deletions abstio/src/abst_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,65 @@
// 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};

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<String> = OnceLock::new();
static ROOT_PLAYER_DIR: OnceLock<String> = OnceLock::new();

pub fn path<I: AsRef<str>>(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}")
}
}

Expand Down
1 change: 0 additions & 1 deletion map_gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
10 changes: 4 additions & 6 deletions map_gui/src/tools/labels.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -108,18 +108,16 @@ impl DrawRoadLabels {
}
}

static SIMPLIFY_PATTERNS: OnceLock<Vec<(Regex, String)>> = OnceLock::new();

// TODO Surely somebody has written one of these.
fn simplify_name(mut x: String) -> Option<String> {
// Skip unnamed roads and highway exits
if x == "???" || x.starts_with("Exit for ") {
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();
}
Expand Down

0 comments on commit dc073c3

Please sign in to comment.