@@ -12,15 +12,22 @@ 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
+ /// The [`FactorInstancePreparer`] for this factor.
15
16
type InstancePreparer : FactorInstancePreparer < Self > ;
17
+
18
+ /// The per-instance state for this factor, constructed by a
19
+ /// [`FactorInstancePreparer`] and available to any host-provided imports
20
+ /// defined by this factor.
16
21
type InstanceState ;
17
22
18
- /// Initializes this Factor for a runtime. This will be called exactly once
23
+ /// Initializes this Factor for a runtime. This should be called at most once.
19
24
fn init < Factors : SpinFactors > ( & mut self , mut ctx : InitContext < Factors , Self > ) -> Result < ( ) > {
20
25
_ = & mut ctx;
21
26
Ok ( ( ) )
22
27
}
23
28
29
+ /// Performs factor-specific validation of the given [`App`]`. This may be
30
+ /// called before, after, or instead of `init`.
24
31
fn validate_app ( & self , app : & App ) -> Result < ( ) > {
25
32
_ = app;
26
33
Ok ( ( ) )
@@ -30,6 +37,8 @@ pub trait Factor: Any + Sized {
30
37
type GetDataFn < Factors , Fact > =
31
38
fn ( & mut <Factors as SpinFactors >:: InstanceState ) -> & mut <Fact as Factor >:: InstanceState ;
32
39
40
+ /// An InitContext is passed to [`Factor::init`], giving access to the global
41
+ /// common [`wasmtime::component::Linker`].
33
42
pub struct InitContext < ' a , Factors : SpinFactors , Fact : Factor > {
34
43
linker : Option < & ' a mut Linker < Factors > > ,
35
44
module_linker : Option < & ' a mut ModuleLinker < Factors > > ,
@@ -94,11 +103,16 @@ where {
94
103
}
95
104
96
105
pub trait FactorInstancePreparer < T : Factor > : Sized {
106
+ /// Returns a new instance of this preparer for the given [`Factor`].
97
107
fn new < Factors : SpinFactors > ( factor : & T , _ctx : PrepareContext < Factors > ) -> Result < Self > ;
98
108
109
+ /// Returns a new instance of the associated [`Factor::InstanceState`].
99
110
fn prepare ( self ) -> Result < T :: InstanceState > ;
100
111
}
101
112
113
+ /// A PrepareContext is passed to [`FactorInstancePreparer::new`], giving access
114
+ /// to any already-initialized [`FactorInstancePreparer`]s, allowing for
115
+ /// inter-[`Factor`] dependencies.
102
116
pub struct PrepareContext < ' a , Factors : SpinFactors > {
103
117
instance_preparers : & ' a mut Factors :: InstancePreparers ,
104
118
// TODO: component: &'a AppComponent,
@@ -110,6 +124,11 @@ impl<'a, Factors: SpinFactors> PrepareContext<'a, Factors> {
110
124
Self { instance_preparers }
111
125
}
112
126
127
+ /// Returns a already-initialized preparer for the given [`Factor`].
128
+ ///
129
+ /// Fails if the current [`SpinFactors`] does not include the given
130
+ /// [`Factor`] or if the given [`Factor`]'s preparer has not been
131
+ /// initialized yet (because it is sequenced after this factor).
113
132
pub fn instance_preparer_mut < T : Factor > ( & mut self ) -> Result < & mut T :: InstancePreparer > {
114
133
let err_msg = match Factors :: instance_preparer_mut :: < T > ( self . instance_preparers ) {
115
134
Some ( Some ( preparer) ) => return Ok ( preparer) ,
@@ -123,6 +142,8 @@ impl<'a, Factors: SpinFactors> PrepareContext<'a, Factors> {
123
142
}
124
143
}
125
144
145
+ /// DefaultInstancePreparer can be used as a [`FactorInstancePreparer`] to
146
+ /// produce a [`Default`] [`Factor::InstanceState`].
126
147
pub type DefaultInstancePreparer = ( ) ;
127
148
128
149
impl < T : Factor > FactorInstancePreparer < T > for DefaultInstancePreparer
0 commit comments