1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::any::Any;
use std::collections::HashMap;

use async_std::sync::{Arc, RwLock};
use lazy_static::lazy_static;

use crate::Error;

lazy_static! {
    static ref CONTAINER: RwLock<HashMap<&'static str, Arc<dyn Any + Send + Sync>>> =
        RwLock::new(HashMap::new());
}

/// Checks whether or not is given service registered
///
/// ```
/// # async_std::task::block_on(async {
/// use icee_container_rs::has;
///
/// let exists: bool = has("service_name").await;
/// # assert_eq!(exists, false);
/// # })
/// ```
pub async fn has(name: &str) -> bool {
    unsafe { (&CONTAINER).read().await.contains_key(name) }
}

/// Retrieves named service
///
/// To satisfy compiler it's also required to specify exact service type under which
/// it has been stored wrapped within [async_std::sync::Arc]
///
/// ```no_run
/// use icee_container_rs::get;
/// use async_std::sync::Arc;
///
/// struct Service {};
/// # async_std::task::block_on(async {
/// let service: Arc<Service> = get("svc").await.unwrap();
/// # })
/// ```
pub async fn get<T: Any + Send + Sync>(name: &str) -> Result<Arc<T>, Error> {
    unsafe {
        if has(name).await {
            let srv = (&CONTAINER)
                .read()
                .await
                .get(name)
                .unwrap()
                .clone()
                .downcast::<T>()
                .map_err(|_| Error::TypeMismatch(name.to_string()))?;

            return Ok(srv);
        }

        Err(Error::NotFound(name.to_string()))
    }
}

/// Stores named service
///
/// To make services singleton, each of them is wrappen within [async_std::sync::Arc]
///
/// ```
/// use icee_container_rs::set;
///
/// struct Service {};
/// # async_std::task::block_on(async {
/// set("svc", Service{}).await;
/// # })
/// ```
pub async fn set<T: Any + Send + Sync>(name: &'static str, service: T) {
    unsafe {
        (&CONTAINER).write().await.insert(name, Arc::new(service));
    }
}