feat: remove lazy_static in favor of LazyLock

This switches to the built-in `std::sync::LazyLock`
This commit is contained in:
Yuri Astrakhan 2025-01-20 19:44:59 -05:00 committed by Amaan Qureshi
parent f23a52f410
commit 48059b72a8
20 changed files with 234 additions and 215 deletions

View file

@ -1,47 +1,63 @@
lazy_static! {
pub static ref ROOT_DIR: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR")).parent().unwrap().to_owned();
pub static ref FIXTURES_DIR: PathBuf = ROOT_DIR.join("test").join("fixtures");
pub static ref HEADER_DIR: PathBuf = ROOT_DIR.join("lib").join("include");
pub static ref GRAMMARS_DIR: PathBuf = ROOT_DIR.join("test").join("fixtures").join("grammars");
pub static ref SCRATCH_BASE_DIR: PathBuf = {
let result = ROOT_DIR.join("target").join("scratch");
fs::create_dir_all(&result).unwrap();
result
};
pub static ref WASM_DIR: PathBuf = ROOT_DIR.join("target").join("release");
pub static ref SCRATCH_DIR: PathBuf = {
// https://doc.rust-lang.org/reference/conditional-compilation.html
let vendor = if cfg!(target_vendor = "apple") {
"apple"
} else if cfg!(target_vendor = "fortanix") {
"fortanix"
} else if cfg!(target_vendor = "pc") {
"pc"
} else {
"unknown"
};
let env = if cfg!(target_env = "gnu") {
"gnu"
} else if cfg!(target_env = "msvc") {
"msvc"
} else if cfg!(target_env = "musl") {
"musl"
} else if cfg!(target_env = "sgx") {
"sgx"
} else {
"unknown"
};
let endian = if cfg!(target_endian = "little") {
"little"
} else if cfg!(target_endian = "big") {
"big"
} else {
"unknown"
};
pub static ROOT_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_owned()
});
let machine = format!("{}-{}-{vendor}-{env}-{endian}", std::env::consts::ARCH, std::env::consts::OS);
let result = SCRATCH_BASE_DIR.join(machine);
fs::create_dir_all(&result).unwrap();
result
pub static FIXTURES_DIR: LazyLock<PathBuf> =
LazyLock::new(|| ROOT_DIR.join("test").join("fixtures"));
pub static HEADER_DIR: LazyLock<PathBuf> = LazyLock::new(|| ROOT_DIR.join("lib").join("include"));
pub static GRAMMARS_DIR: LazyLock<PathBuf> =
LazyLock::new(|| ROOT_DIR.join("test").join("fixtures").join("grammars"));
pub static SCRATCH_BASE_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
let result = ROOT_DIR.join("target").join("scratch");
fs::create_dir_all(&result).unwrap();
result
});
#[cfg(feature = "wasm")]
pub static WASM_DIR: LazyLock<PathBuf> = LazyLock::new(|| ROOT_DIR.join("target").join("release"));
pub static SCRATCH_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
// https://doc.rust-lang.org/reference/conditional-compilation.html
let vendor = if cfg!(target_vendor = "apple") {
"apple"
} else if cfg!(target_vendor = "fortanix") {
"fortanix"
} else if cfg!(target_vendor = "pc") {
"pc"
} else {
"unknown"
};
}
let env = if cfg!(target_env = "gnu") {
"gnu"
} else if cfg!(target_env = "msvc") {
"msvc"
} else if cfg!(target_env = "musl") {
"musl"
} else if cfg!(target_env = "sgx") {
"sgx"
} else {
"unknown"
};
let endian = if cfg!(target_endian = "little") {
"little"
} else if cfg!(target_endian = "big") {
"big"
} else {
"unknown"
};
let machine = format!(
"{}-{}-{vendor}-{env}-{endian}",
std::env::consts::ARCH,
std::env::consts::OS
);
let result = SCRATCH_BASE_DIR.join(machine);
fs::create_dir_all(&result).unwrap();
result
});

View file

@ -1,10 +1,10 @@
use std::{
env, fs,
path::{Path, PathBuf},
sync::LazyLock,
};
use anyhow::Context;
use lazy_static::lazy_static;
use tree_sitter::Language;
use tree_sitter_generate::{ALLOC_HEADER, ARRAY_HEADER};
use tree_sitter_highlight::HighlightConfiguration;
@ -13,15 +13,13 @@ use tree_sitter_tags::TagsConfiguration;
include!("./dirs.rs");
lazy_static! {
static ref TEST_LOADER: Loader = {
let mut loader = Loader::with_parser_lib_path(SCRATCH_DIR.clone());
if env::var("TREE_SITTER_GRAMMAR_DEBUG").is_ok() {
loader.debug_build(true);
}
loader
};
}
static TEST_LOADER: LazyLock<Loader> = LazyLock::new(|| {
let mut loader = Loader::with_parser_lib_path(SCRATCH_DIR.clone());
if env::var("TREE_SITTER_GRAMMAR_DEBUG").is_ok() {
loader.debug_build(true);
}
loader
});
pub fn test_loader() -> &'static Loader {
&TEST_LOADER

View file

@ -3,31 +3,40 @@ use std::{
fs,
os::raw::c_char,
ptr, slice, str,
sync::atomic::{AtomicUsize, Ordering},
sync::{
atomic::{AtomicUsize, Ordering},
LazyLock,
},
};
use lazy_static::lazy_static;
use tree_sitter_highlight::{
c, Error, Highlight, HighlightConfiguration, HighlightEvent, Highlighter, HtmlRenderer,
};
use super::helpers::fixtures::{get_highlight_config, get_language, get_language_queries_path};
lazy_static! {
static ref JS_HIGHLIGHT: HighlightConfiguration =
get_highlight_config("javascript", Some("injections.scm"), &HIGHLIGHT_NAMES);
static ref JSDOC_HIGHLIGHT: HighlightConfiguration =
get_highlight_config("jsdoc", None, &HIGHLIGHT_NAMES);
static ref HTML_HIGHLIGHT: HighlightConfiguration =
get_highlight_config("html", Some("injections.scm"), &HIGHLIGHT_NAMES);
static ref EJS_HIGHLIGHT: HighlightConfiguration = get_highlight_config(
static JS_HIGHLIGHT: LazyLock<HighlightConfiguration> =
LazyLock::new(|| get_highlight_config("javascript", Some("injections.scm"), &HIGHLIGHT_NAMES));
static JSDOC_HIGHLIGHT: LazyLock<HighlightConfiguration> =
LazyLock::new(|| get_highlight_config("jsdoc", None, &HIGHLIGHT_NAMES));
static HTML_HIGHLIGHT: LazyLock<HighlightConfiguration> =
LazyLock::new(|| get_highlight_config("html", Some("injections.scm"), &HIGHLIGHT_NAMES));
static EJS_HIGHLIGHT: LazyLock<HighlightConfiguration> = LazyLock::new(|| {
get_highlight_config(
"embedded-template",
Some("injections-ejs.scm"),
&HIGHLIGHT_NAMES
);
static ref RUST_HIGHLIGHT: HighlightConfiguration =
get_highlight_config("rust", Some("injections.scm"), &HIGHLIGHT_NAMES);
static ref HIGHLIGHT_NAMES: Vec<String> = [
&HIGHLIGHT_NAMES,
)
});
static RUST_HIGHLIGHT: LazyLock<HighlightConfiguration> =
LazyLock::new(|| get_highlight_config("rust", Some("injections.scm"), &HIGHLIGHT_NAMES));
static HIGHLIGHT_NAMES: LazyLock<Vec<String>> = LazyLock::new(|| {
[
"attribute",
"boolean",
"carriage-return",
@ -60,12 +69,15 @@ lazy_static! {
.iter()
.copied()
.map(String::from)
.collect();
static ref HTML_ATTRS: Vec<String> = HIGHLIGHT_NAMES
.collect()
});
static HTML_ATTRS: LazyLock<Vec<String>> = LazyLock::new(|| {
HIGHLIGHT_NAMES
.iter()
.map(|s| format!("class={s}"))
.collect();
}
.collect()
});
#[test]
fn test_highlighting_javascript() {

View file

@ -1,7 +1,6 @@
use std::{env, fmt::Write};
use std::{env, fmt::Write, sync::LazyLock};
use indoc::indoc;
use lazy_static::lazy_static;
use rand::{prelude::StdRng, SeedableRng};
use streaming_iterator::StreamingIterator;
use tree_sitter::{
@ -22,9 +21,8 @@ use crate::tests::{
ITERATION_COUNT,
};
lazy_static! {
static ref EXAMPLE_FILTER: Option<String> = env::var("TREE_SITTER_TEST_EXAMPLE_FILTER").ok();
}
static EXAMPLE_FILTER: LazyLock<Option<String>> =
LazyLock::new(|| env::var("TREE_SITTER_TEST_EXAMPLE_FILTER").ok());
#[test]
fn test_query_errors_on_invalid_syntax() {

View file

@ -1,6 +1,5 @@
use std::fs;
use std::{fs, sync::LazyLock};
use lazy_static::lazy_static;
use streaming_iterator::StreamingIterator;
use tree_sitter::{
wasmtime::Engine, Parser, Query, QueryCursor, WasmError, WasmErrorKind, WasmStore,
@ -8,9 +7,7 @@ use tree_sitter::{
use crate::tests::helpers::{allocations, fixtures::WASM_DIR};
lazy_static! {
static ref ENGINE: Engine = Engine::default();
}
static ENGINE: LazyLock<Engine> = LazyLock::new(Engine::default);
#[test]
fn test_wasm_stdlib_symbols() {