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
use crate::reference::*;

pub trait Table<D, R>
where
    D: std::hash::Hash + std::cmp::Eq + std::fmt::Debug,
    R: Reference<D>,
{
    fn get(&self, hash: u64, data: &D) -> Option<R>;

    fn get_or_insert<CF>(&mut self, hash: u64, data: D, creation_meta: CF) -> R
    where
        CF: FnOnce(&mut D);
}

pub trait BuildTable<D, R>
where
    D: std::hash::Hash + std::cmp::Eq + std::fmt::Debug,
    R: Reference<D>,
{
    type Table: Table<D, R>;

    fn build_table(&self) -> Self::Table;
}

pub struct BuildTableDefault<T>(std::marker::PhantomData<T>);

impl<D, R, T> BuildTable<D, R> for BuildTableDefault<T>
where
    D: std::hash::Hash + std::cmp::Eq + std::fmt::Debug,
    R: Reference<D>,
    T: Default + Table<D, R>,
{
    type Table = T;

    fn build_table(&self) -> T {
        T::default()
    }
}

impl<T> Default for BuildTableDefault<T>
where
    T: Default,
{
    fn default() -> Self {
        Self {
            0: std::marker::PhantomData,
        }
    }
}

impl<T> Clone for BuildTableDefault<T> {
    fn clone(&self) -> Self {
        Self {
            0: std::marker::PhantomData,
        }
    }
}

pub trait TableShared<D, R, T>
where
    D: std::hash::Hash + std::cmp::Eq + std::fmt::Debug,
    R: Reference<D>,
    T: Table<D, R>,
{
    fn get(&self, data: &D) -> Option<R>;

    fn get_or_insert<CF>(&self, data: D, creation_meta: CF) -> R
    where
        CF: FnOnce(&mut D);
}

pub trait BuildTableShared<D, R, T>
where
    D: std::hash::Hash + std::cmp::Eq + std::fmt::Debug,
    R: Reference<D>,
    T: Table<D, R>,
{
    type TableSharedType: TableShared<D, R, T>;

    fn build_tableshared(&self) -> Self::TableSharedType;
}

pub struct BuildTableSharedDefault<TS>(std::marker::PhantomData<TS>);

impl<D, R, T, TS> BuildTableShared<D, R, T> for BuildTableSharedDefault<TS>
where
    D: std::hash::Hash + std::cmp::Eq + std::fmt::Debug,
    R: Reference<D>,
    T: Table<D, R>,
    TS: TableShared<D, R, T> + Default,
{
    type TableSharedType = TS;

    fn build_tableshared(&self) -> TS {
        TS::default()
    }
}