aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces/injectable.rs
blob: 1d3a1a7b0c5bebbd2def648ff8be792105a70918 (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
//! Interface for structs that can be injected into or be injected to.
use std::fmt::Debug;

use crate::errors::injectable::InjectableError;
use crate::private::cast::CastFrom;
use crate::ptr::TransientPtr;
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 Injectable<DIContainerT>: CastFrom
{
    /// Resolves the dependencies of the injectable.
    ///
    /// # Errors
    /// Will return `Err` if resolving the dependencies fails.
    fn resolve(
        di_container: &DIContainerT,
        dependency_history: DependencyHistory,
    ) -> Result<TransientPtr<Self>, InjectableError>
    where
        Self: Sized;
}

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

impl<T, DIContainerT> Injectable<DIContainerT> for T
where
    T: Default + 'static,
{
    fn resolve(
        _: &DIContainerT,
        _: DependencyHistory,
    ) -> Result<TransientPtr<Self>, InjectableError>
    {
        Ok(TransientPtr::new(Self::default()))
    }
}