1
- use std:: collections:: HashMap ;
1
+ use std:: { collections:: HashMap , sync :: Arc } ;
2
2
3
3
use anyhow:: Context ;
4
4
use spin_app:: { App , AppComponent } ;
@@ -35,10 +35,12 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
35
35
hooks : Default :: default ( ) ,
36
36
} )
37
37
}
38
- }
39
38
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.
42
44
///
43
45
/// Hooks are run in the order they are added.
44
46
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> {
47
49
48
50
/// Loads a [`FactorsApp`] with this executor.
49
51
pub async fn load_app (
50
- mut self ,
52
+ self : Arc < Self > ,
51
53
app : App ,
52
54
runtime_config : T :: RuntimeConfig ,
53
- mut component_loader : impl ComponentLoader ,
55
+ component_loader : & impl ComponentLoader ,
54
56
) -> anyhow:: Result < FactorsExecutorApp < T , U > > {
55
57
let configured_app = self
56
58
. factors
57
59
. configure_app ( app, runtime_config)
58
60
. context ( "failed to configure app" ) ?;
59
61
60
- for hooks in & mut self . hooks {
62
+ for hooks in & self . hooks {
61
63
hooks. configure_app ( & configured_app) . await ?;
62
64
}
63
65
@@ -73,7 +75,7 @@ impl<T: RuntimeFactors, U: Send + 'static> FactorsExecutor<T, U> {
73
75
}
74
76
75
77
Ok ( FactorsExecutorApp {
76
- executor : self ,
78
+ executor : self . clone ( ) ,
77
79
configured_app,
78
80
component_instance_pres,
79
81
} )
86
88
T : RuntimeFactors ,
87
89
{
88
90
/// 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 < ( ) > {
90
92
let _ = configured_app;
91
93
Ok ( ( ) )
92
94
}
@@ -103,7 +105,7 @@ where
103
105
pub trait ComponentLoader {
104
106
/// Loads a [`Component`] for the given [`AppComponent`].
105
107
async fn load_component (
106
- & mut self ,
108
+ & self ,
107
109
engine : & spin_core:: wasmtime:: Engine ,
108
110
component : & AppComponent ,
109
111
) -> anyhow:: Result < Component > ;
@@ -117,7 +119,7 @@ type InstancePre<T, U> =
117
119
/// It is generic over the executor's [`RuntimeFactors`] and any ad-hoc additional
118
120
/// per-instance state needed by the caller.
119
121
pub struct FactorsExecutorApp < T : RuntimeFactors , U > {
120
- executor : FactorsExecutor < T , U > ,
122
+ executor : Arc < FactorsExecutor < T , U > > ,
121
123
configured_app : ConfiguredApp < T > ,
122
124
// Maps component IDs -> InstancePres
123
125
component_instance_pres : HashMap < String , InstancePre < T , U > > ,
@@ -249,13 +251,28 @@ impl<T, U> InstanceState<T, U> {
249
251
& self . core
250
252
}
251
253
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
+
252
259
/// 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 {
254
266
& mut self . factors
255
267
}
256
268
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 {
259
276
& mut self . executor
260
277
}
261
278
}
@@ -295,10 +312,10 @@ mod tests {
295
312
let app = App :: new ( "test-app" , locked) ;
296
313
297
314
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 ) ?) ;
299
316
300
317
let factors_app = executor
301
- . load_app ( app, Default :: default ( ) , DummyComponentLoader )
318
+ . load_app ( app, Default :: default ( ) , & mut DummyComponentLoader )
302
319
. await ?;
303
320
304
321
let mut instance_builder = factors_app. prepare ( "empty" ) ?;
@@ -321,7 +338,7 @@ mod tests {
321
338
#[ async_trait]
322
339
impl ComponentLoader for DummyComponentLoader {
323
340
async fn load_component (
324
- & mut self ,
341
+ & self ,
325
342
engine : & spin_core:: wasmtime:: Engine ,
326
343
_component : & AppComponent ,
327
344
) -> anyhow:: Result < Component > {
0 commit comments