Skip to content

Commit fa09b41

Browse files
authored
Merge pull request #3036 from lann/load-instance-pre
factors-executor: Add ComponentLoader::load_instance_pre
2 parents 0126b61 + b162709 commit fa09b41

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

crates/app/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl App {
8484
}
8585

8686
/// Returns an iterator of [`AppComponent`]s defined for this app.
87-
pub fn components(&self) -> impl Iterator<Item = AppComponent<'_>> {
87+
pub fn components(&self) -> impl ExactSizeIterator<Item = AppComponent<'_>> {
8888
self.locked
8989
.components
9090
.iter()

crates/factors-executor/src/lib.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
5252
self: Arc<Self>,
5353
app: App,
5454
runtime_config: T::RuntimeConfig,
55-
component_loader: &impl ComponentLoader,
55+
component_loader: &impl ComponentLoader<T, U>,
5656
) -> anyhow::Result<FactorsExecutorApp<T, U>> {
5757
let configured_app = self
5858
.factors
@@ -63,15 +63,14 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
6363
hooks.configure_app(&configured_app).await?;
6464
}
6565

66-
let mut component_instance_pres = HashMap::new();
66+
let components = configured_app.app().components();
67+
let mut component_instance_pres = HashMap::with_capacity(components.len());
6768

68-
for app_component in configured_app.app().components() {
69-
let component = component_loader
70-
.load_component(self.core_engine.as_ref(), &app_component)
69+
for component in components {
70+
let instance_pre = component_loader
71+
.load_instance_pre(&self.core_engine, &component)
7172
.await?;
72-
let instance_pre = self.core_engine.instantiate_pre(&component)?;
73-
74-
component_instance_pres.insert(app_component.id().to_string(), instance_pre);
73+
component_instance_pres.insert(component.id().to_string(), instance_pre);
7574
}
7675

7776
Ok(FactorsExecutorApp {
@@ -102,13 +101,23 @@ where
102101

103102
/// A ComponentLoader is responsible for loading Wasmtime [`Component`]s.
104103
#[async_trait]
105-
pub trait ComponentLoader {
104+
pub trait ComponentLoader<T: RuntimeFactors, U>: Sync {
106105
/// Loads a [`Component`] for the given [`AppComponent`].
107106
async fn load_component(
108107
&self,
109108
engine: &spin_core::wasmtime::Engine,
110109
component: &AppComponent,
111110
) -> anyhow::Result<Component>;
111+
112+
/// Loads [`InstancePre`] for the given [`AppComponent`].
113+
async fn load_instance_pre(
114+
&self,
115+
engine: &spin_core::Engine<InstanceState<T::InstanceState, U>>,
116+
component: &AppComponent,
117+
) -> anyhow::Result<spin_core::InstancePre<InstanceState<T::InstanceState, U>>> {
118+
let component = self.load_component(engine.as_ref(), component).await?;
119+
engine.instantiate_pre(&component)
120+
}
112121
}
113122

114123
type InstancePre<T, U> =
@@ -344,7 +353,7 @@ mod tests {
344353
struct DummyComponentLoader;
345354

346355
#[async_trait]
347-
impl ComponentLoader for DummyComponentLoader {
356+
impl ComponentLoader<TestFactors, ()> for DummyComponentLoader {
348357
async fn load_component(
349358
&self,
350359
engine: &spin_core::wasmtime::Engine,

crates/trigger/src/cli.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
309309
app: App,
310310
common_options: FactorsConfig,
311311
options: B::CliArgs,
312-
loader: &impl ComponentLoader,
312+
loader: &impl ComponentLoader<B::Factors, T::InstanceState>,
313313
) -> anyhow::Result<TriggerApp<T, B::Factors>> {
314314
let mut core_engine_builder = {
315315
self.trigger.update_core_config(&mut self.engine_config)?;
@@ -340,7 +340,7 @@ impl<T: Trigger<B::Factors>, B: RuntimeFactorsBuilder> TriggerAppBuilder<T, B> {
340340
app: App,
341341
common_options: FactorsConfig,
342342
options: B::CliArgs,
343-
loader: &impl ComponentLoader,
343+
loader: &impl ComponentLoader<B::Factors, T::InstanceState>,
344344
) -> anyhow::Result<impl Future<Output = anyhow::Result<()>>> {
345345
let configured_app = self.build(app, common_options, options, loader).await?;
346346
Ok(self.trigger.run(configured_app))

crates/trigger/src/loader.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::Context as _;
22
use spin_common::{ui::quoted_path, url::parse_file_url};
33
use spin_compose::ComponentSourceLoaderFs;
44
use spin_core::{async_trait, wasmtime, Component};
5-
use spin_factors::AppComponent;
5+
use spin_factors::{AppComponent, RuntimeFactors};
66

77
#[derive(Default)]
88
pub struct ComponentLoader {
@@ -68,7 +68,7 @@ impl ComponentLoader {
6868
}
6969

7070
#[async_trait]
71-
impl spin_factors_executor::ComponentLoader for ComponentLoader {
71+
impl<T: RuntimeFactors, U> spin_factors_executor::ComponentLoader<T, U> for ComponentLoader {
7272
async fn load_component(
7373
&self,
7474
engine: &wasmtime::Engine,

0 commit comments

Comments
 (0)