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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! If you have a collection of singletons you need to initialize, and some of those singletons
//! are dependent on handles from other singletons, the `InitTree` is the structure you need.
//! After implementing the `Init` trait for all of your singleton types all you need to do is add
//! them to the InitTree, and then call `.init()` to receive all of your initialized types.

use std::{
    any::{Any, TypeId},
    collections::HashMap,
};

use itertools::join;

/// If your dependency tree goes beyond this many layers deep we'll refuse to initialize it.
pub const MAX_TREE_DEPTH: u32 = 500;

/// A tree of types to initialize.
#[derive(Default, Clone)]
pub struct InitTree {
    uninitialized: Vec<TypeInitDef>,
    cache: Option<Vec<usize>>,
}

/// Largely an implementation detail. However you may need to create one of these if you're manually
/// implementing `Init`.
#[derive(Clone, Copy)]
pub struct TypeInitDef {
    pub id: fn() -> TypeId,
    pub deps: fn() -> &'static [TypeInitDef],
    pub init: fn(&mut HashMap<TypeId, Box<dyn Any>>) -> Box<dyn Any>,
    pub name: &'static str,
}

impl TypeInitDef {
    /// Creates a new instance of this type.
    ///
    /// # Arguments
    ///
    /// id: The TypeId for the type this will be able to construct.
    ///
    /// deps: A function returning the list of direct dependencies for the constructed type.
    ///
    /// deep_deps: A function which will populate an empty vector with all dependencies for the
    /// constructed type. Called recursively on dependencies.
    ///
    /// init: A function that retrieves the needed dependencies from a HashMap, initializes the
    /// type, and then returns the instance in a type erased `Box`.
    ///
    /// name: The name of the type this constructs.
    pub fn new(
        id: fn() -> TypeId,
        deps: fn() -> &'static [TypeInitDef],
        init: fn(&mut HashMap<TypeId, Box<dyn Any>>) -> Box<dyn Any>,
        name: &'static str,
    ) -> Self {
        Self {
            id,
            deps,
            init,
            name,
        }
    }
}

/// Here for use in macros. Returns a comma separated string of the type names in this iterator.
pub fn get_type_names<'a>(defs: impl Iterator<Item=&'a TypeInitDef>) -> String {
    join(defs.map(|d| d.name), ", ")
}

impl InitTree {
    pub fn new() -> Self {
        Default::default()
    }

    /// InitTree supports the use of a cache between initializations.
    /// It's common for the initialization sequence to be identical between runs.
    /// If you find that the process of discovering dependencies is slowing down
    /// your initialization you can cache the results of this discovery to make future
    /// initializations faster.
    ///
    /// One might for example serialize the cache to a file after initialization and load
    /// it from that file when initializing in the future.
    pub fn enable_caching(&mut self, enabled: bool) {
        if enabled {
            if self.cache == None {
                self.cache = Some(Vec::new());
            }
        } else {
            self.cache = None;
        }
    }

    /// Loads a new cache in, returning the old one, if any.
    /// This will automatically enable caching.
    pub fn load_cache(&mut self, cache: Vec<usize>) -> Option<Vec<usize>> {
        let prior = self.cache.take();
        self.cache = Some(cache);
        prior
    }
    
    pub fn add<T: 'static + Init>(&mut self) {
        self.uninitialized.push(T::self_def());
        let mut deps = Vec::new();
        T::deep_deps_list(&mut deps, 0);
        for t in deps {
            self.uninitialized.push(t)
        }
    }

    /// Initializes the tree, returning a fully initialized tree, and if caching is enabled, a
    /// cache from this initialization.
    pub fn init(mut self) -> InitializedTree {
        let mut initialized = HashMap::new();
        self.uninitialized.sort_by_key(|t| t.id);
        self.uninitialized.dedup_by_key(|t| t.id);
        let mut cache_was_correct = self.cache.is_some();
        if let Some(cache) = &mut self.cache {
            // This cache may be invalid. So we're going to replace it
            // with whatever we learn from this run.
            let mut new_cache = Vec::new();
            for i in cache.iter() {
                if (self.uninitialized[*i].deps)()
                    .iter()
                    .all(|t| initialized.contains_key(&(t.id)()))
                {
                    let new_init = self.uninitialized.swap_remove(*i);
                    let new_value = (new_init.init)(&mut initialized);
                    initialized.insert((new_init.id)(), new_value);
                    new_cache.push(*i);
                } else {
                    cache_was_correct = false;
                }
            }
            *cache = new_cache;
        }
        while self.init_cycle(&mut initialized) > 0 {
            cache_was_correct = false;
        }
        if !self.uninitialized.is_empty() {
            panic!(
                "Unable to resolve initialization tree. Locked on [{}]",
                get_type_names(self.uninitialized.iter()) 
            );
        }
        InitializedTree {
            tree: initialized,
            cache: self.cache,
            cache_was_correct,
        }
    }

    fn init_cycle(&mut self, initialized: &mut HashMap<TypeId, Box<dyn Any>>) -> u32 {
        let mut initialized_count = 0;
        let mut i = 0;
        while i < self.uninitialized.len() {
            if (self.uninitialized[i].deps)()
                .iter()
                .all(|t| initialized.contains_key(&(t.id)()))
            {
                let new_init = self.uninitialized.swap_remove(i);
                let new_value = (new_init.init)(initialized);
                initialized.insert((new_init.id)(), new_value);
                initialized_count += 1;
                if let Some(cache) = &mut self.cache {
                    cache.push(i);
                }
            } else {
                i += 1;
            }
        }
        initialized_count
    }
}

/// A collection of all the structures after they've been initialized. Call `.take::<MyType>()` on
/// this to obtain the newly initialized structure.
#[derive(Default)]
pub struct InitializedTree {
    tree: HashMap<TypeId, Box<dyn Any>>,
    cache: Option<Vec<usize>>,
    cache_was_correct: bool,
}

impl InitializedTree {
    /// Removes the initialized structure from this tree and returns it.
    pub fn take<T: 'static>(&mut self) -> Option<T> {
        self.tree
            .remove(&TypeId::of::<T>())
            .map(|v| *v.downcast::<T>().unwrap())
    }

    /// Removes the initialized structure from this tree and returns it. Prefer `take()` if possible,
    /// but this function is provided in case the type can't be determined at compile time.
    pub fn take_by_type_id(&mut self, t: TypeId) -> Option<Box<dyn Any>> {
        self.tree.remove(&t)
    }

    /// Return the cache from this initialization.
    pub fn take_cache(&mut self) -> Option<Vec<usize>> {
        self.cache.take()
    }

    /// Returns true if the cache loaded in was completely correct.
    pub fn cache_was_correct(&self) -> bool {
       self.cache_was_correct 
    }
}

pub trait Init: Sized {
    fn init(initialized: &mut HashMap<TypeId, Box<dyn Any>>) -> Self;
    fn self_def() -> TypeInitDef;
    fn deps_list() -> &'static [TypeInitDef];
    fn deep_deps_list(t: &mut Vec<TypeInitDef>, call_depth: u32);
}

impl<T: 'static + Default> Init for T {
    fn init(_: &mut HashMap<TypeId, Box<dyn Any>>) -> Self {
        Default::default()
    }

    fn self_def() -> TypeInitDef {
        TypeInitDef {
            id: TypeId::of::<Self>,
            deps: Self::deps_list,
            init: |h| Box::new(Self::init(h)),
            name: "<Unknown Type>", // Default type should never have name printed.
        }
    }

    fn deps_list() -> &'static [TypeInitDef] {
        &[]
    }

    fn deep_deps_list(_t: &mut Vec<TypeInitDef>, _call_depth: u32) {}
}

#[macro_export]
macro_rules! impl_init {
    ($t:ty; ($($arg:ident: &mut $arg_type:ty),*) $init:block) => {
        impl $crate::Init for $t
        {
            fn init(initialized: &mut std::collections::HashMap<std::any::TypeId, Box<dyn std::any::Any>>) -> Self {
                $(
                    let mut $arg = initialized.remove(&std::any::TypeId::of::<$arg_type>()).unwrap();
                )*
                let ret;
                {
                    $(
                        let $arg = $arg.downcast_mut::<$arg_type>().unwrap();
                    )*
                    ret = $init;
                }

                $(
                    initialized.insert(std::any::TypeId::of::<$arg_type>(), $arg);
                )*
                ret
            }

            fn self_def() -> $crate::TypeInitDef {
                $crate::TypeInitDef {
                    id: std::any::TypeId::of::<Self>,
                    deps: Self::deps_list,
                    init: |h| Box::new(Self::init(h)),
                    name: stringify!($t),
                }
            }

            #[allow(non_upper_case_globals)]
            fn deps_list() -> &'static [$crate::TypeInitDef] {
                $(const $arg: $crate::TypeInitDef = $crate::TypeInitDef {
                    id: std::any::TypeId::of::<$arg_type>,
                    deps: <$arg_type as $crate::Init>::deps_list,
                    init: |h| Box::new(<$arg_type as $crate::Init>::init(h)),
                    name: stringify!($arg_type),
                };)*
                &[$($arg,)*]
            }

            fn deep_deps_list(t: &mut Vec<$crate::TypeInitDef>, call_depth: u32) {
                if call_depth >= $crate::MAX_TREE_DEPTH {
                    panic!(
                        "Dependency tree too deep, this is usually due to a circular dependency. Current tree: [{}]",
                        $crate::get_type_names(t.iter())
                    );
                }
                t.extend(Self::deps_list().iter());
                $(
                    <$arg_type as $crate::Init>::deep_deps_list(t, call_depth + 1);
                )*
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(PartialEq, Eq, Debug)]
    struct InitA {
        b: i32,
        c: i32,
        d: i32,
    }

    impl_init!(InitA; (b: &mut InitB, c: &mut InitC, d: &mut InitD) {
        InitA {
            b: b.get_handle(),
            c: c.get_handle(),
            d: d.0,
        }
    });

    #[derive(Default, PartialEq, Eq, Debug)]
    struct InitB;

    impl InitB {
        fn get_handle(&self) -> i32 {
            5
        }
    }

    #[derive(Default, PartialEq, Eq, Debug)]
    struct InitC;

    impl InitC {
        fn get_handle(&self) -> i32 {
            7
        }
    }

    #[derive(PartialEq, Eq, Debug)]
    struct InitD(i32);

    impl_init!(InitD; (e: &mut InitE) {
        InitD(e.get_handle())
    });

    #[derive(Default, PartialEq, Eq, Debug)]
    struct InitE;

    impl InitE {
        fn get_handle(&self) -> i32 {
            10
        }
    }

    #[test]
    fn test_basic_init() {
        let mut tree = InitTree::new();
        tree.add::<InitA>();
        let mut vals= tree.init();
        assert_eq!(vals.take::<InitA>(), Some(InitA { b: 5, c: 7, d: 10 }));
        assert_eq!(vals.take::<InitB>(), Some(InitB));
        assert_eq!(vals.take::<InitC>(), Some(InitC));
        assert_eq!(vals.take::<InitD>(), Some(InitD(10)));
        assert_eq!(vals.take::<InitE>(), Some(InitE));
    }

    struct CantInitA;

    impl_init!(CantInitA; (_b: &mut CantInitB) {
        CantInitA
    });

    struct CantInitB;

    impl_init!(CantInitB; (_a: &mut CantInitA) {
        CantInitB
    });

    #[test]
    #[should_panic]
    fn test_panic() {
        let mut tree = InitTree::new();
        tree.add::<CantInitA>();
        tree.init();
    }

    #[derive(Default, PartialEq, Eq, Debug)]
    struct BaseCoreInit;

    #[derive(PartialEq, Eq, Debug)]
    struct CoreInit;

    impl_init!(CoreInit; (_base: &mut BaseCoreInit) {
        CoreInit
    });

    #[derive(PartialEq, Eq, Debug)]
    struct LevelOneInit;

    impl_init!(LevelOneInit; (_core: &mut CoreInit) {
        LevelOneInit
    });

    #[derive(PartialEq, Eq, Debug)]
    struct LevelTwoInit;

    impl_init!(LevelTwoInit; (_core: &mut CoreInit, _one: &mut LevelOneInit) {
        LevelTwoInit
    });

    #[derive(PartialEq, Eq, Debug)]
    struct LevelThreeInit;

    impl_init!(LevelThreeInit; (_core: &mut CoreInit, _two: &mut LevelTwoInit) {
        LevelThreeInit
    });

    #[derive(PartialEq, Eq, Debug)]
    struct LevelFourInit;

    impl_init!(LevelFourInit; (_core: &mut CoreInit, _three: &mut LevelThreeInit) {
        LevelFourInit
    });

    #[test]
    fn test_layers_with_shared_dep() {
        let mut tree = InitTree::new();
        tree.add::<LevelFourInit>();
        tree.add::<LevelThreeInit>();
        tree.add::<LevelTwoInit>();
        tree.add::<LevelOneInit>();
        let mut initialized = tree.init();
        assert_eq!(initialized.take::<CoreInit>(), Some(CoreInit));
        assert_eq!(initialized.take::<LevelOneInit>(), Some(LevelOneInit));
        assert_eq!(initialized.take::<LevelTwoInit>(), Some(LevelTwoInit));
        assert_eq!(initialized.take::<LevelThreeInit>(), Some(LevelThreeInit));
        assert_eq!(initialized.take::<LevelFourInit>(), Some(LevelFourInit));
    }

    #[test]
    fn test_caching() {
        fn test_init() -> InitTree {
            let mut tree = InitTree::new();
            tree.add::<LevelFourInit>();
            tree.add::<LevelThreeInit>();
            tree.add::<LevelTwoInit>();
            tree.add::<LevelOneInit>();
            tree
        }
        let mut init = test_init();
        init.enable_caching(true);
        let mut initialized = init.init();
        let cache = initialized.take_cache();
        assert!(!initialized.cache_was_correct());
        let mut init = test_init();
        init.load_cache(cache.unwrap());
        let mut initialized = init.init();
        assert!(initialized.take_cache().is_some());
        assert!(initialized.cache_was_correct());
    }
}