@@ -13,28 +13,19 @@ use serde::de::DeserializeOwned;
13
13
14
14
use spin_app:: { App , AppComponent , AppLoader , AppTrigger , Loader , OwnedApp , APP_NAME_KEY } ;
15
15
use spin_core:: {
16
- Config , Engine , EngineBuilder , Instance , InstancePre , ModuleInstance , ModuleInstancePre ,
17
- OutboundWasiHttpHandler , Store , StoreBuilder , WasiVersion ,
16
+ Config , Engine , EngineBuilder , Instance , InstancePre , OutboundWasiHttpHandler , Store ,
17
+ StoreBuilder , WasiVersion ,
18
18
} ;
19
19
20
20
pub use crate :: runtime_config:: RuntimeConfig ;
21
21
22
- pub enum EitherInstancePre < T > {
23
- Component ( InstancePre < T > ) ,
24
- Module ( ModuleInstancePre < T > ) ,
25
- }
26
-
27
- pub enum EitherInstance {
28
- Component ( Instance ) ,
29
- Module ( ModuleInstance ) ,
30
- }
31
-
32
22
#[ async_trait]
33
23
pub trait TriggerExecutor : Sized + Send + Sync {
34
24
const TRIGGER_TYPE : & ' static str ;
35
25
type RuntimeData : OutboundWasiHttpHandler + Default + Send + Sync + ' static ;
36
26
type TriggerConfig ;
37
27
type RunConfig ;
28
+ type InstancePre : TriggerInstancePre < Self :: RuntimeData , Self :: TriggerConfig > ;
38
29
39
30
/// Create a new trigger executor.
40
31
async fn new ( engine : TriggerAppEngine < Self > ) -> Result < Self > ;
@@ -46,18 +37,50 @@ pub trait TriggerExecutor: Sized + Send + Sync {
46
37
fn configure_engine ( _builder : & mut EngineBuilder < Self :: RuntimeData > ) -> Result < ( ) > {
47
38
Ok ( ( ) )
48
39
}
40
+ }
41
+
42
+ /// Helper type alias to project the `Instance` of a given `TriggerExecutor`.
43
+ pub type ExecutorInstance < T > = <<T as TriggerExecutor >:: InstancePre as TriggerInstancePre <
44
+ <T as TriggerExecutor >:: RuntimeData ,
45
+ <T as TriggerExecutor >:: TriggerConfig ,
46
+ > >:: Instance ;
47
+
48
+ #[ async_trait]
49
+ pub trait TriggerInstancePre < T , C > : Sized + Send + Sync
50
+ where
51
+ T : OutboundWasiHttpHandler + Send + Sync ,
52
+ {
53
+ type Instance ;
49
54
50
55
async fn instantiate_pre (
51
- engine : & Engine < Self :: RuntimeData > ,
56
+ engine : & Engine < T > ,
52
57
component : & AppComponent ,
53
- _config : & Self :: TriggerConfig ,
54
- ) -> Result < EitherInstancePre < Self :: RuntimeData > > {
58
+ config : & C ,
59
+ ) -> Result < Self > ;
60
+
61
+ async fn instantiate ( & self , store : & mut Store < T > ) -> Result < Self :: Instance > ;
62
+ }
63
+
64
+ #[ async_trait]
65
+ impl < T , C > TriggerInstancePre < T , C > for InstancePre < T >
66
+ where
67
+ T : OutboundWasiHttpHandler + Send + Sync ,
68
+ {
69
+ type Instance = Instance ;
70
+
71
+ async fn instantiate_pre (
72
+ engine : & Engine < T > ,
73
+ component : & AppComponent ,
74
+ _config : & C ,
75
+ ) -> Result < Self > {
55
76
let comp = component. load_component ( engine) . await ?;
56
- Ok ( EitherInstancePre :: Component (
57
- engine
58
- . instantiate_pre ( & comp)
59
- . with_context ( || format ! ( "Failed to instantiate component '{}'" , component. id( ) ) ) ?,
60
- ) )
77
+ Ok ( engine
78
+ . instantiate_pre ( & comp)
79
+ . with_context ( || format ! ( "Failed to instantiate component '{}'" , component. id( ) ) ) ?)
80
+ }
81
+
82
+ async fn instantiate ( & self , store : & mut Store < T > ) -> Result < Self :: Instance > {
83
+ self . instantiate_async ( store) . await
61
84
}
62
85
}
63
86
@@ -246,7 +269,7 @@ pub struct TriggerAppEngine<Executor: TriggerExecutor> {
246
269
// Trigger configs for this trigger type, with order matching `app.triggers_with_type(Executor::TRIGGER_TYPE)`
247
270
trigger_configs : Vec < Executor :: TriggerConfig > ,
248
271
// Map of {Component ID -> InstancePre} for each component.
249
- component_instance_pres : HashMap < String , EitherInstancePre < Executor :: RuntimeData > > ,
272
+ component_instance_pres : HashMap < String , Executor :: InstancePre > ,
250
273
// Resolver for value template expressions
251
274
resolver : std:: sync:: Arc < spin_expressions:: PreparedResolver > ,
252
275
}
@@ -290,7 +313,7 @@ impl<Executor: TriggerExecutor> TriggerAppEngine<Executor> {
290
313
if let Some ( config) = trigger_config {
291
314
component_instance_pres. insert (
292
315
id. to_owned ( ) ,
293
- Executor :: instantiate_pre ( & engine, & component, config)
316
+ Executor :: InstancePre :: instantiate_pre ( & engine, & component, config)
294
317
. await
295
318
. with_context ( || format ! ( "Failed to instantiate component '{id}'" ) ) ?,
296
319
) ;
@@ -348,7 +371,7 @@ impl<Executor: TriggerExecutor> TriggerAppEngine<Executor> {
348
371
pub async fn prepare_instance (
349
372
& self ,
350
373
component_id : & str ,
351
- ) -> Result < ( EitherInstance , Store < Executor :: RuntimeData > ) > {
374
+ ) -> Result < ( ExecutorInstance < Executor > , Store < Executor :: RuntimeData > ) > {
352
375
let store_builder = self . store_builder ( component_id, WasiVersion :: Preview2 ) ?;
353
376
self . prepare_instance_with_store ( component_id, store_builder)
354
377
. await
@@ -359,7 +382,7 @@ impl<Executor: TriggerExecutor> TriggerAppEngine<Executor> {
359
382
& self ,
360
383
component_id : & str ,
361
384
mut store_builder : StoreBuilder ,
362
- ) -> Result < ( EitherInstance , Store < Executor :: RuntimeData > ) > {
385
+ ) -> Result < ( ExecutorInstance < Executor > , Store < Executor :: RuntimeData > ) > {
363
386
let component = self . get_component ( component_id) ?;
364
387
365
388
// Build Store
@@ -372,18 +395,7 @@ impl<Executor: TriggerExecutor> TriggerAppEngine<Executor> {
372
395
. get ( component_id)
373
396
. expect ( "component_instance_pres missing valid component_id" ) ;
374
397
375
- let instance = match pre {
376
- EitherInstancePre :: Component ( pre) => pre
377
- . instantiate_async ( & mut store)
378
- . await
379
- . map ( EitherInstance :: Component ) ,
380
-
381
- EitherInstancePre :: Module ( pre) => pre
382
- . instantiate_async ( & mut store)
383
- . await
384
- . map ( EitherInstance :: Module ) ,
385
- }
386
- . with_context ( || {
398
+ let instance = pre. instantiate ( & mut store) . await . with_context ( || {
387
399
format ! (
388
400
"app {:?} component {:?} instantiation failed" ,
389
401
self . app_name, component_id
0 commit comments