@@ -12,7 +12,7 @@ pub type Linker<Factors> = wasmtime::component::Linker<<Factors as SpinFactors>:
1212pub type ModuleLinker < Factors > = wasmtime:: Linker < <Factors as SpinFactors >:: InstanceState > ;
1313
1414pub trait Factor : Any + Sized {
15- type InstancePreparer : InstancePreparer < Self > ;
15+ type InstancePreparer : FactorInstancePreparer < Self > ;
1616 type InstanceState ;
1717
1818 /// Initializes this Factor for a runtime. This will be called exactly once
@@ -35,9 +35,12 @@ pub trait Factor: Any + Sized {
3535 }
3636}
3737
38+ type GetDataFn < Factors , Fact > =
39+ fn ( & mut <Factors as SpinFactors >:: InstanceState ) -> & mut <Fact as Factor >:: InstanceState ;
40+
3841pub struct FactorInitContext < ' a , Factors : SpinFactors , Fact : Factor , Linker > {
3942 linker : & ' a mut Linker ,
40- get_data : fn ( & mut Factors :: InstanceState ) -> & mut Fact :: InstanceState ,
43+ get_data : GetDataFn < Factors , Fact > ,
4144}
4245
4346pub type InitContext < ' a , Factors , Fact > = FactorInitContext < ' a , Factors , Fact , Linker < Factors > > ;
@@ -47,17 +50,18 @@ pub type ModuleInitContext<'a, Factors, Fact> =
4750
4851impl < ' a , Factors : SpinFactors , Fact : Factor , Linker > FactorInitContext < ' a , Factors , Fact , Linker > {
4952 #[ 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 {
5454 Self { linker, get_data }
5555 }
5656
5757 pub fn linker ( & mut self ) -> & mut Linker {
5858 self . linker
5959 }
6060
61+ pub fn get_data_fn ( & self ) -> GetDataFn < Factors , Fact > {
62+ self . get_data
63+ }
64+
6165 pub fn link_bindings (
6266 & mut self ,
6367 add_to_linker : impl Fn (
@@ -70,7 +74,23 @@ where {
7074 }
7175}
7276
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+
7388impl < ' 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+
7494 pub fn instance_preparer_mut < T : Factor > ( & mut self ) -> Result < & mut T :: InstancePreparer > {
7595 let err_msg = match Factors :: instance_preparer_mut :: < T > ( self . instance_preparers ) {
7696 Some ( Some ( preparer) ) => return Ok ( preparer) ,
@@ -84,6 +104,22 @@ impl<'a, Factors: SpinFactors> PrepareContext<'a, Factors> {
84104 }
85105}
86106
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+
87123/// Implemented by `#[derive(SpinFactors)]`
88124pub trait SpinFactors : Sized {
89125 type InstancePreparers ;
@@ -179,37 +215,3 @@ impl<T, U, V> Clone for Getter2<T, U, V> {
179215 }
180216}
181217impl < 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