Skip to content

Commit d28f96c

Browse files
committed
factors: Add comments
Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent f1dc5ed commit d28f96c

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

crates/factors/src/lib.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ pub type Linker<Factors> = wasmtime::component::Linker<<Factors as SpinFactors>:
1212
pub type ModuleLinker<Factors> = wasmtime::Linker<<Factors as SpinFactors>::InstanceState>;
1313

1414
pub trait Factor: Any + Sized {
15+
/// The [`FactorInstancePreparer`] for this factor.
1516
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.
1621
type InstanceState;
1722

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.
1924
fn init<Factors: SpinFactors>(&mut self, mut ctx: InitContext<Factors, Self>) -> Result<()> {
2025
_ = &mut ctx;
2126
Ok(())
2227
}
2328

29+
/// Performs factor-specific validation of the given [`App`]`. This may be
30+
/// called before, after, or instead of `init`.
2431
fn validate_app(&self, app: &App) -> Result<()> {
2532
_ = app;
2633
Ok(())
@@ -30,6 +37,8 @@ pub trait Factor: Any + Sized {
3037
type GetDataFn<Factors, Fact> =
3138
fn(&mut <Factors as SpinFactors>::InstanceState) -> &mut <Fact as Factor>::InstanceState;
3239

40+
/// An InitContext is passed to [`Factor::init`], giving access to the global
41+
/// common [`wasmtime::component::Linker`].
3342
pub struct InitContext<'a, Factors: SpinFactors, Fact: Factor> {
3443
linker: Option<&'a mut Linker<Factors>>,
3544
module_linker: Option<&'a mut ModuleLinker<Factors>>,
@@ -94,11 +103,16 @@ where {
94103
}
95104

96105
pub trait FactorInstancePreparer<T: Factor>: Sized {
106+
/// Returns a new instance of this preparer for the given [`Factor`].
97107
fn new<Factors: SpinFactors>(factor: &T, _ctx: PrepareContext<Factors>) -> Result<Self>;
98108

109+
/// Returns a new instance of the associated [`Factor::InstanceState`].
99110
fn prepare(self) -> Result<T::InstanceState>;
100111
}
101112

113+
/// A PrepareContext is passed to [`FactorInstancePreparer::new`], giving access
114+
/// to any already-initialized [`FactorInstancePreparer`]s, allowing for
115+
/// inter-[`Factor`] dependencies.
102116
pub struct PrepareContext<'a, Factors: SpinFactors> {
103117
instance_preparers: &'a mut Factors::InstancePreparers,
104118
// TODO: component: &'a AppComponent,
@@ -110,6 +124,11 @@ impl<'a, Factors: SpinFactors> PrepareContext<'a, Factors> {
110124
Self { instance_preparers }
111125
}
112126

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).
113132
pub fn instance_preparer_mut<T: Factor>(&mut self) -> Result<&mut T::InstancePreparer> {
114133
let err_msg = match Factors::instance_preparer_mut::<T>(self.instance_preparers) {
115134
Some(Some(preparer)) => return Ok(preparer),
@@ -123,6 +142,8 @@ impl<'a, Factors: SpinFactors> PrepareContext<'a, Factors> {
123142
}
124143
}
125144

145+
/// DefaultInstancePreparer can be used as a [`FactorInstancePreparer`] to
146+
/// produce a [`Default`] [`Factor::InstanceState`].
126147
pub type DefaultInstancePreparer = ();
127148

128149
impl<T: Factor> FactorInstancePreparer<T> for DefaultInstancePreparer

crates/factors/tests/smoke.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ struct Factors {
88
}
99

1010
fn main() -> anyhow::Result<()> {
11-
let engine = wasmtime::Engine::default();
12-
let mut linker = wasmtime::component::Linker::new(&engine);
13-
let mut module_linker = wasmtime::Linker::new(&engine);
14-
1511
let mut factors = Factors {
1612
wasi: WasiFactor,
1713
wasip1: WasiPreview1Factor,
1814
// outbound_networking_factor: OutboundNetworkingFactor,
1915
// outbound_http_factor: OutboundHttpFactor,
2016
};
17+
18+
let engine = wasmtime::Engine::default();
19+
let mut linker = wasmtime::component::Linker::new(&engine);
20+
let mut module_linker = wasmtime::Linker::new(&engine);
21+
2122
factors
2223
.init(Some(&mut linker), Some(&mut module_linker))
2324
.unwrap();

0 commit comments

Comments
 (0)