aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/async_injectable.rs
blob: cf903e7743103aeac168f821e012230542a8bf5c (plain)
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
//! Interface for structs that can be injected into or be injected to.
use std::fmt::Debug;
use std::future::ready;
use std::rc::Rc;
use std::sync::Arc;

use crate::errors::injectable::InjectableError;
use crate::future::BoxFuture;
use crate::ptr::TransientPtr;
use crate::ptr_buffer::PtrBuffer;
use crate::util::use_double;

use_double!(crate::dependency_history::DependencyHistory);

/// Interface for structs that can be injected into or be injected to.
pub trait AsyncInjectable<DIContainerT>: 'static + Send + Sync
{
    /// Resolves the dependencies of the injectable.
    ///
    /// # Errors
    /// Will return `Err` if resolving the dependencies fails.
    fn resolve<'di_container, 'fut>(
        di_container: &'di_container DIContainerT,
        dependency_history: DependencyHistory,
    ) -> BoxFuture<'fut, Result<TransientPtr<Self>, InjectableError>>
    where
        Self: Sized + 'fut,
        'di_container: 'fut;

    /// A.
    fn into_ptr_buffer_box(self: Box<Self>) -> PtrBuffer;

    /// A.
    fn into_ptr_buffer_rc(self: Rc<Self>) -> PtrBuffer;

    /// A.
    fn into_ptr_buffer_arc(self: Arc<Self>) -> PtrBuffer;
}

impl<DIContainerT> Debug for dyn AsyncInjectable<DIContainerT>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
    {
        f.write_str("{}")
    }
}

impl<T, DIContainerT> AsyncInjectable<DIContainerT> for T
where
    T: Default + 'static + Send + Sync,
{
    fn resolve<'di_container, 'fut>(
        _: &'di_container DIContainerT,
        _: DependencyHistory,
    ) -> BoxFuture<'fut, Result<TransientPtr<Self>, InjectableError>>
    where
        Self: Sized + 'fut,
        'di_container: 'fut,
    {
        Box::pin(ready(Ok(TransientPtr::new(Self::default()))))
    }

    fn into_ptr_buffer_box(self: Box<Self>) -> PtrBuffer
    {
        PtrBuffer::new_from(self)
    }

    fn into_ptr_buffer_rc(self: Rc<Self>) -> PtrBuffer
    {
        PtrBuffer::new_from(self)
    }

    fn into_ptr_buffer_arc(self: Arc<Self>) -> PtrBuffer
    {
        PtrBuffer::new_from(self)
    }
}