From df274eae122c388866423c6fa578dcc08af45c40 Mon Sep 17 00:00:00 2001 From: David Weis Date: Thu, 23 Nov 2023 15:16:32 +0000 Subject: [PATCH] Make singleton constructor private --- src/ioc_container.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ioc_container.rs b/src/ioc_container.rs index 69df1bd..07310f7 100644 --- a/src/ioc_container.rs +++ b/src/ioc_container.rs @@ -16,12 +16,18 @@ pub enum IocContainerError { type Handle = Arc; -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct IocContainer { map: Arc>>, } impl IocContainer { + fn new() -> Self { + Self { + map: Arc::new(Mutex::new(HashMap::new())), + } + } + pub fn register(&self, object: T) { let type_id = object.type_id(); self.map.lock().unwrap().insert(type_id, Arc::new(object)); @@ -51,7 +57,7 @@ impl IocContainer { pub fn global_instance() -> &'static IocContainer { static INSTANCE: OnceCell = OnceCell::new(); - INSTANCE.get_or_init(IocContainer::default) + INSTANCE.get_or_init(IocContainer::new) } } @@ -63,7 +69,7 @@ mod tests { #[test] fn simple_ioc() { - let container = IocContainer::default(); + let container = IocContainer::new(); container.register(A); let a = container.get::(); assert!(a.is_some()); @@ -71,7 +77,7 @@ mod tests { #[test] fn fail_on_unregistered_type() { - let container = IocContainer::default(); + let container = IocContainer::new(); let not_a = container.get::(); assert!(not_a.is_none()) }