diff --git a/Cargo.toml b/Cargo.toml index 6212d57..ab43fef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "open-feature" version = "0.2.7" edition = "2021" -rust-version = "1.77.0" # MSRV +rust-version = "1.80.1" # MSRV description = "The official OpenFeature Rust SDK." documentation = "https://docs.rs/open-feature" readme = "README.md" @@ -17,11 +17,10 @@ maintenance = { status = "actively-developed" } [dependencies] async-trait = "0.1.80" -lazy_static = "1.5" mockall = { version = "0.14.0", optional = true } serde_json = { version = "1.0.116", optional = true } time = "0.3.36" -tokio = { version = "1.40", features = ["full"] } +tokio = { version = "1.40", features = ["sync"] } typed-builder = "0.22.0" log = { package = "log", version = "0.4", optional = true } @@ -30,6 +29,7 @@ log = { package = "log", version = "0.4", optional = true } env_logger = "0.11.5" structured-logger = "1.0.3" spec = { path = "spec" } +tokio = { version = "1.40", features = ["sync", "rt-multi-thread", "macros"] } [features] default = ["test-util", "dep:log"] diff --git a/src/api/api.rs b/src/api/api.rs index 3344be7..4207957 100644 --- a/src/api/api.rs +++ b/src/api/api.rs @@ -1,4 +1,5 @@ -use lazy_static::lazy_static; +use std::sync::OnceLock; + use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::{ @@ -11,14 +12,16 @@ use super::{ provider_registry::ProviderRegistry, }; -lazy_static! { - /// The singleton instance of [`OpenFeature`] struct. - /// The client should always use this instance to access OpenFeature APIs. - static ref SINGLETON: RwLock = RwLock::new(OpenFeature::default()); +/// The singleton instance of [`OpenFeature`] struct. +/// The client should always use this instance to access OpenFeature APIs. +static SINGLETON: OnceLock> = OnceLock::new(); + +fn get_singleton() -> &'static RwLock { + SINGLETON.get_or_init(|| RwLock::new(OpenFeature::default())) } /// THE struct of the OpenFeature API. -/// Access it via the [`SINGLETON`] instance. +/// Access it via [`OpenFeature::singleton()`] or [`OpenFeature::singleton_mut()`]. #[derive(Default)] pub struct OpenFeature { evaluation_context: GlobalEvaluationContext, @@ -30,12 +33,12 @@ pub struct OpenFeature { impl OpenFeature { /// Get the singleton of [`OpenFeature`]. pub async fn singleton() -> RwLockReadGuard<'static, Self> { - SINGLETON.read().await + get_singleton().read().await } /// Get a mutable singleton of [`OpenFeature`]. pub async fn singleton_mut() -> RwLockWriteGuard<'static, Self> { - SINGLETON.write().await + get_singleton().write().await } /// Set the global evaluation context.