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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use errors::Error;
//use factory::FactoryBase;
use container::{ReadGuard, WriteGuard, Container};
use reflect;

use downcast::Downcast;

use std::marker::PhantomData;
use std::any::Any;

// ++++++++++++++++++++ Method ++++++++++++++++++++

pub trait Method<'a, Key, SvcBase: ?Sized>: Any
    where Key: reflect::Key, SvcBase: Any
{
    type Ret: 'a;
    fn resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>>;
    fn try_resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>>;
}

macro_rules! impl_nil {
    ($nil_ty:ty) => {
        impl<'a, Key, SvcBase: ?Sized> Method<'a, Key, SvcBase> for $nil_ty
            where Key: reflect::Key, SvcBase: Any
        {
            type Ret = ();

            fn resolve_unprotected(_: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok(())
            }
            fn try_resolve_unprotected(_: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok(())
            }
        }
    }
}

impl_nil!(());

// ++++++++++++++++++++ Read ++++++++++++++++++++

pub struct Read<Svc>(PhantomData<fn(Svc)>);

impl_nil!(Read<()>);

impl<'a, Key, SvcBase: ?Sized, Svc> Method<'a, Key, SvcBase> for Read<Svc>
where 
    Key: reflect::Key,
    Svc: reflect::Service<Key = Key>,
    SvcBase: Downcast<Svc>,
{
    type Ret = ReadGuard<'a, Svc, SvcBase>;
    fn resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
        ioc.read::<Svc>()
    }
    fn try_resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
        ioc.try_read::<Svc>()
    }
}

macro_rules! multi_read {
    ($({$($params:ident)+})+) => {$(
        impl<'a, Key, SvcBase: ?Sized, $($params),+> Method<'a, Key, SvcBase> for Read<($($params,)+)>
        where
            Key: reflect::Key,
            $($params: reflect::Service<Key = Key>),+,
            $(SvcBase: Downcast<$params>),+
        {
            type Ret = ($(<Read<$params> as Method<'a, Key, SvcBase>>::Ret,)+);
            fn resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok((
                    $(try!{ioc.read::<$params>()},)+
                ))
            }
            fn try_resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok((
                    $(try!{ioc.try_read::<$params>()},)+
                ))
            }
        }
    )+}
}

multi_read!{
    {A} 
    {A B} 
    {A B C}
    {A B C D}
    {A B C D E}
    {A B C D E F}
    {A B C D E F G}
    {A B C D E F G H}
    {A B C D E F G H J}
    {A B C D E F G H J K}
    {A B C D E F G H J K L}
    {A B C D E F G H J K L M}
    {A B C D E F G H J K L M N}
    {A B C D E F G H J K L M N O}
    //{A B C D E F G H J K L M N O P}
    //{A B C D E F G H J K L M N O P Q}
}

// ++++++++++++++++++++ Write ++++++++++++++++++++

pub struct Write<Svc>(PhantomData<fn(Svc)>);

impl_nil!(Write<()>);

impl<'a, Key, SvcBase: ?Sized, Svc> Method<'a, Key, SvcBase> for Write<Svc>
where 
    Key: reflect::Key,
    Svc: reflect::Service<Key = Key>,
    SvcBase: Downcast<Svc>,
{
    type Ret = WriteGuard<'a, Svc, SvcBase>;
    fn resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
        ioc.write::<Svc>()
    }
    fn try_resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
        ioc.try_write::<Svc>()
    }
}

macro_rules! multi_write {
    ($({$($params:ident)+})+) => {$(
        impl<'a, Key, SvcBase: ?Sized, $($params),+> Method<'a, Key, SvcBase> for Write<($($params,)+)>
        where
            Key: reflect::Key,
            $($params: reflect::Service<Key = Key>),+,
            $(SvcBase: Downcast<$params>),+
        {
            type Ret = ($(<Write<$params> as Method<'a, Key, SvcBase>>::Ret,)+);
            fn resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok((
                    $(try!{ioc.write::<$params>()},)+
                ))
            }
            fn try_resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok((
                    $(try!{ioc.try_write::<$params>()},)+
                ))
            }
        }
    )+}
}

multi_write!{
    {A} 
    {A B} 
    {A B C}
    {A B C D}
    {A B C D E}
    {A B C D E F}
    {A B C D E F G}
    {A B C D E F G H}
    {A B C D E F G H J}
    {A B C D E F G H J K}
    {A B C D E F G H J K L}
    {A B C D E F G H J K L M}
    {A B C D E F G H J K L M N}
    {A B C D E F G H J K L M N O}
    {A B C D E F G H J K L M N O P}
    {A B C D E F G H J K L M N O P Q}
}

/*
// ++++++++++++++++++++ Create ++++++++++++++++++++

pub struct Create<Obj>(fn(Obj));

impl<'a, Cont, Obj> Method<'a, Cont> for Create<Obj>
where 
    Obj: reflect::FactoryObject<Key = Cont::Key>,
    Obj::Factory: FactoryBase<'a, Cont, Obj>,
    Cont: Container<'a>,
    Cont::ServiceBase: Downcast<Obj::Factory>,
{
    type Ret = Obj;
    fn resolve_unprotected(ioc: &'a Cont) -> Result<Self::Ret, Error<'a, Cont::Key>> {
        ioc.create::<Obj>()
    }
}

macro_rules! multi_create {
    ($({$($params:ident)+})+) => {$(
        impl<'a, Cont, $($params),+> Method<'a, Cont> for Create<($($params,)+)>
        where
            $($params: reflect::FactoryObject<Key = Cont::Key>),+,
            $($params::Factory: FactoryBase<'a, Cont, $params>),+,
            Cont: Container<'a>,
            $(Cont::ServiceBase: Downcast<$params::Factory>),+
        {
            type Ret = ($(<Create<$params> as Method<'a, Cont>>::Ret,)+);
            fn resolve_unprotected(ioc: &'a Cont) -> Result<Self::Ret, Error<'a, Cont::Key>> {
                Ok((
                    $(try!{ioc.create::<$params>()},)+
                ))
            }
        }
    )+}
}
*/
// ++++++++++++++++++++ multi-method ++++++++++++++++++++

macro_rules! e {
    ($e:expr) => { $e }
}

macro_rules! multi_methods {
    ($({$($idx:tt,$params:ident)+})+) => {$(
        
        impl<'a, Key, SvcBase: ?Sized, $($params),+> Method<'a, Key, SvcBase> for ($($params,)+) 
        where 
            Key: reflect::Key,
            $($params: Method<'a, Key, SvcBase> + 'a),+, 
            SvcBase: Any,
        {
            type Ret = ($($params::Ret,)+);
            fn resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok((
                    $(try!{e![$params::resolve_unprotected(ioc)]},)+
                ))
            }
            fn try_resolve_unprotected(ioc: &'a Container<Key, SvcBase>) -> Result<Self::Ret, Error<'a, Key>> {
                Ok((
                    $(try!{e![$params::try_resolve_unprotected(ioc)]},)+
                ))
            }
        }

    )+}
}

multi_methods!{
    {0,A} 
    {0,A 1,B} 
    {0,A 1,B 2,C}
    {0,A 1,B 2,C 3,D}
    {0,A 1,B 2,C 3,D 4,E}
    {0,A 1,B 2,C 3,D 4,E 5,F}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J 9,K}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J 9,K 10,L}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J 9,K 10,L 11,M}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J 9,K 10,L 11,M 12,N}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J 9,K 10,L 11,M 12,N 13,O}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J 9,K 10,L 11,M 12,N 13,O 14,P}
    {0,A 1,B 2,C 3,D 4,E 5,F 6,G 7,H 8,J 9,K 10,L 11,M 12,N 13,O 14,P 15,Q}
}