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
use crate::{Mut, Ref, RefMut, ServiceFactory, ServiceProvider, Type};
use std::any::Any;

/// Represents an activator for a service instance.
pub struct Activator {
    service_type: Type,
    service_type_mut: Type,
    implementation_type: Type,
    factory: Ref<ServiceFactory>,
    factory_mut: Ref<ServiceFactory>,
    mutable: bool,
}

impl Activator {
    /// Gets the [service type](crate::Type) associated with the service descriptor.
    pub fn service_type(&self) -> &Type {
        if self.mutable {
            &self.service_type_mut
        } else {
            &self.service_type
        }
    }

    /// Gets the [implementation type](crate::Type) associated with the service descriptor.
    pub fn implementation_type(&self) -> &Type {
        &self.implementation_type
    }

    /// Sets a value indicating whether the activated instance should be mutable.
    pub fn as_mut(&mut self) {
        self.mutable = true;
    }

    /// Gets the factory method the activator represents.
    pub fn factory(&self) -> Ref<ServiceFactory> {
        if self.mutable {
            self.factory_mut.clone()
        } else {
            self.factory.clone()
        }
    }

    /// Creates a new activator using the specified factory methods to instantiate the service.
    ///
    /// # Arguments
    ///
    /// * `factory` - The factory method used to create a service instance
    /// * `factory_mut` - The factory method used to create a mutable service instance
    pub fn new<TSvc: Any + ?Sized, TImpl>(
        factory: fn(&ServiceProvider) -> Ref<TSvc>,
        factory_mut: fn(&ServiceProvider) -> RefMut<TSvc>,
    ) -> Self {
        Self {
            service_type: Type::of::<TSvc>(),
            service_type_mut: Type::of::<Mut<TSvc>>(),
            implementation_type: Type::of::<TImpl>(),
            factory: Ref::new(move |sp| Ref::new(factory(sp))),
            factory_mut: Ref::new(move |sp| Ref::new(factory_mut(sp))),
            mutable: false,
        }
    }
}