Skip to content

Commit 3f44645

Browse files
committed
Migrate from lazy_static to LazyLock
The standard library provides `LazyLock` since 1.80, so we no longer need `lazy_static` as a dependency. This migrates usages of that crate to the standard library. This is unrelated to the crash fix. Test Plan: cargo test Pull Request: #458
1 parent 1fb7364 commit 3f44645

File tree

7 files changed

+19
-25
lines changed

7 files changed

+19
-25
lines changed

.changeset/smart-eagles-shout.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@atlaspack/rust': patch
3+
---
4+
5+
Migrate to LazyLock from lazy_static

Cargo.lock

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/caniuse_database/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ workspace = true
88

99
[dependencies]
1010
browserslist-rs = { workspace = true }
11-
lazy_static = { workspace = true }
1211
thiserror = { workspace = true }
1312
tinyvec = { workspace = true, features = ["alloc", "serde"] }
1413
serde = { workspace = true }

crates/caniuse_database/src/lib.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
//! * Technology preview / non-numeric versions are not supported.
4949
//!
5050
use std::collections::HashMap;
51-
52-
use lazy_static::lazy_static;
51+
use std::sync::LazyLock;
5352

5453
pub use generated::*;
5554
pub use permissive_semver::*;
@@ -66,12 +65,10 @@ type BrowserAgentKey = String;
6665
/// The `data`/stats field in caniuse JSON data. This is generated with `generate.mjs`
6766
type BrowserFeatureStats = HashMap<BrowserFeatureKey, HashMap<BrowserAgentKey, BrowserFeatureStat>>;
6867

69-
lazy_static! {
70-
static ref BROWSER_FEATURE_STATS: BrowserFeatureStats =
71-
serde_json::from_str(BROWSER_FEATURE_STATS_JSON).unwrap();
72-
static ref BROWSER_FEATURES: BrowsersFeaturesData =
73-
BrowsersFeaturesData::new(&BROWSER_FEATURE_STATS).unwrap();
74-
}
68+
static BROWSER_FEATURE_STATS: LazyLock<BrowserFeatureStats> =
69+
LazyLock::new(|| serde_json::from_str(BROWSER_FEATURE_STATS_JSON).unwrap());
70+
static BROWSER_FEATURES: LazyLock<BrowsersFeaturesData> =
71+
LazyLock::new(|| BrowsersFeaturesData::new(&BROWSER_FEATURE_STATS).unwrap());
7572

7673
type BrowserFeatureStat = HashMap<String, u8>;
7774

crates/lmdb-js-lite/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ crate-type = ["cdylib", "rlib"]
1818
anyhow = { workspace = true }
1919
crossbeam = { workspace = true }
2020
heed = { workspace = true }
21-
lazy_static = { workspace = true }
2221
napi = { workspace = true, features = [
2322
"async",
2423
"napi4",

crates/lmdb-js-lite/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838

3939
use std::collections::HashMap;
4040
use std::fmt::Debug;
41+
use std::sync::LazyLock;
4142
use std::sync::{Arc, Mutex, Weak};
4243

4344
use anyhow::anyhow;
44-
use lazy_static::lazy_static;
4545
use napi::bindgen_prelude::Env;
4646
use napi::JsUnknown;
4747
use napi_derive::napi;
@@ -120,9 +120,8 @@ impl LMDBGlobalState {
120120
}
121121
}
122122

123-
lazy_static! {
124-
static ref STATE: Mutex<LMDBGlobalState> = Mutex::new(LMDBGlobalState::new());
125-
}
123+
static STATE: LazyLock<Mutex<LMDBGlobalState>> =
124+
LazyLock::new(|| Mutex::new(LMDBGlobalState::new()));
126125

127126
#[napi]
128127
pub fn init_tracing_subscriber() {

packages/transformers/js/core/src/magic_comments/visitor.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::collections::HashMap;
22

33
use regex::Regex;
4+
use std::sync::LazyLock;
45
use swc_core::ecma::ast::*;
56
use swc_core::ecma::visit::Visit;
67
use swc_core::ecma::visit::VisitWith;
78

8-
thread_local! {
9-
static RE_CHUNK_NAME: Regex = Regex::new(r#"webpackChunkName:\s*['"](?<name>[^'"]+)['"]"#).unwrap();
10-
}
11-
9+
static RE_CHUNK_NAME: LazyLock<Regex> =
10+
LazyLock::new(|| Regex::new(r#"webpackChunkName:\s*['"](?<name>[^'"]+)['"]"#).unwrap());
1211
const MAGIC_COMMENT_DEFAULT_KEYWORD: &str = "webpackChunkName";
1312

1413
/// MagicCommentsVisitor will scan code for Webpack Magic Comments
@@ -66,9 +65,7 @@ impl Visit for MagicCommentsVisitor<'_> {
6665
}
6766

6867
fn match_re(src: &str) -> Option<String> {
69-
RE_CHUNK_NAME.with(|re| {
70-
let caps = re.captures(src)?;
71-
let found = caps.name("name")?;
72-
Some(found.as_str().to_string())
73-
})
68+
RE_CHUNK_NAME
69+
.captures(src)
70+
.and_then(|caps| caps.name("name").map(|found| found.as_str().to_string()))
7471
}

0 commit comments

Comments
 (0)