Repository: getsentry/sentry-rust
Pull request #776 (2025-04-24 17:21:41)

Replace `once_cell` with `std::sync::LazyLock`

FalkWoldmann: As far as I can tell, the README mentions an MSRV of 1.81, and since LazyLock stabilized in 1.80, it seems like it should be okay —does that sound right? :)

Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 58e0fae..5640f5d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3184,7 +3184,6 @@ name = "sentry-backtrace"
 version = "0.37.0"
 dependencies = [
  "backtrace",
- "once_cell",
  "regex",
  "sentry-core",
 ]
@@ -3212,7 +3211,6 @@ dependencies = [
  "criterion",
  "futures",
  "log",
- "once_cell",
  "rand 0.9.0",
  "rayon",
  "regex",
@@ -3230,7 +3228,6 @@ name = "sentry-debug-images"
 version = "0.37.0"
 dependencies = [
  "findshlibs",
- "once_cell",
  "sentry-core",
 ]
 
diff --git a/sentry-backtrace/Cargo.toml b/sentry-backtrace/Cargo.toml
index a3f58eb..30c9a63 100644
--- a/sentry-backtrace/Cargo.toml
+++ b/sentry-backtrace/Cargo.toml
@@ -14,7 +14,6 @@ rust-version = "1.81"
 
 [dependencies]
 backtrace = "0.3.44"
-once_cell = "1"
 regex = { version = "1.5.5", default-features = false, features = [
     "std",
     "unicode-perl",
diff --git a/sentry-backtrace/src/parse.rs b/sentry-backtrace/src/parse.rs
index 03f3df8..f2e82fc 100644
--- a/sentry-backtrace/src/parse.rs
+++ b/sentry-backtrace/src/parse.rs
@@ -1,10 +1,11 @@
-use once_cell::sync::Lazy;
+use std::sync::LazyLock;
+
 use regex::Regex;
 
 use crate::utils::{demangle_symbol, filename, strip_symbol};
 use crate::{Frame, Stacktrace};
 
-static FRAME_RE: Lazy<Regex> = Lazy::new(|| {
+static FRAME_RE: LazyLock<Regex> = LazyLock::new(|| {
     Regex::new(
         r#"(?xm)
         ^
diff --git a/sentry-backtrace/src/utils.rs b/sentry-backtrace/src/utils.rs
index f638eed..fa8b044 100644
--- a/sentry-backtrace/src/utils.rs
+++ b/sentry-backtrace/src/utils.rs
@@ -1,9 +1,8 @@
-use std::borrow::Cow;
+use std::{borrow::Cow, sync::LazyLock};
 
-use once_cell::sync::Lazy;
 use regex::{Captures, Regex};
 
-static HASH_FUNC_RE: Lazy<Regex> = Lazy::new(|| {
+static HASH_FUNC_RE: LazyLock<Regex> = LazyLock::new(|| {
     Regex::new(
         r#"(?x)
         ^(.*)::h[a-f0-9]{16}$
@@ -12,7 +11,7 @@ static HASH_FUNC_RE: Lazy<Regex> = Lazy::new(|| {
     .unwrap()
 });
 
-static CRATE_HASH_RE: Lazy<Regex> = Lazy::new(|| {
+static CRATE_HASH_RE: LazyLock<Regex> = LazyLock::new(|| {
     Regex::new(
         r"(?x)
         \b(\[[a-f0-9]{16}\])
@@ -21,7 +20,7 @@ static CRATE_HASH_RE: Lazy<Regex> = Lazy::new(|| {
     .unwrap()
 });
 
-static CRATE_RE: Lazy<Regex> = Lazy::new(|| {
+static CRATE_RE: LazyLock<Regex> = LazyLock::new(|| {
     Regex::new(
         r"(?x)
         ^
@@ -34,7 +33,7 @@ static CRATE_RE: Lazy<Regex> = Lazy::new(|| {
     .unwrap()
 });
 
-static COMMON_RUST_SYMBOL_ESCAPES_RE: Lazy<Regex> = Lazy::new(|| {
+static COMMON_RUST_SYMBOL_ESCAPES_RE: LazyLock<Regex> = LazyLock::new(|| {
     Regex::new(
         r"(?x)
         \$
diff --git a/sentry-core/Cargo.toml b/sentry-core/Cargo.toml
index 09b4574..4c101ea 100644
--- a/sentry-core/Cargo.toml
+++ b/sentry-core/Cargo.toml
@@ -32,7 +32,6 @@ release-health = []
 cadence = { version = "1.4.0", optional = true }
 crc32fast = { version = "1.4.0", optional = true }
 log = { version = "0.4.8", optional = true, features = ["std"] }
-once_cell = "1"
 rand = { version = "0.9.0", optional = true }
 regex = { version = "1.7.3", optional = true }
 sentry-types = { version = "0.37.0", path = "../sentry-types" }
diff --git a/sentry-core/src/constants.rs b/sentry-core/src/constants.rs
index f33a606..d2ce87f 100644
--- a/sentry-core/src/constants.rs
+++ b/sentry-core/src/constants.rs
@@ -1,7 +1,7 @@
 // Not all constants are used when building without the "client" feature
 #![allow(dead_code)]
 
-use once_cell::sync::Lazy;
+use std::sync::LazyLock;
 
 use crate::protocol::{ClientSdkInfo, ClientSdkPackage};
 
@@ -9,7 +9,7 @@ use crate::protocol::{ClientSdkInfo, ClientSdkPackage};
 const VERSION: &str = env!("CARGO_PKG_VERSION");
 pub(crate) const USER_AGENT: &str = concat!("sentry.rust/", env!("CARGO_PKG_VERSION"));
 
-pub(crate) static SDK_INFO: Lazy<ClientSdkInfo> = Lazy::new(|| ClientSdkInfo {
+pub(crate) static SDK_INFO: LazyLock<ClientSdkInfo> = LazyLock::new(|| ClientSdkInfo {
     name: "sentry.rust".into(),
     version: VERSION.into(),
     packages: vec![ClientSdkPackage {
diff --git a/sentry-core/src/hub_impl.rs b/sentry-core/src/hub_impl.rs
index 42d1fa6..5786daa 100644
--- a/sentry-core/src/hub_impl.rs
+++ b/sentry-core/src/hub_impl.rs
@@ -1,13 +1,11 @@
 use std::cell::{Cell, UnsafeCell};
-use std::sync::{Arc, PoisonError, RwLock};
+use std::sync::{Arc, LazyLock, PoisonError, RwLock};
 use std::thread;
 
 use crate::Scope;
 use crate::{scope::Stack, Client, Hub};
 
-use once_cell::sync::Lazy;
-
-static PROCESS_HUB: Lazy<(Arc<Hub>, thread::ThreadId)> = Lazy::new(|| {
+static PROCESS_HUB: LazyLock<(Arc<Hub>, thread::ThreadId)> = LazyLock::new(|| {
     (
         Arc::new(Hub::new(None, Arc::new(Default::default()))),
         thread::current().id(),
diff --git a/sentry-debug-images/Cargo.toml b/sentry-debug-images/Cargo.toml
index 7f4df4c..035dcc5 100644
--- a/sentry-debug-images/Cargo.toml
+++ b/sentry-debug-images/Cargo.toml
@@ -14,5 +14,4 @@ rust-version = "1.81"
 
 [dependencies]
 findshlibs = "=0.10.2"
-once_cell = "1"
 sentry-core = { version = "0.37.0", path = "../sentry-core" }
diff --git a/sentry-debug-images/src/integration.rs b/sentry-debug-images/src/integration.rs
index 2a60e17..0039045 100644
--- a/sentry-debug-images/src/integration.rs
+++ b/sentry-debug-images/src/integration.rs
@@ -1,6 +1,6 @@
 use std::borrow::Cow;
+use std::sync::LazyLock;
 
-use once_cell::sync::Lazy;
 use sentry_core::protocol::{DebugMeta, Event};
 use sentry_core::{ClientOptions, Integration};
 
@@ -56,7 +56,7 @@ impl Integration for DebugImagesIntegration {
         mut event: Event<'static>,
         _opts: &ClientOptions,
     ) -> Option<Event<'static>> {
-        static DEBUG_META: Lazy<DebugMeta> = Lazy::new(|| DebugMeta {
+        static DEBUG_META: LazyLock<DebugMeta> = LazyLock::new(|| DebugMeta {
             images: crate::debug_images(),
             ..Default::default()
         });


Test patch:
diff --git a/sentry-core/src/test.rs b/sentry-core/src/test.rs
index ed92c1c..a2e150f 100644
--- a/sentry-core/src/test.rs
+++ b/sentry-core/src/test.rs
@@ -19,15 +19,14 @@
 //! assert_eq!(events[0].message.as_ref().unwrap(), "Hello World!");
 //! ```
 
-use std::sync::{Arc, Mutex};
-
-use once_cell::sync::Lazy;
+use std::sync::{Arc, LazyLock, Mutex};
 
 use crate::protocol::Event;
 use crate::types::Dsn;
 use crate::{ClientOptions, Envelope, Hub, Transport};
 
-static TEST_DSN: Lazy<Dsn> = Lazy::new(|| "https://public@sentry.invalid/1".parse().unwrap());
+static TEST_DSN: LazyLock<Dsn> =
+    LazyLock::new(|| "https://public@sentry.invalid/1".parse().unwrap());
 
 /// Collects events instead of sending them.
 ///