Skip to content

Commit 8d9182a

Browse files
authored
refactor!: replace lazy_static with std::sync::OnceLock (#127)
1 parent 7f74461 commit 8d9182a

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "open-feature"
33
version = "0.2.7"
44
edition = "2021"
5-
rust-version = "1.77.0" # MSRV
5+
rust-version = "1.80.1" # MSRV
66
description = "The official OpenFeature Rust SDK."
77
documentation = "https://docs.rs/open-feature"
88
readme = "README.md"
@@ -17,11 +17,10 @@ maintenance = { status = "actively-developed" }
1717

1818
[dependencies]
1919
async-trait = "0.1.80"
20-
lazy_static = "1.5"
2120
mockall = { version = "0.14.0", optional = true }
2221
serde_json = { version = "1.0.116", optional = true }
2322
time = "0.3.36"
24-
tokio = { version = "1.40", features = ["full"] }
23+
tokio = { version = "1.40", features = ["sync"] }
2524
typed-builder = "0.22.0"
2625

2726
log = { package = "log", version = "0.4", optional = true }
@@ -30,6 +29,7 @@ log = { package = "log", version = "0.4", optional = true }
3029
env_logger = "0.11.5"
3130
structured-logger = "1.0.3"
3231
spec = { path = "spec" }
32+
tokio = { version = "1.40", features = ["sync", "rt-multi-thread", "macros"] }
3333

3434
[features]
3535
default = ["test-util", "dep:log"]

src/api/api.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use lazy_static::lazy_static;
1+
use std::sync::OnceLock;
2+
23
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
34

45
use crate::{
@@ -11,14 +12,16 @@ use super::{
1112
provider_registry::ProviderRegistry,
1213
};
1314

14-
lazy_static! {
15-
/// The singleton instance of [`OpenFeature`] struct.
16-
/// The client should always use this instance to access OpenFeature APIs.
17-
static ref SINGLETON: RwLock<OpenFeature> = RwLock::new(OpenFeature::default());
15+
/// The singleton instance of [`OpenFeature`] struct.
16+
/// The client should always use this instance to access OpenFeature APIs.
17+
static SINGLETON: OnceLock<RwLock<OpenFeature>> = OnceLock::new();
18+
19+
fn get_singleton() -> &'static RwLock<OpenFeature> {
20+
SINGLETON.get_or_init(|| RwLock::new(OpenFeature::default()))
1821
}
1922

2023
/// THE struct of the OpenFeature API.
21-
/// Access it via the [`SINGLETON`] instance.
24+
/// Access it via [`OpenFeature::singleton()`] or [`OpenFeature::singleton_mut()`].
2225
#[derive(Default)]
2326
pub struct OpenFeature {
2427
evaluation_context: GlobalEvaluationContext,
@@ -30,12 +33,12 @@ pub struct OpenFeature {
3033
impl OpenFeature {
3134
/// Get the singleton of [`OpenFeature`].
3235
pub async fn singleton() -> RwLockReadGuard<'static, Self> {
33-
SINGLETON.read().await
36+
get_singleton().read().await
3437
}
3538

3639
/// Get a mutable singleton of [`OpenFeature`].
3740
pub async fn singleton_mut() -> RwLockWriteGuard<'static, Self> {
38-
SINGLETON.write().await
41+
get_singleton().write().await
3942
}
4043

4144
/// Set the global evaluation context.

0 commit comments

Comments
 (0)