util: Replace lazy_static! with OnceLock (#13215)

This PR replaces the `lazy_static!` usages in the `util` crate with
`OnceLock` from the standard library.

This allows us to drop the `lazy_static` dependency from this crate.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-06-18 12:44:58 -04:00 committed by GitHub
parent 41180b8d81
commit 01b836a191
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 29 additions and 24 deletions

View file

@ -22,7 +22,6 @@ dirs.workspace = true
futures.workspace = true
git2 = { workspace = true, optional = true }
globset.workspace = true
lazy_static.workspace = true
log.workspace = true
rand.workspace = true
regex.workspace = true

View file

@ -1,3 +1,4 @@
use std::sync::OnceLock;
use std::{
ffi::OsStr,
path::{Path, PathBuf},
@ -6,8 +7,10 @@ use std::{
use globset::{Glob, GlobSet, GlobSetBuilder};
use serde::{Deserialize, Serialize};
lazy_static::lazy_static! {
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
/// Returns the path to the user's home directory.
pub fn home_dir() -> &'static PathBuf {
static HOME_DIR: OnceLock<PathBuf> = OnceLock::new();
HOME_DIR.get_or_init(|| dirs::home_dir().expect("failed to determine home directory"))
}
pub trait PathExt {
@ -50,7 +53,7 @@ impl<T: AsRef<Path>> PathExt for T {
/// Linux or macOS, the original path is returned unchanged.
fn compact(&self) -> PathBuf {
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
match self.as_ref().strip_prefix(HOME.as_path()) {
match self.as_ref().strip_prefix(home_dir().as_path()) {
Ok(relative_path) => {
let mut shortened_path = PathBuf::new();
shortened_path.push("~");
@ -477,7 +480,7 @@ mod tests {
#[test]
fn test_path_compact() {
let path: PathBuf = [
HOME.to_string_lossy().to_string(),
home_dir().to_string_lossy().to_string(),
"some_file.txt".to_string(),
]
.iter()

View file

@ -6,8 +6,9 @@ pub mod serde;
pub mod test;
use futures::Future;
use lazy_static::lazy_static;
use rand::{seq::SliceRandom, Rng};
use regex::Regex;
use std::sync::OnceLock;
use std::{
borrow::Cow,
cmp::{self, Ordering},
@ -309,13 +310,14 @@ pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut se
}
pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
lazy_static! {
pub static ref ZED_MEASUREMENTS: bool = env::var("ZED_MEASUREMENTS")
static ZED_MEASUREMENTS: OnceLock<bool> = OnceLock::new();
let zed_measurements = ZED_MEASUREMENTS.get_or_init(|| {
env::var("ZED_MEASUREMENTS")
.map(|measurements| measurements == "1" || measurements == "true")
.unwrap_or(false);
}
.unwrap_or(false)
});
if *ZED_MEASUREMENTS {
if *zed_measurements {
let start = Instant::now();
let result = f();
let elapsed = start.elapsed();
@ -662,15 +664,17 @@ impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
Some(self.cmp(other))
}
}
lazy_static! {
static ref EMOJI_REGEX: regex::Regex = regex::Regex::new("(\\p{Emoji}|\u{200D})").unwrap();
fn emoji_regex() -> &'static Regex {
static EMOJI_REGEX: OnceLock<Regex> = OnceLock::new();
EMOJI_REGEX.get_or_init(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap())
}
/// Returns true if the given string consists of emojis only.
/// E.g. "👨‍👩‍👧‍👧👋" will return true, but "👋!" will return false.
pub fn word_consists_of_emojis(s: &str) -> bool {
let mut prev_end = 0;
for capture in EMOJI_REGEX.find_iter(s) {
for capture in emoji_regex().find_iter(s) {
if capture.start() != prev_end {
return false;
}