11use crate :: { Mut , Ref , RefMut , ServiceFactory , ServiceProvider , Type } ;
22use std:: any:: Any ;
33
4+ macro_rules! new {
5+ ( $( $traits: tt) +) => {
6+ /// Creates a new activator using the specified factory methods to instantiate the service.
7+ ///
8+ /// # Arguments
9+ ///
10+ /// * `factory` - The factory method used to create a service instance
11+ /// * `factory_mut` - The factory method used to create a mutable service instance
12+ pub fn new<TSvc : ?Sized + $( $traits) +, TImpl >(
13+ factory: fn ( & ServiceProvider ) -> Ref <TSvc >,
14+ factory_mut: fn ( & ServiceProvider ) -> RefMut <TSvc >,
15+ ) -> Self {
16+ Self {
17+ service_type: Type :: of:: <TSvc >( ) ,
18+ service_type_mut: Type :: of:: <Mut <TSvc >>( ) ,
19+ implementation_type: Type :: of:: <TImpl >( ) ,
20+ factory: Ref :: new( move |sp| Ref :: new( factory( sp) ) ) ,
21+ factory_mut: Ref :: new( move |sp| Ref :: new( factory_mut( sp) ) ) ,
22+ mutable: false ,
23+ }
24+ }
25+ } ;
26+ }
27+
428/// Represents an activator for a service instance.
529pub struct Activator {
630 service_type : Type ,
@@ -12,7 +36,7 @@ pub struct Activator {
1236}
1337
1438impl Activator {
15- /// Gets the [service type](crate:: Type) associated with the service descriptor.
39+ /// Gets the [service type](Type) associated with the service descriptor.
1640 pub fn service_type ( & self ) -> & Type {
1741 if self . mutable {
1842 & self . service_type_mut
@@ -21,17 +45,19 @@ impl Activator {
2145 }
2246 }
2347
24- /// Gets the [implementation type](crate::Type) associated with the service descriptor.
48+ /// Gets the [implementation type](Type) associated with the service descriptor.
49+ #[ inline]
2550 pub fn implementation_type ( & self ) -> & Type {
2651 & self . implementation_type
2752 }
2853
2954 /// Sets a value indicating whether the activated instance should be mutable.
55+ #[ inline]
3056 pub fn as_mut ( & mut self ) {
3157 self . mutable = true ;
3258 }
3359
34- /// Gets the factory method the activator represents.
60+ /// Gets the [ factory](ServiceFactory) method the activator represents.
3561 pub fn factory ( & self ) -> Ref < ServiceFactory > {
3662 if self . mutable {
3763 self . factory_mut . clone ( )
@@ -40,23 +66,11 @@ impl Activator {
4066 }
4167 }
4268
43- /// Creates a new activator using the specified factory methods to instantiate the service.
44- ///
45- /// # Arguments
46- ///
47- /// * `factory` - The factory method used to create a service instance
48- /// * `factory_mut` - The factory method used to create a mutable service instance
49- pub fn new < TSvc : Any + ?Sized , TImpl > (
50- factory : fn ( & ServiceProvider ) -> Ref < TSvc > ,
51- factory_mut : fn ( & ServiceProvider ) -> RefMut < TSvc > ,
52- ) -> Self {
53- Self {
54- service_type : Type :: of :: < TSvc > ( ) ,
55- service_type_mut : Type :: of :: < Mut < TSvc > > ( ) ,
56- implementation_type : Type :: of :: < TImpl > ( ) ,
57- factory : Ref :: new ( move |sp| Ref :: new ( factory ( sp) ) ) ,
58- factory_mut : Ref :: new ( move |sp| Ref :: new ( factory_mut ( sp) ) ) ,
59- mutable : false ,
69+ cfg_if:: cfg_if! {
70+ if #[ cfg( feature = "async" ) ] {
71+ new!( Any + Send + Sync ) ;
72+ } else {
73+ new!( Any ) ;
6074 }
6175 }
6276}
0 commit comments