66// - async.poll: Poll an async value for completion
77// - async.wait: Wait for an async value to complete
88
9+ #[ cfg( all( feature = "component-model-async" , not( feature = "std" ) ) ) ]
10+ use alloc:: {
11+ boxed:: Box ,
12+ vec:: Vec ,
13+ } ;
914#[ cfg( all( feature = "component-model-async" , feature = "std" ) ) ]
1015use std:: {
1116 boxed:: Box ,
1217 collections:: HashMap ,
13- sync:: { Arc , Mutex } ,
18+ sync:: {
19+ Arc ,
20+ Mutex ,
21+ } ,
1422 vec:: Vec ,
1523} ;
1624
17- #[ cfg( all( feature = "component-model-async" , not( feature = "std" ) ) ) ]
18- use alloc:: { boxed:: Box , vec:: Vec } ;
19-
20- #[ cfg( all( feature = "component-model-async" , not( feature = "std" ) ) ) ]
21- use wrt_foundation:: { bounded:: BoundedVec , safe_memory:: NoStdProvider } ;
22-
2325#[ cfg( feature = "component-model-async" ) ]
24- use wrt_error:: { Error , Result } ;
25-
26+ use wrt_error:: {
27+ Error ,
28+ Result ,
29+ } ;
30+ #[ cfg( all( feature = "component-model-async" , not( feature = "std" ) ) ) ]
31+ use wrt_foundation:: {
32+ bounded:: BoundedVec ,
33+ safe_memory:: NoStdProvider ,
34+ } ;
2635#[ cfg( all( feature = "component-model-async" , feature = "std" ) ) ]
27- use wrt_foundation:: { builtin:: BuiltinType , component_value:: ComponentValue } ;
36+ use wrt_foundation:: {
37+ builtin:: BuiltinType ,
38+ component_value:: ComponentValue ,
39+ } ;
2840
2941#[ cfg( all( feature = "component-model-async" , not( feature = "std" ) ) ) ]
3042use crate :: types:: Value as ComponentValue ;
@@ -57,7 +69,7 @@ pub enum AsyncStatus {
5769/// Storage for async value information
5870pub struct AsyncValueStore {
5971 /// Map of async IDs to their values
60- values : HashMap < u32 , AsyncValue > ,
72+ values : HashMap < u32 , AsyncValue > ,
6173 /// Next available async ID
6274 next_id : u32 ,
6375}
@@ -70,15 +82,15 @@ pub struct AsyncValue {
7082 /// Result value (if available)
7183 result : Option < Vec < ComponentValue > > ,
7284 /// Error message (if failed)
73- error : Option < String > ,
85+ error : Option < String > ,
7486}
7587
7688#[ cfg( feature = "component-model-async" ) ]
7789impl AsyncValueStore {
7890 /// Create a new async value store
7991 pub fn new ( ) -> Self {
8092 Self {
81- values : HashMap :: new ( ) ,
93+ values : HashMap :: new ( ) ,
8294 next_id : 1 , // Start at 1, 0 is reserved
8395 }
8496 }
@@ -93,7 +105,14 @@ impl AsyncValueStore {
93105 /// Create a new async value with the given status
94106 pub fn create_async ( & mut self , status : AsyncStatus ) -> u32 {
95107 let id = self . generate_id ( ) ;
96- self . values . insert ( id, AsyncValue { status, result : None , error : None } ) ;
108+ self . values . insert (
109+ id,
110+ AsyncValue {
111+ status,
112+ result : None ,
113+ error : None ,
114+ } ,
115+ ) ;
97116 id
98117 }
99118
@@ -105,7 +124,7 @@ impl AsyncValueStore {
105124 async_value. result = Some ( result) ;
106125
107126 Ok ( ( ) )
108- }
127+ } ,
109128 None => Err ( Error :: component_not_found ( "Error occurred" ) ) ,
110129 }
111130 }
@@ -118,7 +137,7 @@ impl AsyncValueStore {
118137 async_value. error = Some ( error) ;
119138
120139 Ok ( ( ) )
121- }
140+ } ,
122141 None => Err ( Error :: component_not_found ( "Error occurred" ) ) ,
123142 }
124143 }
@@ -136,9 +155,7 @@ impl AsyncValueStore {
136155 match self . values . get ( & id) {
137156 Some ( async_value) => {
138157 if async_value. status == AsyncStatus :: Ready {
139- async_value. result . clone ( ) . ok_or_else ( || {
140- Error :: async_error ( "Error occurred" )
141- } )
158+ async_value. result . clone ( ) . ok_or_else ( || Error :: async_error ( "Error occurred" ) )
142159 } else if async_value. status == AsyncStatus :: Failed {
143160 Err ( Error :: async_error (
144161 & async_value
@@ -149,7 +166,7 @@ impl AsyncValueStore {
149166 } else {
150167 Err ( Error :: async_error ( "Error occurred" ) )
151168 }
152- }
169+ } ,
153170 None => Err ( Error :: component_not_found ( "Error occurred" ) ) ,
154171 }
155172 }
@@ -207,7 +224,9 @@ impl BuiltinHandler for AsyncNewHandler {
207224 }
208225
209226 fn clone_handler ( & self ) -> Box < dyn BuiltinHandler > {
210- Box :: new ( Self { async_store : self . async_store . clone ( ) } )
227+ Box :: new ( Self {
228+ async_store : self . async_store . clone ( ) ,
229+ } )
211230 }
212231}
213232
@@ -243,7 +262,7 @@ impl BuiltinHandler for AsyncGetHandler {
243262 ComponentValue :: U32 ( id) => * id,
244263 _ => {
245264 return Err ( Error :: runtime_execution_error ( "Error occurred" ) ) ;
246- }
265+ } ,
247266 } ;
248267
249268 // Get the result of the async computation
@@ -252,7 +271,9 @@ impl BuiltinHandler for AsyncGetHandler {
252271 }
253272
254273 fn clone_handler ( & self ) -> Box < dyn BuiltinHandler > {
255- Box :: new ( Self { async_store : self . async_store . clone ( ) } )
274+ Box :: new ( Self {
275+ async_store : self . async_store . clone ( ) ,
276+ } )
256277 }
257278}
258279
@@ -288,7 +309,7 @@ impl BuiltinHandler for AsyncPollHandler {
288309 ComponentValue :: U32 ( id) => * id,
289310 _ => {
290311 return Err ( Error :: runtime_execution_error ( "Error occurred" ) ) ;
291- }
312+ } ,
292313 } ;
293314
294315 // Check the status of the async computation
@@ -304,7 +325,9 @@ impl BuiltinHandler for AsyncPollHandler {
304325 }
305326
306327 fn clone_handler ( & self ) -> Box < dyn BuiltinHandler > {
307- Box :: new ( Self { async_store : self . async_store . clone ( ) } )
328+ Box :: new ( Self {
329+ async_store : self . async_store . clone ( ) ,
330+ } )
308331 }
309332}
310333
@@ -343,7 +366,7 @@ impl BuiltinHandler for AsyncWaitHandler {
343366 ComponentValue :: U32 ( id) => * id,
344367 _ => {
345368 return Err ( Error :: runtime_execution_error ( "Error occurred" ) ) ;
346- }
369+ } ,
347370 } ;
348371
349372 // Use Component Model polling instead of Rust futures
@@ -353,10 +376,10 @@ impl BuiltinHandler for AsyncWaitHandler {
353376 match store. get_status ( async_id) {
354377 Ok ( AsyncStatus :: Ready ) => {
355378 return store. get_result ( async_id) ;
356- }
379+ } ,
357380 Ok ( AsyncStatus :: Failed ) => {
358381 return store. get_result ( async_id) ; // Will return the error
359- }
382+ } ,
360383 Ok ( AsyncStatus :: Pending ) => {
361384 // Drop the lock and yield/sleep briefly
362385 drop ( store) ;
@@ -366,14 +389,16 @@ impl BuiltinHandler for AsyncWaitHandler {
366389
367390 // Continue polling
368391 continue ;
369- }
392+ } ,
370393 Err ( e) => return Err ( e) ,
371394 }
372395 }
373396 }
374397
375398 fn clone_handler ( & self ) -> Box < dyn BuiltinHandler > {
376- Box :: new ( Self { async_store : self . async_store . clone ( ) } )
399+ Box :: new ( Self {
400+ async_store : self . async_store . clone ( ) ,
401+ } )
377402 }
378403}
379404
@@ -444,7 +469,7 @@ mod tests {
444469 // Verify the async value was created
445470 let async_store = store. lock ( ) . unwrap ( ) ;
446471 assert ! ( async_store. has_async( * id) ) ;
447- }
472+ } ,
448473 _ => panic ! ( "Expected U32 result" ) ,
449474 }
450475
@@ -477,7 +502,7 @@ mod tests {
477502 match & result[ 0 ] {
478503 ComponentValue :: U32 ( value) => {
479504 assert_eq ! ( * value, 42 ) ;
480- }
505+ } ,
481506 _ => panic ! ( "Expected U32 result" ) ,
482507 }
483508 }
@@ -543,7 +568,7 @@ mod tests {
543568 match & result[ 0 ] {
544569 ComponentValue :: U32 ( value) => {
545570 assert_eq ! ( * value, 42 ) ;
546- }
571+ } ,
547572 _ => panic ! ( "Expected U32 result" ) ,
548573 }
549574 }
0 commit comments