Skip to content

Commit 7dcbc4e

Browse files
lannrylev
authored andcommitted
factors: Remove InstanceFactors
Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent 1d0c79e commit 7dcbc4e

File tree

15 files changed

+52
-84
lines changed

15 files changed

+52
-84
lines changed

crates/factor-key-value/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use std::{
1010
use anyhow::ensure;
1111
use host::KEY_VALUE_STORES_KEY;
1212
use spin_factors::{
13-
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, PrepareContext,
14-
PreparedInstanceBuilders, RuntimeFactors,
13+
ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, PrepareContext, RuntimeFactors,
1514
};
1615
use util::{CachingStoreManager, DefaultManagerGetter};
1716

@@ -89,8 +88,7 @@ impl Factor for KeyValueFactor {
8988

9089
fn prepare<T: RuntimeFactors>(
9190
&self,
92-
ctx: PrepareContext<Self>,
93-
_builders: &mut PreparedInstanceBuilders<T>,
91+
ctx: PrepareContext<T, Self>,
9492
) -> anyhow::Result<InstanceBuilder> {
9593
let app_state = ctx.app_state();
9694
let allowed_stores = app_state

crates/factor-llm/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use std::sync::Arc;
66

77
use async_trait::async_trait;
88
use spin_factors::{
9-
ConfigureAppContext, Factor, PrepareContext, PreparedInstanceBuilders, RuntimeFactors,
10-
SelfInstanceBuilder,
9+
ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
1110
};
1211
use spin_locked_app::MetadataKey;
1312
use spin_world::v1::llm::{self as v1};
@@ -77,8 +76,7 @@ impl Factor for LlmFactor {
7776

7877
fn prepare<T: RuntimeFactors>(
7978
&self,
80-
ctx: PrepareContext<Self>,
81-
_builders: &mut PreparedInstanceBuilders<T>,
79+
ctx: PrepareContext<T, Self>,
8280
) -> anyhow::Result<Self::InstanceBuilder> {
8381
let allowed_models = ctx
8482
.app_state()

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use spin_factor_outbound_networking::{
1414
ComponentTlsConfigs, OutboundAllowedHosts, OutboundNetworkingFactor,
1515
};
1616
use spin_factors::{
17-
anyhow, ConfigureAppContext, Factor, PrepareContext, PreparedInstanceBuilders, RuntimeFactors,
18-
SelfInstanceBuilder,
17+
anyhow, ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
1918
};
2019
use wasmtime_wasi_http::WasiHttpCtx;
2120

@@ -59,10 +58,9 @@ impl Factor for OutboundHttpFactor {
5958

6059
fn prepare<T: RuntimeFactors>(
6160
&self,
62-
_ctx: PrepareContext<Self>,
63-
builders: &mut PreparedInstanceBuilders<T>,
61+
mut ctx: PrepareContext<T, Self>,
6462
) -> anyhow::Result<Self::InstanceBuilder> {
65-
let outbound_networking = builders.get_mut::<OutboundNetworkingFactor>()?;
63+
let outbound_networking = ctx.instance_builder::<OutboundNetworkingFactor>()?;
6664
let allowed_hosts = outbound_networking.allowed_hosts();
6765
let component_tls_configs = outbound_networking.component_tls_configs().clone();
6866
Ok(InstanceState {

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use rumqttc::{AsyncClient, Event, Incoming, Outgoing, QoS};
99
use spin_core::async_trait;
1010
use spin_factor_outbound_networking::OutboundNetworkingFactor;
1111
use spin_factors::{
12-
ConfigureAppContext, Factor, PrepareContext, PreparedInstanceBuilders, RuntimeFactors,
13-
SelfInstanceBuilder,
12+
ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
1413
};
1514
use spin_world::v2::mqtt::{self as v2, Error, Qos};
1615
use tokio::sync::Mutex;
@@ -49,11 +48,10 @@ impl Factor for OutboundMqttFactor {
4948

5049
fn prepare<T: RuntimeFactors>(
5150
&self,
52-
_ctx: PrepareContext<Self>,
53-
builders: &mut PreparedInstanceBuilders<T>,
51+
mut ctx: PrepareContext<T, Self>,
5452
) -> anyhow::Result<Self::InstanceBuilder> {
55-
let allowed_hosts = builders
56-
.get_mut::<OutboundNetworkingFactor>()?
53+
let allowed_hosts = ctx
54+
.instance_builder::<OutboundNetworkingFactor>()?
5755
.allowed_hosts();
5856
Ok(InstanceState::new(
5957
allowed_hosts,

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ impl<C: Send + Sync + Client + 'static> Factor for OutboundMysqlFactor<C> {
3232

3333
fn prepare<T: spin_factors::RuntimeFactors>(
3434
&self,
35-
_ctx: spin_factors::PrepareContext<Self>,
36-
builders: &mut spin_factors::PreparedInstanceBuilders<T>,
35+
mut ctx: spin_factors::PrepareContext<T, Self>,
3736
) -> anyhow::Result<Self::InstanceBuilder> {
38-
let allowed_hosts = builders
39-
.get_mut::<OutboundNetworkingFactor>()?
37+
let allowed_hosts = ctx
38+
.instance_builder::<OutboundNetworkingFactor>()?
4039
.allowed_hosts();
4140
Ok(InstanceState {
4241
allowed_hosts,

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use spin_factor_variables::VariablesFactor;
1313
use spin_factor_wasi::{SocketAddrUse, WasiFactor};
1414
use spin_factors::{
1515
anyhow::{self, Context},
16-
ConfigureAppContext, Error, Factor, FactorInstanceBuilder, PrepareContext,
17-
PreparedInstanceBuilders, RuntimeFactors,
16+
ConfigureAppContext, Error, Factor, FactorInstanceBuilder, PrepareContext, RuntimeFactors,
1817
};
1918

2019
pub use config::{
@@ -82,17 +81,16 @@ impl Factor for OutboundNetworkingFactor {
8281

8382
fn prepare<T: RuntimeFactors>(
8483
&self,
85-
ctx: PrepareContext<Self>,
86-
builders: &mut PreparedInstanceBuilders<T>,
84+
mut ctx: PrepareContext<T, Self>,
8785
) -> anyhow::Result<Self::InstanceBuilder> {
8886
let hosts = ctx
8987
.app_state()
9088
.component_allowed_hosts
9189
.get(ctx.app_component().id())
9290
.cloned()
9391
.context("missing component allowed hosts")?;
94-
let resolver = builders
95-
.get_mut::<VariablesFactor>()?
92+
let resolver = ctx
93+
.instance_builder::<VariablesFactor>()?
9694
.expression_resolver()
9795
.clone();
9896
let allowed_hosts_future = async move {
@@ -103,7 +101,7 @@ impl Factor for OutboundNetworkingFactor {
103101
.boxed()
104102
.shared();
105103

106-
match builders.get_mut::<WasiFactor>() {
104+
match ctx.instance_builder::<WasiFactor>() {
107105
Ok(wasi_builder) => {
108106
// Update Wasi socket allowed ports
109107
let allowed_hosts = OutboundAllowedHosts {

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ mod host;
44
use client::Client;
55
use spin_factor_outbound_networking::{OutboundAllowedHosts, OutboundNetworkingFactor};
66
use spin_factors::{
7-
anyhow, ConfigureAppContext, Factor, PrepareContext, PreparedInstanceBuilders, RuntimeFactors,
8-
SelfInstanceBuilder,
7+
anyhow, ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
98
};
109
use tokio_postgres::Client as PgClient;
1110

@@ -36,11 +35,10 @@ impl<C: Send + Sync + Client + 'static> Factor for OutboundPgFactor<C> {
3635

3736
fn prepare<T: RuntimeFactors>(
3837
&self,
39-
_ctx: PrepareContext<Self>,
40-
builders: &mut PreparedInstanceBuilders<T>,
38+
mut ctx: PrepareContext<T, Self>,
4139
) -> anyhow::Result<Self::InstanceBuilder> {
42-
let allowed_hosts = builders
43-
.get_mut::<OutboundNetworkingFactor>()?
40+
let allowed_hosts = ctx
41+
.instance_builder::<OutboundNetworkingFactor>()?
4442
.allowed_hosts();
4543
Ok(InstanceState {
4644
allowed_hosts,

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ mod host;
33
use host::InstanceState;
44
use spin_factor_outbound_networking::OutboundNetworkingFactor;
55
use spin_factors::{
6-
anyhow, ConfigureAppContext, Factor, PrepareContext, PreparedInstanceBuilders, RuntimeFactors,
7-
SelfInstanceBuilder,
6+
anyhow, ConfigureAppContext, Factor, PrepareContext, RuntimeFactors, SelfInstanceBuilder,
87
};
98

109
/// The [`Factor`] for `fermyon:spin/outbound-redis`.
@@ -42,11 +41,10 @@ impl Factor for OutboundRedisFactor {
4241

4342
fn prepare<T: RuntimeFactors>(
4443
&self,
45-
_ctx: PrepareContext<Self>,
46-
builders: &mut PreparedInstanceBuilders<T>,
44+
mut ctx: PrepareContext<T, Self>,
4745
) -> anyhow::Result<Self::InstanceBuilder> {
48-
let allowed_hosts = builders
49-
.get_mut::<OutboundNetworkingFactor>()?
46+
let allowed_hosts = ctx
47+
.instance_builder::<OutboundNetworkingFactor>()?
5048
.allowed_hosts();
5149
Ok(InstanceState {
5250
allowed_hosts,

crates/factor-sqlite/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ impl Factor for SqliteFactor {
8686

8787
fn prepare<T: spin_factors::RuntimeFactors>(
8888
&self,
89-
ctx: spin_factors::PrepareContext<Self>,
90-
_builders: &mut spin_factors::PreparedInstanceBuilders<T>,
89+
ctx: spin_factors::PrepareContext<T, Self>,
9190
) -> spin_factors::anyhow::Result<Self::InstanceBuilder> {
9291
let allowed_databases = ctx
9392
.app_state()

crates/factor-variables/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::sync::Arc;
77
use runtime_config::RuntimeConfig;
88
use spin_expressions::{ProviderResolver as ExpressionResolver, Template};
99
use spin_factors::{
10-
anyhow, ConfigureAppContext, Factor, InitContext, PrepareContext, PreparedInstanceBuilders,
11-
RuntimeFactors, SelfInstanceBuilder,
10+
anyhow, ConfigureAppContext, Factor, InitContext, PrepareContext, RuntimeFactors,
11+
SelfInstanceBuilder,
1212
};
1313

1414
/// A factor for providing variables to components.
@@ -54,8 +54,7 @@ impl Factor for VariablesFactor {
5454

5555
fn prepare<T: RuntimeFactors>(
5656
&self,
57-
ctx: PrepareContext<Self>,
58-
_builders: &mut PreparedInstanceBuilders<T>,
57+
ctx: PrepareContext<T, Self>,
5958
) -> anyhow::Result<InstanceState> {
6059
let component_id = ctx.app_component().id().to_string();
6160
let expression_resolver = ctx.app_state().expression_resolver.clone();

crates/factor-wasi/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::{
1313
use io::{PipeReadStream, PipedWriteStream};
1414
use spin_factors::{
1515
anyhow, AppComponent, Factor, FactorInstanceBuilder, InitContext, PrepareContext,
16-
PreparedInstanceBuilders, RuntimeFactors, RuntimeFactorsInstanceState,
16+
RuntimeFactors, RuntimeFactorsInstanceState,
1717
};
1818
use wasmtime_wasi::{
1919
DirPerms, FilePerms, ResourceTable, StdinStream, StdoutStream, WasiCtx, WasiCtxBuilder,
@@ -109,8 +109,7 @@ impl Factor for WasiFactor {
109109

110110
fn prepare<T: RuntimeFactors>(
111111
&self,
112-
ctx: PrepareContext<Self>,
113-
_builders: &mut PreparedInstanceBuilders<T>,
112+
ctx: PrepareContext<T, Self>,
114113
) -> anyhow::Result<InstanceBuilder> {
115114
let mut wasi_ctx = WasiCtxBuilder::new();
116115

crates/factors-derive/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
149149
#factors_path::PrepareContext::new(
150150
configured_app.app_state::<#factor_types>().unwrap(),
151151
&app_component,
152+
&mut builders,
152153
),
153-
&mut #factors_path::PreparedInstanceBuilders::new(&mut builders),
154154
).map_err(#Error::factor_prepare_error::<#factor_types>)?
155155
);
156156
)*

crates/factors/src/factor.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ use std::any::Any;
22

33
use wasmtime::component::{Linker, ResourceTable};
44

5-
use crate::{
6-
prepare::FactorInstanceBuilder, App, Error, PrepareContext, PreparedInstanceBuilders,
7-
RuntimeFactors,
8-
};
5+
use crate::{prepare::FactorInstanceBuilder, App, Error, PrepareContext, RuntimeFactors};
96

107
/// A contained (i.e., "factored") piece of runtime functionality.
118
pub trait Factor: Any + Sized {
@@ -65,8 +62,7 @@ pub trait Factor: Any + Sized {
6562
/// used.
6663
fn prepare<T: RuntimeFactors>(
6764
&self,
68-
ctx: PrepareContext<Self>,
69-
_builders: &mut PreparedInstanceBuilders<T>,
65+
ctx: PrepareContext<T, Self>,
7066
) -> anyhow::Result<Self::InstanceBuilder>;
7167
}
7268

@@ -150,12 +146,12 @@ impl<'a, T: RuntimeFactors, F: Factor> ConfigureAppContext<'a, T, F> {
150146
}
151147

152148
/// Get the [`App`] being configured.
153-
pub fn app(&self) -> &App {
149+
pub fn app(&self) -> &'a App {
154150
self.app
155151
}
156152

157153
/// Get the app state related to the given factor.
158-
pub fn app_state<U: Factor>(&self) -> crate::Result<&U::AppState> {
154+
pub fn app_state<U: Factor>(&self) -> crate::Result<&'a U::AppState> {
159155
T::app_state::<U>(self.app_state).ok_or(Error::no_such_factor::<U>())
160156
}
161157

crates/factors/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ pub use spin_factors_derive::RuntimeFactors;
1212

1313
pub use crate::{
1414
factor::{ConfigureAppContext, ConfiguredApp, Factor, FactorInstanceState, InitContext},
15-
prepare::{
16-
FactorInstanceBuilder, PrepareContext, PreparedInstanceBuilders, SelfInstanceBuilder,
17-
},
15+
prepare::{FactorInstanceBuilder, PrepareContext, SelfInstanceBuilder},
1816
runtime_config::{FactorRuntimeConfigSource, RuntimeConfigSourceFinalizer},
1917
runtime_factors::{
2018
AsInstanceState, HasInstanceBuilder, RuntimeFactors, RuntimeFactorsInstanceState,

crates/factors/src/prepare.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -39,51 +39,43 @@ impl<T: SelfInstanceBuilder> FactorInstanceBuilder for T {
3939
/// A PrepareContext is passed to [`Factor::prepare`].
4040
///
4141
/// This gives the factor access to app state and the app component.
42-
pub struct PrepareContext<'a, F: Factor> {
42+
pub struct PrepareContext<'a, T: RuntimeFactors, F: Factor> {
4343
pub(crate) app_state: &'a F::AppState,
4444
pub(crate) app_component: &'a AppComponent<'a>,
45+
pub(crate) instance_builders: &'a mut T::InstanceBuilders,
4546
}
4647

47-
impl<'a, F: Factor> PrepareContext<'a, F> {
48+
impl<'a, T: RuntimeFactors, F: Factor> PrepareContext<'a, T, F> {
4849
#[doc(hidden)]
49-
pub fn new(app_state: &'a F::AppState, app_component: &'a AppComponent) -> Self {
50+
pub fn new(
51+
app_state: &'a F::AppState,
52+
app_component: &'a AppComponent,
53+
instance_builders: &'a mut T::InstanceBuilders,
54+
) -> Self {
5055
Self {
5156
app_state,
5257
app_component,
58+
instance_builders,
5359
}
5460
}
5561

5662
/// Get the app state related to the factor.
57-
pub fn app_state(&self) -> &F::AppState {
63+
pub fn app_state(&self) -> &'a F::AppState {
5864
self.app_state
5965
}
6066

6167
/// Get the app component.
62-
pub fn app_component(&self) -> &AppComponent {
68+
pub fn app_component(&self) -> &'a AppComponent {
6369
self.app_component
6470
}
65-
}
66-
67-
/// The collection of all the already prepared `InstanceBuilder`s.
68-
///
69-
/// Use `InstanceBuilders::get_mut` to get a mutable reference to a specific factor's instance builder.
70-
pub struct PreparedInstanceBuilders<'a, T: RuntimeFactors> {
71-
pub(crate) inner: &'a mut T::InstanceBuilders,
72-
}
73-
74-
impl<'a, T: RuntimeFactors> PreparedInstanceBuilders<'a, T> {
75-
#[doc(hidden)]
76-
pub fn new(inner: &'a mut T::InstanceBuilders) -> Self {
77-
Self { inner }
78-
}
7971

8072
/// Returns the prepared [`FactorInstanceBuilder`] for the given [`Factor`].
8173
///
8274
/// Fails if the current [`RuntimeFactors`] does not include the given
8375
/// [`Factor`] or if the given [`Factor`]'s builder has not been prepared
8476
/// yet (because it is sequenced after this factor).
85-
pub fn get_mut<U: Factor>(&mut self) -> crate::Result<&mut U::InstanceBuilder> {
86-
T::instance_builder_mut::<U>(self.inner)
77+
pub fn instance_builder<U: Factor>(&mut self) -> crate::Result<&mut U::InstanceBuilder> {
78+
T::instance_builder_mut::<U>(self.instance_builders)
8779
.ok_or(Error::no_such_factor::<U>())?
8880
.ok_or_else(|| {
8981
Error::DependencyOrderingError(format!(

0 commit comments

Comments
 (0)