Skip to content

Commit 2b216e6

Browse files
committed
Replace ancient lazy_static crate with once_cell or const slices
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro whereas `once_cell` achieves the same using generics. Its implementation has also been [proposed for inclusion in `std`], making it easier to switch to a standardized version if/when that happens. The author of that winit PR is making this change to many more crates, slowly turning the scales in favour of `once_cell` in most dependency trees. Furthermore `lazy_static` hasn't published any updates for 3 years. See also [the `once_cell` F.A.Q.]. In addition "constant" `Vec`tor allocations don't need to be wrapped in a `lazy_static!` macro call at all but can be replaced with true `const` slices (or `const` sized arrays, but those are slightly more tedious to construct). [motivation in winit]: rust-windowing/winit#2313 [proposed for inclusion in `std`]: rust-lang/rust#74465 [the `once_cell` F.A.Q.]: https://docs.rs/once_cell/latest/once_cell/#faq
1 parent 02c6775 commit 2b216e6

File tree

10 files changed

+88
-70
lines changed

10 files changed

+88
-70
lines changed

Diff for: sentry-backtrace/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Sentry integration and utilities for dealing with stacktraces.
1212
edition = "2018"
1313

1414
[dependencies]
15-
sentry-core = { version = "0.26.0", path = "../sentry-core" }
16-
lazy_static = "1.4.0"
1715
backtrace = "0.3.44"
16+
once_cell = "1"
1817
regex = "1.5.5"
18+
sentry-core = { version = "0.26.0", path = "../sentry-core" }

Diff for: sentry-backtrace/src/parse.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use once_cell::sync::Lazy;
12
use regex::Regex;
23

34
use crate::utils::{demangle_symbol, filename, strip_symbol};
45
use crate::{Frame, Stacktrace};
56

6-
lazy_static::lazy_static! {
7-
static ref FRAME_RE: Regex = Regex::new(r#"(?xm)
7+
static FRAME_RE: Lazy<Regex> = Lazy::new(|| {
8+
Regex::new(
9+
r#"(?xm)
810
^
911
\s*(?:\d+:)?\s* # frame number (missing for inline)
1012
@@ -27,8 +29,10 @@ lazy_static::lazy_static! {
2729
(?::(?P<colno>\d+))? # optional source column
2830
)?
2931
$
30-
"#).unwrap();
31-
}
32+
"#,
33+
)
34+
.unwrap()
35+
});
3236

3337
/// Parses a backtrace string into a Sentry `Stacktrace`.
3438
pub fn parse_stacktrace(bt: &str) -> Option<Stacktrace> {

Diff for: sentry-backtrace/src/trim.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@ use sentry_core::protocol::{Frame, Stacktrace};
22

33
use crate::utils::function_starts_with;
44

5-
lazy_static::lazy_static! {
6-
static ref WELL_KNOWN_SYS_MODULES: Vec<&'static str> = vec![
7-
"std::",
8-
"core::",
9-
"alloc::",
10-
"backtrace::",
11-
"sentry::",
12-
"sentry_core::",
13-
"sentry_types::",
14-
// these are not modules but things like __rust_maybe_catch_panic
15-
"__rust_",
16-
"___rust_",
17-
// these are well-known library frames
18-
"anyhow::",
19-
"log::",
20-
];
5+
const WELL_KNOWN_SYS_MODULES: &[&str] = &[
6+
"std::",
7+
"core::",
8+
"alloc::",
9+
"backtrace::",
10+
"sentry::",
11+
"sentry_core::",
12+
"sentry_types::",
13+
// these are not modules but things like __rust_maybe_catch_panic
14+
"__rust_",
15+
"___rust_",
16+
// these are well-known library frames
17+
"anyhow::",
18+
"log::",
19+
];
2120

22-
static ref WELL_KNOWN_BORDER_FRAMES: Vec<&'static str> = vec![
23-
"std::panicking::begin_panic",
24-
"core::panicking::panic",
25-
// well-known library frames
26-
"anyhow::",
27-
"<sentry_log::Logger as log::Log>::log",
28-
];
29-
}
21+
const WELL_KNOWN_BORDER_FRAMES: &[&str] = &[
22+
"std::panicking::begin_panic",
23+
"core::panicking::panic",
24+
// well-known library frames
25+
"anyhow::",
26+
"<sentry_log::Logger as log::Log>::log",
27+
];
3028

3129
/// A helper function to trim a stacktrace.
3230
pub fn trim_stacktrace<F>(stacktrace: &mut Stacktrace, f: F)

Diff for: sentry-backtrace/src/utils.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1+
use once_cell::sync::Lazy;
12
use regex::{Captures, Regex};
23

3-
lazy_static::lazy_static! {
4-
static ref HASH_FUNC_RE: Regex = Regex::new(r#"(?x)
4+
static HASH_FUNC_RE: Lazy<Regex> = Lazy::new(|| {
5+
Regex::new(
6+
r#"(?x)
57
^(.*)::h[a-f0-9]{16}$
6-
"#).unwrap();
7-
8-
static ref CRATE_RE: Regex = Regex::new(r#"(?x)
8+
"#,
9+
)
10+
.unwrap()
11+
});
12+
13+
static CRATE_RE: Lazy<Regex> = Lazy::new(|| {
14+
Regex::new(
15+
r#"(?x)
916
^
1017
(?:_?<)? # trait impl syntax
1118
(?:\w+\ as \ )? # anonymous implementor
1219
([a-zA-Z0-9_]+?) # crate name
1320
(?:\.\.|::) # crate delimiter (.. or ::)
14-
"#).unwrap();
15-
16-
static ref COMMON_RUST_SYMBOL_ESCAPES_RE: Regex = Regex::new(r#"(?x)
21+
"#,
22+
)
23+
.unwrap()
24+
});
25+
26+
static COMMON_RUST_SYMBOL_ESCAPES_RE: Lazy<Regex> = Lazy::new(|| {
27+
Regex::new(
28+
r#"(?x)
1729
\$
1830
(SP|BP|RF|LT|GT|LP|RP|C|
1931
u7e|u20|u27|u5b|u5d|u7b|u7d|u3b|u2b|u22)
2032
\$
21-
"#).unwrap();
22-
}
33+
"#,
34+
)
35+
.unwrap()
36+
});
2337

2438
/// Tries to parse the rust crate from a function name.
2539
pub fn parse_crate_name(func_name: &str) -> Option<String> {

Diff for: sentry-core/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ debug-logs = ["log_"]
2727
test = ["client"]
2828

2929
[dependencies]
30+
log_ = { package = "log", version = "0.4.8", optional = true, features = ["std"] }
31+
once_cell = "1"
32+
rand = { version = "0.8.1", optional = true }
3033
sentry-types = { version = "0.26.0", path = "../sentry-types" }
3134
serde = { version = "1.0.104", features = ["derive"] }
32-
lazy_static = "1.4.0"
33-
rand = { version = "0.8.1", optional = true }
3435
serde_json = "1.0.46"
35-
log_ = { package = "log", version = "0.4.8", optional = true, features = ["std"] }
3636

3737
[dev-dependencies]
3838
# Because we re-export all the public API in `sentry`, we actually run all the

Diff for: sentry-core/src/constants.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
use once_cell::sync::Lazy;
2+
13
use crate::protocol::{ClientSdkInfo, ClientSdkPackage};
24

35
/// The version of the library
46
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
7+
pub const USER_AGENT: &str = concat!("sentry.rust/", env!("CARGO_PKG_VERSION"));
58

6-
lazy_static::lazy_static! {
7-
pub static ref USER_AGENT: String = format!("sentry.rust/{}", VERSION);
8-
pub static ref SDK_INFO: ClientSdkInfo = ClientSdkInfo {
9-
name: "sentry.rust".into(),
9+
pub static SDK_INFO: Lazy<ClientSdkInfo> = Lazy::new(|| ClientSdkInfo {
10+
name: "sentry.rust".into(),
11+
version: VERSION.into(),
12+
packages: vec![ClientSdkPackage {
13+
name: "cargo:sentry".into(),
1014
version: VERSION.into(),
11-
packages: vec![ClientSdkPackage {
12-
name: "cargo:sentry".into(),
13-
version: VERSION.into(),
14-
}],
15-
integrations: vec![],
16-
};
17-
}
15+
}],
16+
integrations: vec![],
17+
});

Diff for: sentry-core/src/hub.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ use std::sync::{Arc, Mutex, PoisonError, RwLock, TryLockError};
88
use std::thread;
99
use std::time::Duration;
1010

11+
#[cfg(feature = "client")]
12+
use once_cell::sync::Lazy;
13+
1114
use crate::protocol::{Breadcrumb, Event, Level, SessionStatus};
1215
use crate::types::Uuid;
1316
use crate::{event_from_error, Integration, IntoBreadcrumbs, Scope, ScopeGuard};
1417
#[cfg(feature = "client")]
1518
use crate::{scope::Stack, session::Session, Client, Envelope};
1619

1720
#[cfg(feature = "client")]
18-
lazy_static::lazy_static! {
19-
static ref PROCESS_HUB: (Arc<Hub>, thread::ThreadId) = (
21+
static PROCESS_HUB: Lazy<(Arc<Hub>, thread::ThreadId)> = Lazy::new(|| {
22+
(
2023
Arc::new(Hub::new(None, Arc::new(Default::default()))),
21-
thread::current().id()
22-
);
23-
}
24+
thread::current().id(),
25+
)
26+
});
2427

2528
#[cfg(feature = "client")]
2629
thread_local! {

Diff for: sentry-core/src/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
2222
use std::sync::{Arc, Mutex};
2323

24+
use once_cell::sync::Lazy;
25+
2426
use crate::protocol::Event;
2527
use crate::types::Dsn;
2628
use crate::{ClientOptions, Envelope, Hub, Transport};
2729

28-
lazy_static::lazy_static! {
29-
static ref TEST_DSN: Dsn = "https://[email protected]/1".parse().unwrap();
30-
}
30+
static TEST_DSN: Lazy<Dsn> = Lazy::new(|| "https://[email protected]/1".parse().unwrap());
3131

3232
/// Collects events instead of sending them.
3333
///

Diff for: sentry-debug-images/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ Sentry integration that adds the list of loaded libraries to events.
1212
edition = "2018"
1313

1414
[dependencies]
15-
sentry-core = { version = "0.26.0", path = "../sentry-core" }
16-
lazy_static = "1.4.0"
1715
findshlibs = "=0.10.2"
16+
once_cell = "1"
17+
sentry-core = { version = "0.26.0", path = "../sentry-core" }

Diff for: sentry-debug-images/src/integration.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22

3+
use once_cell::sync::Lazy;
34
use sentry_core::protocol::{DebugMeta, Event};
45
use sentry_core::{ClientOptions, Integration};
56

@@ -55,12 +56,10 @@ impl Integration for DebugImagesIntegration {
5556
mut event: Event<'static>,
5657
_opts: &ClientOptions,
5758
) -> Option<Event<'static>> {
58-
lazy_static::lazy_static! {
59-
static ref DEBUG_META: DebugMeta = DebugMeta {
60-
images: crate::debug_images(),
61-
..Default::default()
62-
};
63-
}
59+
static DEBUG_META: Lazy<DebugMeta> = Lazy::new(|| DebugMeta {
60+
images: crate::debug_images(),
61+
..Default::default()
62+
});
6463

6564
if event.debug_meta.is_empty() && (self.filter)(&event) {
6665
event.debug_meta = Cow::Borrowed(&DEBUG_META);

0 commit comments

Comments
 (0)