@@ -15,15 +15,15 @@ pub trait CoroState<Ret, ResumeContext>: Debug {
1515#[ non_exhaustive] // some variants are feature-gated
1616pub enum SuspendReason {
1717 /// host function yielded
18- /// potentially some host functions might expect resume argument when calling resume
18+ /// some host functions might expect resume argument when calling resume
1919 Yield ( YieldedValue ) ,
2020
2121 /// time to suspend has come,
2222 /// host shouldn't provide resume argument when calling resume
2323 #[ cfg( feature = "std" ) ]
2424 SuspendedEpoch ,
2525
26- /// user's should-suspend-callback,
26+ /// user's should-suspend-callback returned Break ,
2727 /// host shouldn't provide resume argument when calling resume
2828 SuspendedCallback ,
2929
@@ -65,7 +65,7 @@ impl<R, State> PotentialCoroCallResult<R, State> {
6565 pub fn suspend_to_err ( self ) -> Result < R > {
6666 match self {
6767 PotentialCoroCallResult :: Return ( r) => Ok ( r) ,
68- PotentialCoroCallResult :: Suspended ( r, _) => Err ( crate :: Error :: UnexpectedSuspend ( r) ) ,
68+ PotentialCoroCallResult :: Suspended ( r, _) => Err ( crate :: Error :: UnexpectedSuspend ( r. into ( ) ) ) ,
6969 }
7070 }
7171
@@ -112,7 +112,7 @@ impl<R, State> PotentialCoroCallResult<R, State> {
112112 }
113113 /// transforms result
114114 pub fn map_result < OutR > ( self , mapper : impl FnOnce ( R ) -> OutR ) -> PotentialCoroCallResult < OutR , State > {
115- self . map ( ( ) , |val, _| mapper ( val) , |s, _| { s } )
115+ self . map ( ( ) , |val, _| mapper ( val) , |s, _| s )
116116 }
117117}
118118
@@ -138,7 +138,7 @@ impl<R> CoroStateResumeResult<R> {
138138 mapper : impl FnOnce ( R , Usr ) -> OutR ,
139139 otherwise : impl FnOnce ( Usr ) ,
140140 ) -> CoroStateResumeResult < OutR > {
141- PotentialCoroCallResult :: < R , ( ) > :: from ( self ) . map ( user_val, mapper, |( ) , usr|{ otherwise ( usr) } ) . into ( )
141+ PotentialCoroCallResult :: < R , ( ) > :: from ( self ) . map ( user_val, mapper, |( ) , usr| otherwise ( usr) ) . into ( )
142142 }
143143}
144144
@@ -161,3 +161,39 @@ impl<SrcR> From<CoroStateResumeResult<SrcR>> for PotentialCoroCallResult<SrcR, (
161161 }
162162 }
163163}
164+
165+ impl SuspendReason {
166+ /// shotrhand to package val into a Box<any> in a [SuspendReason::Yield] variant
167+ /// you'll need to specify type explicitly, because you'll need to use exact same type when downcasting
168+ pub fn make_yield < T > ( val : impl Into < T > + core:: any:: Any ) -> Self {
169+ Self :: Yield ( Some ( alloc:: boxed:: Box :: new ( val) as alloc:: boxed:: Box < dyn core:: any:: Any > ) )
170+ }
171+ }
172+
173+ // same as SuspendReason, but without [tinywasm_types::YieldedValue]
174+ // it's opaque anyway, and error has Send and Sync which aren't typically needed for yielded value
175+ #[ derive( Debug ) ]
176+ pub enum UnexpectedSuspendError {
177+ /// host function yielded
178+ Yield ,
179+
180+ /// timeout,
181+ #[ cfg( feature = "std" ) ]
182+ SuspendedEpoch ,
183+
184+ /// user's should-suspend-callback returned Break,
185+ SuspendedCallback ,
186+
187+ /// async should_suspend flag was set
188+ SuspendedFlag ,
189+ }
190+ impl From < SuspendReason > for UnexpectedSuspendError {
191+ fn from ( value : SuspendReason ) -> Self {
192+ match value {
193+ SuspendReason :: Yield ( _) => Self :: Yield ,
194+ SuspendReason :: SuspendedEpoch => Self :: SuspendedEpoch ,
195+ SuspendReason :: SuspendedCallback => Self :: SuspendedCallback ,
196+ SuspendReason :: SuspendedFlag => Self :: SuspendedFlag ,
197+ }
198+ }
199+ }
0 commit comments