Skip to content

Commit 1d026e1

Browse files
committed
factors: Merge FactorInstancePreparer trait into Factor
Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent 138efde commit 1d026e1

File tree

8 files changed

+119
-125
lines changed

8 files changed

+119
-125
lines changed

crates/factor-outbound-networking/src/lib.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use spin_factor_variables::VariablesFactor;
88
use spin_factor_wasi::WasiFactor;
99
use spin_factors::{
1010
anyhow::{self, Context},
11-
ConfigureAppContext, Factor, FactorInstancePreparer, InstancePreparers, PrepareContext,
12-
RuntimeConfig, RuntimeFactors,
11+
ConfigureAppContext, Factor, InstancePreparers, PrepareContext, RuntimeConfig, RuntimeFactors,
1312
};
1413
use spin_outbound_networking::{AllowedHostsConfig, ALLOWED_HOSTS_KEY};
1514

@@ -44,24 +43,11 @@ impl Factor for OutboundNetworkingFactor {
4443
component_allowed_hosts,
4544
})
4645
}
47-
}
48-
49-
#[derive(Default)]
50-
pub struct AppState {
51-
component_allowed_hosts: HashMap<String, Arc<[String]>>,
52-
}
53-
54-
type SharedFutureResult<T> = Shared<BoxFuture<'static, Arc<anyhow::Result<T>>>>;
55-
56-
pub struct InstancePreparer {
57-
allowed_hosts_future: SharedFutureResult<AllowedHostsConfig>,
58-
}
5946

60-
impl FactorInstancePreparer<OutboundNetworkingFactor> for InstancePreparer {
61-
fn new<Factors: RuntimeFactors>(
62-
ctx: PrepareContext<OutboundNetworkingFactor>,
63-
mut preparers: InstancePreparers<Factors>,
64-
) -> anyhow::Result<Self> {
47+
fn create_preparer<T: RuntimeFactors>(
48+
ctx: PrepareContext<Self>,
49+
mut preparers: InstancePreparers<T>,
50+
) -> anyhow::Result<Self::InstancePreparer> {
6551
let hosts = ctx
6652
.app_config()
6753
.component_allowed_hosts
@@ -101,18 +87,40 @@ impl FactorInstancePreparer<OutboundNetworkingFactor> for InstancePreparer {
10187
}
10288
}
10389
});
104-
Ok(Self {
105-
allowed_hosts_future,
106-
})
90+
Ok(InstancePreparer::new(allowed_hosts_future))
10791
}
10892

109-
fn prepare(self) -> anyhow::Result<<OutboundNetworkingFactor as Factor>::InstanceState> {
93+
fn prepare(
94+
&self,
95+
_preparer: InstancePreparer,
96+
) -> anyhow::Result<<OutboundNetworkingFactor as Factor>::InstanceState> {
11097
Ok(())
11198
}
11299
}
113100

101+
#[derive(Default)]
102+
pub struct AppState {
103+
component_allowed_hosts: HashMap<String, Arc<[String]>>,
104+
}
105+
106+
type SharedFutureResult<T> = Shared<BoxFuture<'static, Arc<anyhow::Result<T>>>>;
107+
108+
#[derive(Default)]
109+
pub struct InstancePreparer {
110+
allowed_hosts_future: Option<SharedFutureResult<AllowedHostsConfig>>,
111+
}
112+
114113
impl InstancePreparer {
114+
fn new(allowed_hosts_future: SharedFutureResult<AllowedHostsConfig>) -> Self {
115+
Self {
116+
allowed_hosts_future: Some(allowed_hosts_future),
117+
}
118+
}
119+
115120
pub async fn resolve_allowed_hosts(&self) -> Arc<anyhow::Result<AllowedHostsConfig>> {
116-
self.allowed_hosts_future.clone().await
121+
self.allowed_hosts_future
122+
.clone()
123+
.expect("allowed_hosts_future not set")
124+
.await
117125
}
118126
}

crates/factor-variables/src/lib.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::sync::Arc;
22

33
use spin_expressions::ProviderResolver;
44
use spin_factors::{
5-
anyhow, ConfigureAppContext, Factor, FactorInstancePreparer, InitContext, InstancePreparers,
6-
PrepareContext, RuntimeConfig, RuntimeFactors,
5+
anyhow, ConfigureAppContext, Factor, InitContext, InstancePreparers, PrepareContext,
6+
RuntimeConfig, RuntimeFactors,
77
};
88
use spin_world::{async_trait, v1::config as v1_config, v2::variables};
99

@@ -42,13 +42,32 @@ impl Factor for VariablesFactor {
4242
resolver: Arc::new(resolver),
4343
})
4444
}
45+
46+
fn create_preparer<T: RuntimeFactors>(
47+
ctx: PrepareContext<Self>,
48+
_preparers: InstancePreparers<T>,
49+
) -> anyhow::Result<Self::InstancePreparer> {
50+
let component_id = ctx.app_component().id().to_string();
51+
let resolver = ctx.app_config().resolver.clone();
52+
Ok(InstancePreparer {
53+
state: InstanceState {
54+
component_id,
55+
resolver,
56+
},
57+
})
58+
}
59+
60+
fn prepare(&self, preparer: InstancePreparer) -> anyhow::Result<InstanceState> {
61+
Ok(preparer.state)
62+
}
4563
}
4664

4765
#[derive(Default)]
4866
pub struct AppState {
4967
resolver: Arc<ProviderResolver>,
5068
}
5169

70+
#[derive(Default)]
5271
pub struct InstancePreparer {
5372
state: InstanceState,
5473
}
@@ -59,26 +78,7 @@ impl InstancePreparer {
5978
}
6079
}
6180

62-
impl FactorInstancePreparer<VariablesFactor> for InstancePreparer {
63-
fn new<Factors: RuntimeFactors>(
64-
ctx: PrepareContext<VariablesFactor>,
65-
_preparers: InstancePreparers<Factors>,
66-
) -> anyhow::Result<Self> {
67-
let component_id = ctx.app_component().id().to_string();
68-
let resolver = ctx.app_config().resolver.clone();
69-
Ok(Self {
70-
state: InstanceState {
71-
component_id,
72-
resolver,
73-
},
74-
})
75-
}
76-
77-
fn prepare(self) -> anyhow::Result<InstanceState> {
78-
Ok(self.state)
79-
}
80-
}
81-
81+
#[derive(Default)]
8282
pub struct InstanceState {
8383
component_id: String,
8484
resolver: Arc<ProviderResolver>,

crates/factor-wasi/src/lib.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ pub mod preview1;
33
use std::{future::Future, net::SocketAddr, path::Path};
44

55
use spin_factors::{
6-
anyhow, AppComponent, Factor, FactorInstancePreparer, InitContext, InstancePreparers,
7-
PrepareContext, RuntimeFactors,
6+
anyhow, AppComponent, Factor, InitContext, InstancePreparers, PrepareContext, RuntimeFactors,
87
};
98
use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView};
109

@@ -69,6 +68,36 @@ impl Factor for WasiFactor {
6968
}
7069
Ok(())
7170
}
71+
72+
fn create_preparer<T: RuntimeFactors>(
73+
ctx: PrepareContext<Self>,
74+
_preparers: InstancePreparers<T>,
75+
) -> anyhow::Result<Self::InstancePreparer> {
76+
let mut wasi_ctx = WasiCtxBuilder::new();
77+
78+
// Apply environment variables
79+
for (key, val) in ctx.app_component().environment() {
80+
wasi_ctx.env(key, val);
81+
}
82+
83+
// Mount files
84+
let mount_ctx = MountFilesContext {
85+
wasi_ctx: &mut wasi_ctx,
86+
};
87+
ctx.factor()
88+
.files_mounter
89+
.mount_files(ctx.app_component(), mount_ctx)?;
90+
91+
Ok(InstancePreparer { wasi_ctx })
92+
}
93+
94+
fn prepare(&self, preparer: InstancePreparer) -> anyhow::Result<InstanceState> {
95+
let InstancePreparer { mut wasi_ctx } = preparer;
96+
Ok(InstanceState {
97+
ctx: wasi_ctx.build(),
98+
table: Default::default(),
99+
})
100+
}
72101
}
73102

74103
pub trait FilesMounter {
@@ -122,36 +151,11 @@ pub struct InstancePreparer {
122151
wasi_ctx: WasiCtxBuilder,
123152
}
124153

125-
impl FactorInstancePreparer<WasiFactor> for InstancePreparer {
126-
// NOTE: Replaces WASI parts of AppComponent::apply_store_config
127-
fn new<Factors: RuntimeFactors>(
128-
ctx: PrepareContext<WasiFactor>,
129-
_preparers: InstancePreparers<Factors>,
130-
) -> anyhow::Result<Self> {
131-
let mut wasi_ctx = WasiCtxBuilder::new();
132-
133-
// Apply environment variables
134-
for (key, val) in ctx.app_component().environment() {
135-
wasi_ctx.env(key, val);
154+
impl Default for InstancePreparer {
155+
fn default() -> Self {
156+
Self {
157+
wasi_ctx: WasiCtxBuilder::new(),
136158
}
137-
138-
// Mount files
139-
let mount_ctx = MountFilesContext {
140-
wasi_ctx: &mut wasi_ctx,
141-
};
142-
ctx.factor()
143-
.files_mounter
144-
.mount_files(ctx.app_component(), mount_ctx)?;
145-
146-
Ok(Self { wasi_ctx })
147-
}
148-
149-
fn prepare(self) -> anyhow::Result<InstanceState> {
150-
let Self { mut wasi_ctx } = self;
151-
Ok(InstanceState {
152-
ctx: wasi_ctx.build(),
153-
table: Default::default(),
154-
})
155159
}
156160
}
157161

crates/factor-wasi/src/preview1.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use spin_factors::{
2-
anyhow, Factor, FactorInstancePreparer, InitContext, InstancePreparers, PrepareContext,
3-
RuntimeFactors,
4-
};
1+
use spin_factors::{anyhow, Factor, InitContext, RuntimeFactors};
52
use wasmtime_wasi::{preview1::WasiP1Ctx, WasiCtxBuilder};
63

74
pub struct WasiPreview1Factor;
@@ -17,23 +14,20 @@ impl Factor for WasiPreview1Factor {
1714
) -> anyhow::Result<()> {
1815
ctx.link_module_bindings(wasmtime_wasi::preview1::add_to_linker_async)
1916
}
17+
18+
fn prepare(&self, mut preparer: InstancePreparer) -> anyhow::Result<WasiP1Ctx> {
19+
Ok(preparer.wasi_ctx.build_p1())
20+
}
2021
}
2122

2223
pub struct InstancePreparer {
2324
wasi_ctx: WasiCtxBuilder,
2425
}
2526

26-
impl FactorInstancePreparer<WasiPreview1Factor> for InstancePreparer {
27-
fn new<Factors: RuntimeFactors>(
28-
_ctx: PrepareContext<WasiPreview1Factor>,
29-
_preparers: InstancePreparers<Factors>,
30-
) -> anyhow::Result<Self> {
31-
Ok(Self {
27+
impl Default for InstancePreparer {
28+
fn default() -> Self {
29+
Self {
3230
wasi_ctx: WasiCtxBuilder::new(),
33-
})
34-
}
35-
36-
fn prepare(mut self) -> anyhow::Result<WasiP1Ctx> {
37-
Ok(self.wasi_ctx.build_p1())
31+
}
3832
}
3933
}

crates/factors-derive/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
112112
};
113113
#(
114114
preparers.#factor_names = Some(
115-
#factors_path::FactorInstancePreparer::<#factor_types>::new::<#name>(
115+
#Factor::create_preparer::<Self>(
116116
#factors_path::PrepareContext::new(
117117
&self.#factor_names,
118118
configured_app.app_config::<#factor_types>().unwrap(),
@@ -124,7 +124,8 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
124124
)*
125125
Ok(#state_name {
126126
#(
127-
#factor_names: #factors_path::FactorInstancePreparer::<#factor_types>::prepare(
127+
#factor_names: #Factor::prepare(
128+
&self.#factor_names,
128129
preparers.#factor_names.unwrap(),
129130
)?,
130131
)*

crates/factors/src/factor.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::any::Any;
22

33
use anyhow::Context;
44

5-
use crate::{App, FactorInstancePreparer, Linker, ModuleLinker, RuntimeConfig, RuntimeFactors};
5+
use crate::{
6+
App, InstancePreparers, Linker, ModuleLinker, PrepareContext, RuntimeConfig, RuntimeFactors,
7+
};
68

79
pub trait Factor: Any + Sized {
810
/// Per-app state for this factor.
@@ -11,7 +13,7 @@ pub trait Factor: Any + Sized {
1113
type AppState: Default;
1214

1315
/// The [`FactorInstancePreparer`] for this factor.
14-
type InstancePreparer: FactorInstancePreparer<Self>;
16+
type InstancePreparer: Default;
1517

1618
/// The per-instance state for this factor, constructed by a
1719
/// [`FactorInstancePreparer`] and available to any host-provided imports
@@ -38,6 +40,18 @@ pub trait Factor: Any + Sized {
3840
_ = ctx;
3941
Ok(Default::default())
4042
}
43+
44+
/// Returns a new instance of this preparer for the given [`Factor`].
45+
fn create_preparer<T: RuntimeFactors>(
46+
ctx: PrepareContext<Self>,
47+
_preparers: InstancePreparers<T>,
48+
) -> anyhow::Result<Self::InstancePreparer> {
49+
_ = ctx;
50+
Ok(Default::default())
51+
}
52+
53+
/// Returns a new instance of the associated [`Factor::InstanceState`].
54+
fn prepare(&self, preparer: Self::InstancePreparer) -> anyhow::Result<Self::InstanceState>;
4155
}
4256

4357
pub(crate) type GetDataFn<Facts, Fact> =

crates/factors/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod factor;
2-
mod instance_preparer;
2+
mod prepare;
33
mod runtime_config;
44
mod spin_factors;
55

@@ -10,7 +10,7 @@ pub use spin_factors_derive::RuntimeFactors;
1010

1111
pub use crate::{
1212
factor::{ConfigureAppContext, ConfiguredApp, Factor, InitContext},
13-
instance_preparer::{FactorInstancePreparer, InstancePreparers, PrepareContext},
13+
prepare::{InstancePreparers, PrepareContext},
1414
runtime_config::{RuntimeConfig, RuntimeConfigSource},
1515
spin_factors::RuntimeFactors,
1616
};

crates/factors/src/instance_preparer.rs renamed to crates/factors/src/prepare.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,6 @@ use anyhow::Context;
22

33
use crate::{AppComponent, Factor, RuntimeFactors};
44

5-
pub trait FactorInstancePreparer<F: Factor>: Sized {
6-
/// Returns a new instance of this preparer for the given [`Factor`].
7-
fn new<T: RuntimeFactors>(
8-
ctx: PrepareContext<F>,
9-
_preparers: InstancePreparers<T>,
10-
) -> anyhow::Result<Self>;
11-
12-
/// Returns a new instance of the associated [`Factor::InstanceState`].
13-
fn prepare(self) -> anyhow::Result<F::InstanceState>;
14-
}
15-
16-
impl<F: Factor> FactorInstancePreparer<F> for ()
17-
where
18-
F::InstanceState: Default,
19-
{
20-
fn new<T: RuntimeFactors>(
21-
_ctx: PrepareContext<F>,
22-
_preparers: InstancePreparers<T>,
23-
) -> anyhow::Result<Self> {
24-
Ok(())
25-
}
26-
27-
fn prepare(self) -> anyhow::Result<F::InstanceState> {
28-
Ok(Default::default())
29-
}
30-
}
31-
325
/// A PrepareContext is passed to [`FactorInstancePreparer::new`], giving access
336
/// to any already-initialized [`FactorInstancePreparer`]s, allowing for
347
/// inter-[`Factor`] dependencies.

0 commit comments

Comments
 (0)