1- use lazy_static:: lazy_static;
1+ use std:: sync:: OnceLock ;
2+
23use tokio:: sync:: { RwLock , RwLockReadGuard , RwLockWriteGuard } ;
34
45use 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 ) ]
2326pub struct OpenFeature {
2427 evaluation_context : GlobalEvaluationContext ,
@@ -30,12 +33,12 @@ pub struct OpenFeature {
3033impl 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