1use std::{
2 any::{Any, TypeId, type_name},
3 collections::HashMap,
4 sync::Arc,
5};
6
7use crate::DependencyInjectionError;
8use parking_lot::RwLock;
9
10#[derive(Clone, Debug)]
15pub struct State {
16 inner: Arc<RwLock<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>>,
17}
18
19impl State {
20 pub fn new() -> Self {
22 Self {
23 inner: Arc::new(RwLock::new(HashMap::new())),
24 }
25 }
26
27 pub fn get<T>(&self) -> Result<T, DependencyInjectionError>
35 where
36 T: Clone + Send + Sync + 'static,
37 {
38 self.borrow::<T>().map(|value| (*value).clone())
39 }
40
41 pub fn borrow<T>(&self) -> Result<Arc<T>, DependencyInjectionError>
49 where
50 T: Send + Sync + 'static,
51 {
52 let map = self.inner.read();
53 let type_name = type_name::<T>().to_string();
54
55 let state_ref = map
56 .get(&TypeId::of::<T>())
57 .ok_or_else(|| DependencyInjectionError::dependency_not_found(type_name.clone()))?;
58
59 state_ref
60 .clone()
61 .downcast::<T>()
62 .map_err(|_| DependencyInjectionError::dependency_not_found(type_name))
63 }
64
65 pub fn insert<T: Send + Sync + 'static>(&self, state: T) {
66 self.inner
67 .write()
68 .insert(TypeId::of::<T>(), Arc::new(state));
69 }
70
71 pub(crate) fn insert_instance(&self, type_id: TypeId, instance: Arc<dyn Any + Send + Sync>) {
72 self.inner.write().insert(type_id, instance);
73 }
74}
75
76impl Default for State {
77 fn default() -> Self {
78 Self::new()
79 }
80}