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
use std::marker::PhantomData;
use crate::{
locking::UnmountedLock,
stores::{StoreCons, StoresList},
};
use super::{component::ComponentStore, types::HookReturn};
pub struct HookBuilder {
pub(crate) untyped_stores: *const (),
pub(crate) lock: UnmountedLock,
pub(crate) current_component: &'static dyn ComponentStore,
}
impl HookBuilder {
pub fn init<T: StoresList>(self) -> HookConstruction<T, T> {
let current: &T = unsafe { &*(self.untyped_stores as *const T) };
HookConstruction {
current,
entire: PhantomData,
lock: self.lock,
current_component: self.current_component,
}
}
}
pub struct HookConstruction<CurrentStores: StoresList, EntireStores: StoresList> {
pub(crate) current: &'static CurrentStores,
pub(crate) entire: PhantomData<EntireStores>,
pub(crate) lock: UnmountedLock,
pub(crate) current_component: &'static dyn ComponentStore,
}
impl<ThisStore, RestStores, EntireStores>
HookConstruction<StoreCons<ThisStore, RestStores>, EntireStores>
where
ThisStore: Default + 'static,
RestStores: StoresList,
EntireStores: StoresList,
{
pub(crate) fn use_one_store(
self,
) -> (
HookConstruction<RestStores, EntireStores>,
&'static ThisStore,
) {
let Self { current, .. } = self;
let StoreCons(store, rest) = current;
let new_rs = HookConstruction {
current: rest,
entire: PhantomData,
lock: self.lock,
current_component: self.current_component,
};
(new_rs, store)
}
}
fn run_hook<Arg, Out, Ret>(
store: &'static Ret::StoresList,
lock: UnmountedLock,
current_component: &'static dyn ComponentStore,
hook_func: fn(HookBuilder, Arg) -> Ret,
hook_arg: Arg,
) -> Out
where
Ret: HookReturn<Out>,
{
let untyped_stores = store as *const <Ret as HookReturn<Out>>::StoresList as *const ();
let cc = HookBuilder {
untyped_stores,
lock,
current_component,
};
let out: Out = hook_func(cc, hook_arg).get_val();
out
}
impl<ThisStore, RestStores, EntireStores>
HookConstruction<StoreCons<ThisStore, RestStores>, EntireStores>
where
ThisStore: StoresList,
RestStores: StoresList,
EntireStores: StoresList,
{
pub fn hook<Arg, Out, Ret>(
self,
hook_func: fn(HookBuilder, Arg) -> Ret,
hook_arg: Arg,
) -> (HookConstruction<RestStores, EntireStores>, Out)
where
Ret: HookReturn<Out, StoresList = ThisStore>,
{
let (rest_stores, store) = self.use_one_store();
let out = run_hook(
store,
rest_stores.lock.clone(),
rest_stores.current_component,
hook_func,
hook_arg,
);
(rest_stores, out)
}
}