Skip to content

Commit 138efde

Browse files
committed
factors: Rename SpinFactors->RuntimeFactors
And shorten some generic params. Signed-off-by: Lann Martin <lann.martin@fermyon.com>
1 parent 0bf64cc commit 138efde

File tree

10 files changed

+114
-117
lines changed

10 files changed

+114
-117
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ use spin_factor_wasi::WasiFactor;
99
use spin_factors::{
1010
anyhow::{self, Context},
1111
ConfigureAppContext, Factor, FactorInstancePreparer, InstancePreparers, PrepareContext,
12-
RuntimeConfig, SpinFactors,
12+
RuntimeConfig, RuntimeFactors,
1313
};
1414
use spin_outbound_networking::{AllowedHostsConfig, ALLOWED_HOSTS_KEY};
1515

1616
pub struct OutboundNetworkingFactor;
1717

1818
impl Factor for OutboundNetworkingFactor {
19-
type AppConfig = AppConfig;
19+
type AppState = AppState;
2020
type InstancePreparer = InstancePreparer;
2121
type InstanceState = ();
2222

23-
fn configure_app<Factors: SpinFactors>(
23+
fn configure_app<Factors: RuntimeFactors>(
2424
&self,
2525
ctx: ConfigureAppContext<Factors>,
2626
_runtime_config: &mut impl RuntimeConfig,
27-
) -> anyhow::Result<Self::AppConfig> {
27+
) -> anyhow::Result<Self::AppState> {
2828
// Extract allowed_outbound_hosts for all components
2929
let component_allowed_hosts = ctx
3030
.app()
@@ -40,14 +40,14 @@ impl Factor for OutboundNetworkingFactor {
4040
))
4141
})
4242
.collect::<anyhow::Result<_>>()?;
43-
Ok(AppConfig {
43+
Ok(AppState {
4444
component_allowed_hosts,
4545
})
4646
}
4747
}
4848

4949
#[derive(Default)]
50-
pub struct AppConfig {
50+
pub struct AppState {
5151
component_allowed_hosts: HashMap<String, Arc<[String]>>,
5252
}
5353

@@ -58,7 +58,7 @@ pub struct InstancePreparer {
5858
}
5959

6060
impl FactorInstancePreparer<OutboundNetworkingFactor> for InstancePreparer {
61-
fn new<Factors: SpinFactors>(
61+
fn new<Factors: RuntimeFactors>(
6262
ctx: PrepareContext<OutboundNetworkingFactor>,
6363
mut preparers: InstancePreparers<Factors>,
6464
) -> anyhow::Result<Self> {

crates/factor-variables/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ use std::sync::Arc;
33
use spin_expressions::ProviderResolver;
44
use spin_factors::{
55
anyhow, ConfigureAppContext, Factor, FactorInstancePreparer, InitContext, InstancePreparers,
6-
PrepareContext, RuntimeConfig, SpinFactors,
6+
PrepareContext, RuntimeConfig, RuntimeFactors,
77
};
88
use spin_world::{async_trait, v1::config as v1_config, v2::variables};
99

1010
pub struct VariablesFactor;
1111

1212
impl Factor for VariablesFactor {
13-
type AppConfig = AppConfig;
13+
type AppState = AppState;
1414
type InstancePreparer = InstancePreparer;
1515
type InstanceState = InstanceState;
1616

17-
fn init<Factors: SpinFactors>(
17+
fn init<Factors: RuntimeFactors>(
1818
&mut self,
1919
mut ctx: InitContext<Factors, Self>,
2020
) -> anyhow::Result<()> {
@@ -23,11 +23,11 @@ impl Factor for VariablesFactor {
2323
Ok(())
2424
}
2525

26-
fn configure_app<Factors: SpinFactors>(
26+
fn configure_app<Factors: RuntimeFactors>(
2727
&self,
2828
ctx: ConfigureAppContext<Factors>,
2929
_runtime_config: &mut impl RuntimeConfig,
30-
) -> anyhow::Result<Self::AppConfig> {
30+
) -> anyhow::Result<Self::AppState> {
3131
let app = ctx.app();
3232
let mut resolver =
3333
ProviderResolver::new(app.variables().map(|(key, val)| (key.clone(), val.clone())))?;
@@ -38,14 +38,14 @@ impl Factor for VariablesFactor {
3838
)?;
3939
}
4040
// TODO: add providers from runtime config
41-
Ok(AppConfig {
41+
Ok(AppState {
4242
resolver: Arc::new(resolver),
4343
})
4444
}
4545
}
4646

4747
#[derive(Default)]
48-
pub struct AppConfig {
48+
pub struct AppState {
4949
resolver: Arc<ProviderResolver>,
5050
}
5151

@@ -60,7 +60,7 @@ impl InstancePreparer {
6060
}
6161

6262
impl FactorInstancePreparer<VariablesFactor> for InstancePreparer {
63-
fn new<Factors: SpinFactors>(
63+
fn new<Factors: RuntimeFactors>(
6464
ctx: PrepareContext<VariablesFactor>,
6565
_preparers: InstancePreparers<Factors>,
6666
) -> anyhow::Result<Self> {

crates/factor-wasi/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{future::Future, net::SocketAddr, path::Path};
44

55
use spin_factors::{
66
anyhow, AppComponent, Factor, FactorInstancePreparer, InitContext, InstancePreparers,
7-
PrepareContext, SpinFactors,
7+
PrepareContext, RuntimeFactors,
88
};
99
use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView};
1010

@@ -21,11 +21,11 @@ impl WasiFactor {
2121
}
2222

2323
impl Factor for WasiFactor {
24-
type AppConfig = ();
24+
type AppState = ();
2525
type InstancePreparer = InstancePreparer;
2626
type InstanceState = InstanceState;
2727

28-
fn init<Factors: SpinFactors>(
28+
fn init<Factors: RuntimeFactors>(
2929
&mut self,
3030
mut ctx: InitContext<Factors, Self>,
3131
) -> anyhow::Result<()> {
@@ -124,7 +124,7 @@ pub struct InstancePreparer {
124124

125125
impl FactorInstancePreparer<WasiFactor> for InstancePreparer {
126126
// NOTE: Replaces WASI parts of AppComponent::apply_store_config
127-
fn new<Factors: SpinFactors>(
127+
fn new<Factors: RuntimeFactors>(
128128
ctx: PrepareContext<WasiFactor>,
129129
_preparers: InstancePreparers<Factors>,
130130
) -> anyhow::Result<Self> {

crates/factor-wasi/src/preview1.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use spin_factors::{
22
anyhow, Factor, FactorInstancePreparer, InitContext, InstancePreparers, PrepareContext,
3-
SpinFactors,
3+
RuntimeFactors,
44
};
55
use wasmtime_wasi::{preview1::WasiP1Ctx, WasiCtxBuilder};
66

77
pub struct WasiPreview1Factor;
88

99
impl Factor for WasiPreview1Factor {
10-
type AppConfig = ();
10+
type AppState = ();
1111
type InstancePreparer = InstancePreparer;
1212
type InstanceState = WasiP1Ctx;
1313

14-
fn init<Factors: SpinFactors>(
14+
fn init<Factors: RuntimeFactors>(
1515
&mut self,
1616
mut ctx: InitContext<Factors, Self>,
1717
) -> anyhow::Result<()> {
@@ -24,7 +24,7 @@ pub struct InstancePreparer {
2424
}
2525

2626
impl FactorInstancePreparer<WasiPreview1Factor> for InstancePreparer {
27-
fn new<Factors: SpinFactors>(
27+
fn new<Factors: RuntimeFactors>(
2828
_ctx: PrepareContext<WasiPreview1Factor>,
2929
_preparers: InstancePreparers<Factors>,
3030
) -> anyhow::Result<Self> {

crates/factors-derive/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use proc_macro2::TokenStream;
22
use quote::{format_ident, quote};
33
use syn::{parse_macro_input, Data, DeriveInput, Error};
44

5-
#[proc_macro_derive(SpinFactors)]
5+
#[proc_macro_derive(RuntimeFactors)]
66
pub fn derive_factors(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
77
let input = parse_macro_input!(input as DeriveInput);
88
let expanded = expand_factors(&input).unwrap_or_else(|err| err.into_compile_error());
@@ -20,7 +20,7 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
2020
let name = &input.ident;
2121
let vis = &input.vis;
2222

23-
let app_configs_name = format_ident!("{name}AppConfigs");
23+
let app_configs_name = format_ident!("{name}AppState");
2424
let preparers_name = format_ident!("{name}InstancePreparers");
2525
let state_name = format_ident!("{name}InstanceState");
2626

@@ -133,8 +133,8 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
133133

134134
}
135135

136-
impl #factors_path::SpinFactors for #name {
137-
type AppConfigs = #app_configs_name;
136+
impl #factors_path::RuntimeFactors for #name {
137+
type AppState = #app_configs_name;
138138
type InstancePreparers = #preparers_name;
139139
type InstanceState = #state_name;
140140

@@ -159,7 +159,7 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
159159

160160
}
161161

162-
fn app_config<T: #Factor>(app_configs: &Self::AppConfigs) -> Option<&T::AppConfig> {
162+
fn app_config<T: #Factor>(app_configs: &Self::AppState) -> Option<&T::AppState> {
163163
let type_id = #TypeId::of::<T>();
164164
#(
165165
if type_id == #TypeId::of::<#factor_types>() {
@@ -172,7 +172,7 @@ fn expand_factors(input: &DeriveInput) -> syn::Result<TokenStream> {
172172

173173
#vis struct #app_configs_name {
174174
#(
175-
pub #factor_names: Option<<#factor_types as #Factor>::AppConfig>,
175+
pub #factor_names: Option<<#factor_types as #Factor>::AppState>,
176176
)*
177177
}
178178

crates/factors/src/factor.rs

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::any::Any;
22

33
use anyhow::Context;
44

5-
use crate::{App, FactorInstancePreparer, Linker, ModuleLinker, RuntimeConfig, SpinFactors};
5+
use crate::{App, FactorInstancePreparer, Linker, ModuleLinker, RuntimeConfig, RuntimeFactors};
66

77
pub trait Factor: Any + Sized {
8-
/// Per-app configuration for this factor.
8+
/// Per-app state for this factor.
99
///
1010
/// See [`Factor::configure_app`].
11-
type AppConfig: Default;
11+
type AppState: Default;
1212

1313
/// The [`FactorInstancePreparer`] for this factor.
1414
type InstancePreparer: FactorInstancePreparer<Self>;
@@ -20,10 +20,7 @@ pub trait Factor: Any + Sized {
2020

2121
/// Initializes this Factor for a runtime. This will be called at most once,
2222
/// before any call to [`FactorInstancePreparer::new`]
23-
fn init<Factors: SpinFactors>(
24-
&mut self,
25-
mut ctx: InitContext<Factors, Self>,
26-
) -> anyhow::Result<()> {
23+
fn init<T: RuntimeFactors>(&mut self, mut ctx: InitContext<T, Self>) -> anyhow::Result<()> {
2724
// TODO: Should `ctx` always be immut? Rename this param/type?
2825
_ = &mut ctx;
2926
Ok(())
@@ -33,33 +30,33 @@ pub trait Factor: Any + Sized {
3330
/// [`App`]. A runtime may - but is not required to - reuse the returned
3431
/// config across multiple instances. Note that this may be called without
3532
/// any call to `init` in cases where only validation is needed.
36-
fn configure_app<Factors: SpinFactors>(
33+
fn configure_app<T: RuntimeFactors>(
3734
&self,
38-
ctx: ConfigureAppContext<Factors>,
35+
ctx: ConfigureAppContext<T>,
3936
_runtime_config: &mut impl RuntimeConfig,
40-
) -> anyhow::Result<Self::AppConfig> {
37+
) -> anyhow::Result<Self::AppState> {
4138
_ = ctx;
4239
Ok(Default::default())
4340
}
4441
}
4542

46-
pub(crate) type GetDataFn<Factors, Fact> =
47-
fn(&mut <Factors as SpinFactors>::InstanceState) -> &mut <Fact as Factor>::InstanceState;
43+
pub(crate) type GetDataFn<Facts, Fact> =
44+
fn(&mut <Facts as RuntimeFactors>::InstanceState) -> &mut <Fact as Factor>::InstanceState;
4845

4946
/// An InitContext is passed to [`Factor::init`], giving access to the global
5047
/// common [`wasmtime::component::Linker`].
51-
pub struct InitContext<'a, Factors: SpinFactors, T: Factor> {
52-
pub(crate) linker: Option<&'a mut Linker<Factors>>,
53-
pub(crate) module_linker: Option<&'a mut ModuleLinker<Factors>>,
54-
pub(crate) get_data: GetDataFn<Factors, T>,
48+
pub struct InitContext<'a, T: RuntimeFactors, F: Factor> {
49+
pub(crate) linker: Option<&'a mut Linker<T>>,
50+
pub(crate) module_linker: Option<&'a mut ModuleLinker<T>>,
51+
pub(crate) get_data: GetDataFn<T, F>,
5552
}
5653

57-
impl<'a, Factors: SpinFactors, T: Factor> InitContext<'a, Factors, T> {
54+
impl<'a, T: RuntimeFactors, F: Factor> InitContext<'a, T, F> {
5855
#[doc(hidden)]
5956
pub fn new(
60-
linker: Option<&'a mut Linker<Factors>>,
61-
module_linker: Option<&'a mut ModuleLinker<Factors>>,
62-
get_data: GetDataFn<Factors, T>,
57+
linker: Option<&'a mut Linker<T>>,
58+
module_linker: Option<&'a mut ModuleLinker<T>>,
59+
get_data: GetDataFn<T, F>,
6360
) -> Self {
6461
Self {
6562
linker,
@@ -68,23 +65,23 @@ impl<'a, Factors: SpinFactors, T: Factor> InitContext<'a, Factors, T> {
6865
}
6966
}
7067

71-
pub fn linker(&mut self) -> Option<&mut Linker<Factors>> {
68+
pub fn linker(&mut self) -> Option<&mut Linker<T>> {
7269
self.linker.as_deref_mut()
7370
}
7471

75-
pub fn module_linker(&mut self) -> Option<&mut ModuleLinker<Factors>> {
72+
pub fn module_linker(&mut self) -> Option<&mut ModuleLinker<T>> {
7673
self.module_linker.as_deref_mut()
7774
}
7875

79-
pub fn get_data_fn(&self) -> GetDataFn<Factors, T> {
76+
pub fn get_data_fn(&self) -> GetDataFn<T, F> {
8077
self.get_data
8178
}
8279

8380
pub fn link_bindings(
8481
&mut self,
8582
add_to_linker: impl Fn(
86-
&mut Linker<Factors>,
87-
fn(&mut Factors::InstanceState) -> &mut T::InstanceState,
83+
&mut Linker<T>,
84+
fn(&mut T::InstanceState) -> &mut F::InstanceState,
8885
) -> anyhow::Result<()>,
8986
) -> anyhow::Result<()>
9087
where {
@@ -98,8 +95,8 @@ where {
9895
pub fn link_module_bindings(
9996
&mut self,
10097
add_to_linker: impl Fn(
101-
&mut ModuleLinker<Factors>,
102-
fn(&mut Factors::InstanceState) -> &mut T::InstanceState,
98+
&mut ModuleLinker<T>,
99+
fn(&mut T::InstanceState) -> &mut F::InstanceState,
103100
) -> anyhow::Result<()>,
104101
) -> anyhow::Result<()>
105102
where {
@@ -111,42 +108,42 @@ where {
111108
}
112109
}
113110

114-
pub struct ConfigureAppContext<'a, Factors: SpinFactors> {
111+
pub struct ConfigureAppContext<'a, T: RuntimeFactors> {
115112
pub(crate) app: &'a App,
116-
pub(crate) app_configs: &'a Factors::AppConfigs,
113+
pub(crate) app_configs: &'a T::AppState,
117114
}
118115

119-
impl<'a, Factors: SpinFactors> ConfigureAppContext<'a, Factors> {
116+
impl<'a, T: RuntimeFactors> ConfigureAppContext<'a, T> {
120117
#[doc(hidden)]
121-
pub fn new(app: &'a App, app_configs: &'a Factors::AppConfigs) -> Self {
118+
pub fn new(app: &'a App, app_configs: &'a T::AppState) -> Self {
122119
Self { app, app_configs }
123120
}
124121

125122
pub fn app(&self) -> &App {
126123
self.app
127124
}
128125

129-
pub fn app_config<T: Factor>(&self) -> crate::Result<&T::AppConfig> {
130-
Factors::app_config::<T>(self.app_configs).context("no such factor")
126+
pub fn app_config<F: Factor>(&self) -> crate::Result<&F::AppState> {
127+
T::app_config::<F>(self.app_configs).context("no such factor")
131128
}
132129
}
133130

134-
pub struct ConfiguredApp<Factors: SpinFactors> {
131+
pub struct ConfiguredApp<T: RuntimeFactors> {
135132
app: App,
136-
app_configs: Factors::AppConfigs,
133+
app_configs: T::AppState,
137134
}
138135

139-
impl<Factors: SpinFactors> ConfiguredApp<Factors> {
136+
impl<T: RuntimeFactors> ConfiguredApp<T> {
140137
#[doc(hidden)]
141-
pub fn new(app: App, app_configs: Factors::AppConfigs) -> Self {
138+
pub fn new(app: App, app_configs: T::AppState) -> Self {
142139
Self { app, app_configs }
143140
}
144141

145142
pub fn app(&self) -> &App {
146143
&self.app
147144
}
148145

149-
pub fn app_config<T: Factor>(&self) -> crate::Result<&T::AppConfig> {
150-
Factors::app_config::<T>(&self.app_configs).context("no such factor")
146+
pub fn app_config<F: Factor>(&self) -> crate::Result<&F::AppState> {
147+
T::app_config::<F>(&self.app_configs).context("no such factor")
151148
}
152149
}

0 commit comments

Comments
 (0)