Skip to content

Commit 98eac93

Browse files
committed
refactor: Replace lazy_static with std::sync::LazyLock
std::cell::LazyCell and std::sync::LazyLock were stabilized in Rust 1.80.0. We can now use the standard library types instead of the lazy_static crate.
1 parent e74b0c1 commit 98eac93

File tree

14 files changed

+64
-54
lines changed

14 files changed

+64
-54
lines changed

Cargo.lock

Lines changed: 0 additions & 2 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
@@ -32,7 +32,6 @@ json-patch = "2.0.0"
3232
k8s-openapi = { version = "0.22.0", default-features = false, features = ["schemars", "v1_30"] }
3333
# We use rustls instead of openssl for easier portablitly, e.g. so that we can build stackablectl without the need to vendor (build from source) openssl
3434
kube = { version = "0.92.1", default-features = false, features = ["client", "jsonpatch", "runtime", "derive", "rustls-tls"] }
35-
lazy_static = "1.5.0"
3635
opentelemetry = "0.23.0"
3736
opentelemetry_sdk = { version = "0.23.0", features = ["rt-tokio"] }
3837
opentelemetry-appender-tracing = "0.4.0"

crates/k8s-version/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Changed
8+
9+
- Replace `lazy_static` with `std::cell::LazyCell` ([#xxx]).
10+
11+
[#xxx](https://github.com/stackabletech/operator-rs/pull/xxx)
12+
713
## [0.1.1] - 2024-07-10
814

915
### Changed

crates/k8s-version/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ darling = ["dep:darling"]
1111

1212
[dependencies]
1313
darling = { workspace = true, optional = true }
14-
lazy_static.workspace = true
1514
regex.workspace = true
1615
snafu.workspace = true
1716

crates/k8s-version/src/group.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use std::{fmt, ops::Deref, str::FromStr};
1+
use std::{fmt, ops::Deref, str::FromStr, sync::LazyLock};
22

3-
use lazy_static::lazy_static;
43
use regex::Regex;
54
use snafu::{ensure, Snafu};
65

76
const MAX_GROUP_LENGTH: usize = 253;
87

9-
lazy_static! {
10-
static ref API_GROUP_REGEX: Regex =
11-
Regex::new(r"^(?:(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9])\.?)+$")
12-
.expect("failed to compile API group regex");
13-
}
8+
static API_GROUP_REGEX: LazyLock<Regex> = LazyLock::new(|| {
9+
Regex::new(r"^(?:(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9])\.?)+$")
10+
.expect("failed to compile API group regex")
11+
});
1412

1513
/// Error variants which can be encountered when creating a new [`Group`] from
1614
/// unparsed input.

crates/k8s-version/src/level.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ use std::{
44
num::ParseIntError,
55
ops::{Add, AddAssign, Sub, SubAssign},
66
str::FromStr,
7+
sync::LazyLock,
78
};
89

9-
use lazy_static::lazy_static;
1010
use regex::Regex;
1111
use snafu::{OptionExt, ResultExt, Snafu};
1212

1313
#[cfg(feature = "darling")]
1414
use darling::FromMeta;
1515

16-
lazy_static! {
17-
static ref LEVEL_REGEX: Regex = Regex::new(r"^(?P<identifier>[a-z]+)(?P<version>\d+)$")
18-
.expect("failed to compile level regex");
19-
}
16+
static LEVEL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
17+
Regex::new(r"^(?P<identifier>[a-z]+)(?P<version>\d+)$").expect("failed to compile level regex")
18+
});
2019

2120
/// Error variants which can be encountered when creating a new [`Level`] from
2221
/// unparsed input.

crates/k8s-version/src/version.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::{cmp::Ordering, fmt::Display, num::ParseIntError, str::FromStr};
1+
use std::{cmp::Ordering, fmt::Display, num::ParseIntError, str::FromStr, sync::LazyLock};
22

3-
use lazy_static::lazy_static;
43
use regex::Regex;
54
use snafu::{OptionExt, ResultExt, Snafu};
65

@@ -9,11 +8,10 @@ use darling::FromMeta;
98

109
use crate::{Level, ParseLevelError};
1110

12-
lazy_static! {
13-
static ref VERSION_REGEX: Regex =
14-
Regex::new(r"^v(?P<major>\d+)(?P<level>[a-z0-9][a-z0-9-]{0,60}[a-z0-9])?$")
15-
.expect("failed to compile version regex");
16-
}
11+
static VERSION_REGEX: LazyLock<Regex> = LazyLock::new(|| {
12+
Regex::new(r"^v(?P<major>\d+)(?P<level>[a-z0-9][a-z0-9-]{0,60}[a-z0-9])?$")
13+
.expect("failed to compile version regex")
14+
});
1715

1816
/// Error variants which can be encountered when creating a new [`Version`] from
1917
/// unparsed input.

crates/stackable-operator/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Changed
8+
9+
- Replace `lazy_static` with `std::cell::LazyCell` ([#xxx]).
10+
11+
[#xxx](https://github.com/stackabletech/operator-rs/pull/xxx)
12+
713
## [0.70.0] - 2024-07-10
814

915
### Added

crates/stackable-operator/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ futures.workspace = true
2424
json-patch.workspace = true
2525
k8s-openapi.workspace = true
2626
kube.workspace = true
27-
lazy_static.workspace = true
2827
opentelemetry_sdk.workspace = true
2928
opentelemetry-jaeger.workspace = true
3029
product-config.workspace = true

crates/stackable-operator/src/commons/opa.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@
4343
//! assert_eq!(opa_config.document_url(&cluster, Some("allow"), OpaApiVersion::V1), "v1/data/test/allow".to_string());
4444
//! assert_eq!(opa_config.full_document_url(&cluster, "http://localhost:8081", None, OpaApiVersion::V1), "http://localhost:8081/v1/data/test".to_string());
4545
//! ```
46+
use std::sync::LazyLock;
47+
4648
use crate::client::{Client, GetApi};
4749
use k8s_openapi::{api::core::v1::ConfigMap, NamespaceResourceScope};
4850
use kube::{Resource, ResourceExt};
49-
use lazy_static::lazy_static;
5051
use regex::Regex;
5152
use schemars::{self, JsonSchema};
5253
use serde::{Deserialize, Serialize};
5354
use snafu::{OptionExt, ResultExt, Snafu};
5455

56+
static DOT_REGEX: LazyLock<Regex> =
57+
LazyLock::new(|| Regex::new("\\.").expect("failed to compile OPA dot regex"));
58+
59+
/// To remove leading slashes from OPA package name (if present)
60+
static LEADING_SLASH_REGEX: LazyLock<Regex> =
61+
LazyLock::new(|| Regex::new("(/*)(.*)").expect("failed to compile OPA leasing slash regex"));
62+
5563
type Result<T, E = Error> = std::result::Result<T, E>;
5664

5765
#[derive(Debug, Snafu)]
@@ -72,11 +80,6 @@ pub enum Error {
7280
},
7381
}
7482

75-
lazy_static! {
76-
static ref DOT_REGEX: Regex = Regex::new("\\.").unwrap();
77-
/// To remove leading slashes from OPA package name (if present)
78-
static ref LEADING_SLASH_REGEX: Regex = Regex::new("(/*)(.*)").unwrap();
79-
}
8083
/// Indicates the OPA API version. This is required to choose the correct
8184
/// path when constructing the OPA urls to query.
8285
pub enum OpaApiVersion {

0 commit comments

Comments
 (0)