@@ -12,7 +12,7 @@ pub type Linker<Factors> = wasmtime::component::Linker<<Factors as SpinFactors>:
12
12
pub type ModuleLinker < Factors > = wasmtime:: Linker < <Factors as SpinFactors >:: InstanceState > ;
13
13
14
14
pub trait Factor : Any + Sized {
15
- type InstancePreparer : InstancePreparer < Self > ;
15
+ type InstancePreparer : FactorInstancePreparer < Self > ;
16
16
type InstanceState ;
17
17
18
18
/// Initializes this Factor for a runtime. This will be called exactly once
@@ -35,9 +35,12 @@ pub trait Factor: Any + Sized {
35
35
}
36
36
}
37
37
38
+ type GetDataFn < Factors , Fact > =
39
+ fn ( & mut <Factors as SpinFactors >:: InstanceState ) -> & mut <Fact as Factor >:: InstanceState ;
40
+
38
41
pub struct FactorInitContext < ' a , Factors : SpinFactors , Fact : Factor , Linker > {
39
42
linker : & ' a mut Linker ,
40
- get_data : fn ( & mut Factors :: InstanceState ) -> & mut Fact :: InstanceState ,
43
+ get_data : GetDataFn < Factors , Fact > ,
41
44
}
42
45
43
46
pub type InitContext < ' a , Factors , Fact > = FactorInitContext < ' a , Factors , Fact , Linker < Factors > > ;
@@ -47,17 +50,18 @@ pub type ModuleInitContext<'a, Factors, Fact> =
47
50
48
51
impl < ' a , Factors : SpinFactors , Fact : Factor , Linker > FactorInitContext < ' a , Factors , Fact , Linker > {
49
52
#[ doc( hidden) ]
50
- pub fn new (
51
- linker : & ' a mut Linker ,
52
- get_data : fn ( & mut Factors :: InstanceState ) -> & mut Fact :: InstanceState ,
53
- ) -> Self {
53
+ pub fn new ( linker : & ' a mut Linker , get_data : GetDataFn < Factors , Fact > ) -> Self {
54
54
Self { linker, get_data }
55
55
}
56
56
57
57
pub fn linker ( & mut self ) -> & mut Linker {
58
58
self . linker
59
59
}
60
60
61
+ pub fn get_data_fn ( & self ) -> GetDataFn < Factors , Fact > {
62
+ self . get_data
63
+ }
64
+
61
65
pub fn link_bindings (
62
66
& mut self ,
63
67
add_to_linker : impl Fn (
@@ -70,7 +74,23 @@ where {
70
74
}
71
75
}
72
76
77
+ pub trait FactorInstancePreparer < T : Factor > : Sized {
78
+ fn new < Factors : SpinFactors > ( factor : & T , _ctx : PrepareContext < Factors > ) -> Result < Self > ;
79
+
80
+ fn prepare ( self ) -> Result < T :: InstanceState > ;
81
+ }
82
+
83
+ pub struct PrepareContext < ' a , Factors : SpinFactors > {
84
+ instance_preparers : & ' a mut Factors :: InstancePreparers ,
85
+ // TODO: component: &'a AppComponent,
86
+ }
87
+
73
88
impl < ' a , Factors : SpinFactors > PrepareContext < ' a , Factors > {
89
+ #[ doc( hidden) ]
90
+ pub fn new ( instance_preparers : & ' a mut Factors :: InstancePreparers ) -> Self {
91
+ Self { instance_preparers }
92
+ }
93
+
74
94
pub fn instance_preparer_mut < T : Factor > ( & mut self ) -> Result < & mut T :: InstancePreparer > {
75
95
let err_msg = match Factors :: instance_preparer_mut :: < T > ( self . instance_preparers ) {
76
96
Some ( Some ( preparer) ) => return Ok ( preparer) ,
@@ -84,6 +104,22 @@ impl<'a, Factors: SpinFactors> PrepareContext<'a, Factors> {
84
104
}
85
105
}
86
106
107
+ pub type DefaultInstancePreparer = ( ) ;
108
+
109
+ impl < T : Factor > FactorInstancePreparer < T > for DefaultInstancePreparer
110
+ where
111
+ T :: InstanceState : Default ,
112
+ {
113
+ fn new < Factors : SpinFactors > ( factor : & T , ctx : PrepareContext < Factors > ) -> Result < Self > {
114
+ ( _, _) = ( factor, ctx) ;
115
+ Ok ( ( ) )
116
+ }
117
+
118
+ fn prepare ( self ) -> Result < T :: InstanceState > {
119
+ Ok ( Default :: default ( ) )
120
+ }
121
+ }
122
+
87
123
/// Implemented by `#[derive(SpinFactors)]`
88
124
pub trait SpinFactors : Sized {
89
125
type InstancePreparers ;
@@ -179,37 +215,3 @@ impl<T, U, V> Clone for Getter2<T, U, V> {
179
215
}
180
216
}
181
217
impl < T , U , V > Copy for Getter2 < T , U , V > { }
182
-
183
- pub trait InstancePreparer < T : Factor > : Sized {
184
- fn new < Factors : SpinFactors > ( _factor : & T , _ctx : PrepareContext < Factors > ) -> Result < Self > ;
185
-
186
- fn prepare ( self ) -> Result < T :: InstanceState > ;
187
- }
188
-
189
- pub struct PrepareContext < ' a , Factors : SpinFactors > {
190
- instance_preparers : & ' a mut Factors :: InstancePreparers ,
191
- // TODO: component: &'a AppComponent,
192
- }
193
-
194
- impl < ' a , Factors : SpinFactors > PrepareContext < ' a , Factors > {
195
- #[ doc( hidden) ]
196
- pub fn new ( instance_preparers : & ' a mut Factors :: InstancePreparers ) -> Self {
197
- Self { instance_preparers }
198
- }
199
- }
200
-
201
- pub type DefaultInstancePreparer = ( ) ;
202
-
203
- impl < T : Factor > InstancePreparer < T > for DefaultInstancePreparer
204
- where
205
- T :: InstanceState : Default ,
206
- {
207
- fn new < Factors : SpinFactors > ( factor : & T , ctx : PrepareContext < Factors > ) -> Result < Self > {
208
- ( _, _) = ( factor, ctx) ;
209
- Ok ( ( ) )
210
- }
211
-
212
- fn prepare ( self ) -> Result < T :: InstanceState > {
213
- Ok ( Default :: default ( ) )
214
- }
215
- }
0 commit comments