use crate::rudi::dicontainer::container;
use std::any::Any;
use std::sync::{Arc, OnceLock, RwLock};
static GLOBAL: OnceLock<RwLock<container::Container>> = OnceLock::new();
fn global() -> &'static RwLock<container::Container> {
GLOBAL.get_or_init(|| {
let mut c = container::Container::new();
container::bootstrap(&mut c);
RwLock::new(c)
})
}
pub fn register<F, T>(key: &str, factory: F)
where
F: Fn() -> T + Send + Sync + 'static,
T: Any + Send + Sync + 'static,
{
global().write().unwrap().register(key, factory);
}
pub fn register_singleton<F, T>(key: &str, factory: F)
where
F: Fn() -> T + Send + Sync + 'static,
T: Any + Send + Sync + 'static,
{
global().write().unwrap().register_singleton(key, factory);
}
pub fn get<T: Any + Clone>(key: &str) -> Option<T> {
global().read().unwrap().get(key)
}
pub fn get_singleton<T: Any + Send + Sync>(key: &str) -> Option<Arc<T>> {
global().read().unwrap().get_singleton(key)
}
pub fn get_bean<T: Any + Send + Sync>(key: &str) -> Option<Arc<T>> {
global().read().unwrap().get_bean(key)
}