Skip to content

Commit 662ffbf

Browse files
committed
Loosen ownership/borrowing requirements in FactorExecutor
Signed-off-by: Ryan Levick <[email protected]>
1 parent f7f1284 commit 662ffbf

File tree

1 file changed

+34
-17
lines changed
  • crates/factors-executor/src

1 file changed

+34
-17
lines changed

crates/factors-executor/src/lib.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, sync::Arc};
22

33
use anyhow::Context;
44
use spin_app::{App, AppComponent};
@@ -35,10 +35,12 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
3535
hooks: Default::default(),
3636
})
3737
}
38-
}
3938

40-
impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
41-
/// Adds the given [`ExecutorHooks`] to this executor.
39+
pub fn core_engine(&self) -> &spin_core::Engine<InstanceState<T::InstanceState, U>> {
40+
&self.core_engine
41+
}
42+
43+
// Adds the given [`ExecutorHooks`] to this executor.
4244
///
4345
/// Hooks are run in the order they are added.
4446
pub fn add_hooks(&mut self, hooks: impl ExecutorHooks<T, U> + 'static) {
@@ -47,17 +49,17 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
4749

4850
/// Loads a [`FactorsApp`] with this executor.
4951
pub async fn load_app(
50-
mut self,
52+
self: Arc<Self>,
5153
app: App,
5254
runtime_config: T::RuntimeConfig,
53-
mut component_loader: impl ComponentLoader,
55+
component_loader: &impl ComponentLoader,
5456
) -> anyhow::Result<FactorsExecutorApp<T, U>> {
5557
let configured_app = self
5658
.factors
5759
.configure_app(app, runtime_config)
5860
.context("failed to configure app")?;
5961

60-
for hooks in &mut self.hooks {
62+
for hooks in &self.hooks {
6163
hooks.configure_app(&configured_app).await?;
6264
}
6365

@@ -73,7 +75,7 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
7375
}
7476

7577
Ok(FactorsExecutorApp {
76-
executor: self,
78+
executor: self.clone(),
7779
configured_app,
7880
component_instance_pres,
7981
})
@@ -86,7 +88,7 @@ where
8688
T: RuntimeFactors,
8789
{
8890
/// Configure app hooks run immediately after [`RuntimeFactors::configure_app`].
89-
async fn configure_app(&mut self, configured_app: &ConfiguredApp<T>) -> anyhow::Result<()> {
91+
async fn configure_app(&self, configured_app: &ConfiguredApp<T>) -> anyhow::Result<()> {
9092
let _ = configured_app;
9193
Ok(())
9294
}
@@ -103,7 +105,7 @@ where
103105
pub trait ComponentLoader {
104106
/// Loads a [`Component`] for the given [`AppComponent`].
105107
async fn load_component(
106-
&mut self,
108+
&self,
107109
engine: &spin_core::wasmtime::Engine,
108110
component: &AppComponent,
109111
) -> anyhow::Result<Component>;
@@ -117,7 +119,7 @@ type InstancePre<T, U> =
117119
/// It is generic over the executor's [`RuntimeFactors`] and any ad-hoc additional
118120
/// per-instance state needed by the caller.
119121
pub struct FactorsExecutorApp<T: RuntimeFactors, U> {
120-
executor: FactorsExecutor<T, U>,
122+
executor: Arc<FactorsExecutor<T, U>>,
121123
configured_app: ConfiguredApp<T>,
122124
// Maps component IDs -> InstancePres
123125
component_instance_pres: HashMap<String, InstancePre<T, U>>,
@@ -249,13 +251,28 @@ impl<T, U> InstanceState<T, U> {
249251
&self.core
250252
}
251253

254+
/// Provides mutable access to the [`spin_core::State`].
255+
pub fn core_state_mut(&mut self) -> &mut spin_core::State {
256+
&mut self.core
257+
}
258+
252259
/// Provides access to the [`RuntimeFactors::InstanceState`].
253-
pub fn factors_instance_state(&mut self) -> &mut T {
260+
pub fn factors_instance_state(&self) -> &T {
261+
&self.factors
262+
}
263+
264+
/// Provides mutable access to the [`RuntimeFactors::InstanceState`].
265+
pub fn factors_instance_state_mut(&mut self) -> &mut T {
254266
&mut self.factors
255267
}
256268

257-
/// Provides access to the `Self::ExecutorInstanceState`.
258-
pub fn executor_instance_state(&mut self) -> &mut U {
269+
/// Provides access to the ad-hoc executor instance state.
270+
pub fn executor_instance_state(&self) -> &U {
271+
&self.executor
272+
}
273+
274+
/// Provides mutable access to the ad-hoc executor instance state.
275+
pub fn executor_instance_state_mut(&mut self) -> &mut U {
259276
&mut self.executor
260277
}
261278
}
@@ -295,10 +312,10 @@ mod tests {
295312
let app = App::new("test-app", locked);
296313

297314
let engine_builder = spin_core::Engine::builder(&Default::default())?;
298-
let executor = FactorsExecutor::new(engine_builder, env.factors)?;
315+
let executor = Arc::new(FactorsExecutor::new(engine_builder, env.factors)?);
299316

300317
let factors_app = executor
301-
.load_app(app, Default::default(), DummyComponentLoader)
318+
.load_app(app, Default::default(), &mut DummyComponentLoader)
302319
.await?;
303320

304321
let mut instance_builder = factors_app.prepare("empty")?;
@@ -321,7 +338,7 @@ mod tests {
321338
#[async_trait]
322339
impl ComponentLoader for DummyComponentLoader {
323340
async fn load_component(
324-
&mut self,
341+
&self,
325342
engine: &spin_core::wasmtime::Engine,
326343
_component: &AppComponent,
327344
) -> anyhow::Result<Component> {

0 commit comments

Comments
 (0)