Skip to content

Commit 48059b7

Browse files
nyurikamaanq
authored andcommitted
feat: remove lazy_static in favor of LazyLock
This switches to the built-in `std::sync::LazyLock`
1 parent f23a52f commit 48059b7

File tree

20 files changed

+235
-216
lines changed

20 files changed

+235
-216
lines changed

Cargo.lock

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ heck = "0.5.0"
118118
html-escape = "0.2.13"
119119
indexmap = "2.7.1"
120120
indoc = "2.0.5"
121-
lazy_static = "1.5.0"
122121
libloading = "0.8.6"
123122
log = { version = "0.4.25", features = ["std"] }
124123
memchr = "2.7.4"

cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ heck.workspace = true
4343
html-escape.workspace = true
4444
indexmap.workspace = true
4545
indoc.workspace = true
46-
lazy_static.workspace = true
4746
log.workspace = true
4847
memchr.workspace = true
4948
rand.workspace = true

cli/benches/benchmark.rs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,72 @@ use std::{
33
env, fs,
44
path::{Path, PathBuf},
55
str,
6+
sync::LazyLock,
67
time::Instant,
78
};
89

910
use anyhow::Context;
10-
use lazy_static::lazy_static;
1111
use tree_sitter::{Language, Parser, Query};
1212
use tree_sitter_loader::{CompileConfig, Loader};
1313

1414
include!("../src/tests/helpers/dirs.rs");
1515

16-
lazy_static! {
17-
static ref LANGUAGE_FILTER: Option<String> =
18-
env::var("TREE_SITTER_BENCHMARK_LANGUAGE_FILTER").ok();
19-
static ref EXAMPLE_FILTER: Option<String> =
20-
env::var("TREE_SITTER_BENCHMARK_EXAMPLE_FILTER").ok();
21-
static ref REPETITION_COUNT: usize = env::var("TREE_SITTER_BENCHMARK_REPETITION_COUNT")
16+
static LANGUAGE_FILTER: LazyLock<Option<String>> =
17+
LazyLock::new(|| env::var("TREE_SITTER_BENCHMARK_LANGUAGE_FILTER").ok());
18+
static EXAMPLE_FILTER: LazyLock<Option<String>> =
19+
LazyLock::new(|| env::var("TREE_SITTER_BENCHMARK_EXAMPLE_FILTER").ok());
20+
static REPETITION_COUNT: LazyLock<usize> = LazyLock::new(|| {
21+
env::var("TREE_SITTER_BENCHMARK_REPETITION_COUNT")
2222
.map(|s| s.parse::<usize>().unwrap())
23-
.unwrap_or(5);
24-
static ref TEST_LOADER: Loader = Loader::with_parser_lib_path(SCRATCH_DIR.clone());
25-
static ref EXAMPLE_AND_QUERY_PATHS_BY_LANGUAGE_DIR: BTreeMap<PathBuf, (Vec<PathBuf>, Vec<PathBuf>)> = {
26-
fn process_dir(result: &mut BTreeMap<PathBuf, (Vec<PathBuf>, Vec<PathBuf>)>, dir: &Path) {
27-
if dir.join("grammar.js").exists() {
28-
let relative_path = dir.strip_prefix(GRAMMARS_DIR.as_path()).unwrap();
29-
let (example_paths, query_paths) =
30-
result.entry(relative_path.to_owned()).or_default();
31-
32-
if let Ok(example_files) = fs::read_dir(dir.join("examples")) {
33-
example_paths.extend(example_files.filter_map(|p| {
34-
let p = p.unwrap().path();
35-
if p.is_file() {
36-
Some(p)
37-
} else {
38-
None
39-
}
40-
}));
41-
}
23+
.unwrap_or(5)
24+
});
25+
static TEST_LOADER: LazyLock<Loader> =
26+
LazyLock::new(|| Loader::with_parser_lib_path(SCRATCH_DIR.clone()));
27+
28+
#[allow(clippy::type_complexity)]
29+
static EXAMPLE_AND_QUERY_PATHS_BY_LANGUAGE_DIR: LazyLock<
30+
BTreeMap<PathBuf, (Vec<PathBuf>, Vec<PathBuf>)>,
31+
> = LazyLock::new(|| {
32+
fn process_dir(result: &mut BTreeMap<PathBuf, (Vec<PathBuf>, Vec<PathBuf>)>, dir: &Path) {
33+
if dir.join("grammar.js").exists() {
34+
let relative_path = dir.strip_prefix(GRAMMARS_DIR.as_path()).unwrap();
35+
let (example_paths, query_paths) = result.entry(relative_path.to_owned()).or_default();
36+
37+
if let Ok(example_files) = fs::read_dir(dir.join("examples")) {
38+
example_paths.extend(example_files.filter_map(|p| {
39+
let p = p.unwrap().path();
40+
if p.is_file() {
41+
Some(p)
42+
} else {
43+
None
44+
}
45+
}));
46+
}
4247

43-
if let Ok(query_files) = fs::read_dir(dir.join("queries")) {
44-
query_paths.extend(query_files.filter_map(|p| {
45-
let p = p.unwrap().path();
46-
if p.is_file() {
47-
Some(p)
48-
} else {
49-
None
50-
}
51-
}));
52-
}
53-
} else {
54-
for entry in fs::read_dir(dir).unwrap() {
55-
let entry = entry.unwrap().path();
56-
if entry.is_dir() {
57-
process_dir(result, &entry);
48+
if let Ok(query_files) = fs::read_dir(dir.join("queries")) {
49+
query_paths.extend(query_files.filter_map(|p| {
50+
let p = p.unwrap().path();
51+
if p.is_file() {
52+
Some(p)
53+
} else {
54+
None
5855
}
56+
}));
57+
}
58+
} else {
59+
for entry in fs::read_dir(dir).unwrap() {
60+
let entry = entry.unwrap().path();
61+
if entry.is_dir() {
62+
process_dir(result, &entry);
5963
}
6064
}
6165
}
66+
}
6267

63-
let mut result = BTreeMap::new();
64-
process_dir(&mut result, &GRAMMARS_DIR);
65-
result
66-
};
67-
}
68+
let mut result = BTreeMap::new();
69+
process_dir(&mut result, &GRAMMARS_DIR);
70+
result
71+
});
6872

6973
fn main() {
7074
let max_path_length = EXAMPLE_AND_QUERY_PATHS_BY_LANGUAGE_DIR

cli/generate/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ anyhow.workspace = true
2020
heck.workspace = true
2121
indexmap.workspace = true
2222
indoc.workspace = true
23-
lazy_static.workspace = true
2423
log.workspace = true
2524
regex.workspace = true
2625
regex-syntax.workspace = true

cli/generate/src/build_tables/item.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::{
22
cmp::Ordering,
33
fmt,
44
hash::{Hash, Hasher},
5+
sync::LazyLock,
56
};
67

7-
use lazy_static::lazy_static;
8-
98
use crate::{
109
grammars::{
1110
LexicalGrammar, Production, ProductionStep, ReservedWordSetId, SyntaxGrammar,
@@ -14,22 +13,20 @@ use crate::{
1413
rules::{Associativity, Precedence, Symbol, SymbolType, TokenSet},
1514
};
1615

17-
lazy_static! {
18-
static ref START_PRODUCTION: Production = Production {
19-
dynamic_precedence: 0,
20-
steps: vec![ProductionStep {
21-
symbol: Symbol {
22-
index: 0,
23-
kind: SymbolType::NonTerminal,
24-
},
25-
precedence: Precedence::None,
26-
associativity: None,
27-
alias: None,
28-
field_name: None,
29-
reserved_word_set_id: NO_RESERVED_WORDS,
30-
}],
31-
};
32-
}
16+
static START_PRODUCTION: LazyLock<Production> = LazyLock::new(|| Production {
17+
dynamic_precedence: 0,
18+
steps: vec![ProductionStep {
19+
symbol: Symbol {
20+
index: 0,
21+
kind: SymbolType::NonTerminal,
22+
},
23+
precedence: Precedence::None,
24+
associativity: None,
25+
alias: None,
26+
field_name: None,
27+
reserved_word_set_id: NO_RESERVED_WORDS,
28+
}],
29+
});
3330

3431
/// A [`ParseItem`] represents an in-progress match of a single production in a grammar.
3532
#[derive(Clone, Copy, Debug)]

cli/generate/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use std::{
33
io::Write,
44
path::{Path, PathBuf},
55
process::{Command, Stdio},
6+
sync::LazyLock,
67
};
78

89
use anyhow::Result;
910
use build_tables::build_tables;
1011
use grammars::InputGrammar;
11-
use lazy_static::lazy_static;
1212
pub use node_types::VariableInfoError;
1313
use parse_grammar::parse_grammar;
1414
pub use parse_grammar::ParseGrammarError;
@@ -34,12 +34,12 @@ pub use build_tables::ParseTableBuilderError;
3434
use serde::Serialize;
3535
use thiserror::Error;
3636

37-
lazy_static! {
38-
static ref JSON_COMMENT_REGEX: Regex = RegexBuilder::new("^\\s*//.*")
37+
static JSON_COMMENT_REGEX: LazyLock<Regex> = LazyLock::new(|| {
38+
RegexBuilder::new("^\\s*//.*")
3939
.multi_line(true)
4040
.build()
41-
.unwrap();
42-
}
41+
.unwrap()
42+
});
4343

4444
struct GeneratedParser {
4545
c_code: String,

cli/loader/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ cc.workspace = true
2929
etcetera.workspace = true
3030
fs4.workspace = true
3131
indoc.workspace = true
32-
lazy_static.workspace = true
3332
libloading.workspace = true
3433
once_cell.workspace = true
3534
path-slash.workspace = true

cli/loader/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
mem,
1515
path::{Path, PathBuf},
1616
process::Command,
17+
sync::LazyLock,
1718
time::SystemTime,
1819
};
1920

@@ -23,7 +24,6 @@ use anyhow::{anyhow, Context, Result};
2324
use etcetera::BaseStrategy as _;
2425
use fs4::fs_std::FileExt;
2526
use indoc::indoc;
26-
use lazy_static::lazy_static;
2727
use libloading::{Library, Symbol};
2828
use once_cell::unsync::OnceCell;
2929
use path_slash::PathBufExt as _;
@@ -41,9 +41,8 @@ use tree_sitter_highlight::HighlightConfiguration;
4141
use tree_sitter_tags::{Error as TagsError, TagsConfiguration};
4242
use url::Url;
4343

44-
lazy_static! {
45-
static ref GRAMMAR_NAME_REGEX: Regex = Regex::new(r#""name":\s*"(.*?)""#).unwrap();
46-
}
44+
static GRAMMAR_NAME_REGEX: LazyLock<Regex> =
45+
LazyLock::new(|| Regex::new(r#""name":\s*"(.*?)""#).unwrap());
4746

4847
pub const EMSCRIPTEN_TAG: &str = concat!("docker.io/emscripten/emsdk:", env!("EMSCRIPTEN_VERSION"));
4948

cli/src/fuzz/mod.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::{collections::HashMap, env, fs, path::Path};
1+
use std::{collections::HashMap, env, fs, path::Path, sync::LazyLock};
22

3-
use lazy_static::lazy_static;
43
use rand::Rng;
54
use regex::Regex;
65
use tree_sitter::{Language, Parser};
@@ -23,16 +22,27 @@ use crate::{
2322
test::{parse_tests, print_diff, print_diff_key, strip_sexp_fields, TestEntry},
2423
};
2524

26-
lazy_static! {
27-
pub static ref LOG_ENABLED: bool = env::var("TREE_SITTER_LOG").is_ok();
28-
pub static ref LOG_GRAPH_ENABLED: bool = env::var("TREE_SITTER_LOG_GRAPHS").is_ok();
29-
pub static ref LANGUAGE_FILTER: Option<String> = env::var("TREE_SITTER_LANGUAGE").ok();
30-
pub static ref EXAMPLE_INCLUDE: Option<Regex> = regex_env_var("TREE_SITTER_EXAMPLE_INCLUDE");
31-
pub static ref EXAMPLE_EXCLUDE: Option<Regex> = regex_env_var("TREE_SITTER_EXAMPLE_EXCLUDE");
32-
pub static ref START_SEED: usize = new_seed();
33-
pub static ref EDIT_COUNT: usize = int_env_var("TREE_SITTER_EDITS").unwrap_or(3);
34-
pub static ref ITERATION_COUNT: usize = int_env_var("TREE_SITTER_ITERATIONS").unwrap_or(10);
35-
}
25+
pub static LOG_ENABLED: LazyLock<bool> = LazyLock::new(|| env::var("TREE_SITTER_LOG").is_ok());
26+
27+
pub static LOG_GRAPH_ENABLED: LazyLock<bool> =
28+
LazyLock::new(|| env::var("TREE_SITTER_LOG_GRAPHS").is_ok());
29+
30+
pub static LANGUAGE_FILTER: LazyLock<Option<String>> =
31+
LazyLock::new(|| env::var("TREE_SITTER_LANGUAGE").ok());
32+
33+
pub static EXAMPLE_INCLUDE: LazyLock<Option<Regex>> =
34+
LazyLock::new(|| regex_env_var("TREE_SITTER_EXAMPLE_INCLUDE"));
35+
36+
pub static EXAMPLE_EXCLUDE: LazyLock<Option<Regex>> =
37+
LazyLock::new(|| regex_env_var("TREE_SITTER_EXAMPLE_EXCLUDE"));
38+
39+
pub static START_SEED: LazyLock<usize> = LazyLock::new(new_seed);
40+
41+
pub static EDIT_COUNT: LazyLock<usize> =
42+
LazyLock::new(|| int_env_var("TREE_SITTER_EDITS").unwrap_or(3));
43+
44+
pub static ITERATION_COUNT: LazyLock<usize> =
45+
LazyLock::new(|| int_env_var("TREE_SITTER_ITERATIONS").unwrap_or(10));
3646

3747
fn int_env_var(name: &'static str) -> Option<usize> {
3848
env::var(name).ok().and_then(|e| e.parse().ok())

0 commit comments

Comments
 (0)