@@ -4,6 +4,11 @@ use crate::Result;
44// use alloc::boxed::Box;
55pub ( crate ) use tinywasm_types:: { ResumeArgument , YieldedValue } ;
66
7+ ///"coroutine statse", "coroutine instance", "resumable". Stores info to continue a function that was paused
8+ pub trait CoroState < Ret , ResumeContext > : Debug {
9+ /// resumes the execution of the coroutine
10+ fn resume ( & mut self , ctx : ResumeContext , arg : ResumeArgument ) -> Result < CoroStateResumeResult < Ret > > ;
11+ }
712
813/// explains why did execution suspend, and carries payload if needed
914#[ derive( Debug ) ]
@@ -24,7 +29,6 @@ pub enum SuspendReason {
2429 /// async should_suspend flag was set
2530 /// host shouldn't provide resume argument when calling resume
2631 SuspendedFlag ,
27-
2832 // possible others: delimited continuations proposal, debugger breakpoint, out of fuel
2933}
3034
@@ -55,13 +59,19 @@ pub enum CoroStateResumeResult<R> {
5559}
5660
5761impl < R , State > PotentialCoroCallResult < R , State > {
62+
63+ /// in case you expect function only to return
64+ /// you can make Suspend into [crate::Error::UnexpectedSuspend] error
65+ pub fn suspend_to_err ( self ) -> Result < R > {
66+ match self {
67+ PotentialCoroCallResult :: Return ( r) => Ok ( r) ,
68+ PotentialCoroCallResult :: Suspended ( r, _) => Err ( crate :: Error :: UnexpectedSuspend ( r) ) ,
69+ }
70+ }
71+
5872 /// true if coro is finished
5973 pub fn finished ( & self ) -> bool {
60- if let Self :: Return ( _) = self {
61- true
62- } else {
63- false
64- }
74+ matches ! ( self , Self :: Return ( _) )
6575 }
6676 /// separates state from PotentialCoroCallResult, leaving CoroStateResumeResult (one without state)
6777 pub fn split_state ( self ) -> ( CoroStateResumeResult < R > , Option < State > ) {
@@ -90,7 +100,7 @@ impl<R, State> PotentialCoroCallResult<R, State> {
90100 self ,
91101 user_val : Usr ,
92102 mapper : impl FnOnce ( R , Usr ) -> OutR ,
93- otherwise : impl FnOnce ( Usr ) -> ( ) ,
103+ otherwise : impl FnOnce ( Usr ) ,
94104 ) -> PotentialCoroCallResult < OutR , State > {
95105 match self {
96106 Self :: Return ( res) => PotentialCoroCallResult :: Return ( mapper ( res, user_val) ) ,
@@ -109,11 +119,7 @@ impl<R, State> PotentialCoroCallResult<R, State> {
109119impl < R > CoroStateResumeResult < R > {
110120 /// true if coro is finished
111121 pub fn finished ( & self ) -> bool {
112- if let Self :: Return ( _) = self {
113- true
114- } else {
115- false
116- }
122+ matches ! ( self , Self :: Return ( _) )
117123 }
118124 /// separates result from CoroStateResumeResult, leaving unit type in it's place
119125 pub fn split_result ( self ) -> ( CoroStateResumeResult < ( ) > , Option < R > ) {
@@ -129,7 +135,7 @@ impl<R> CoroStateResumeResult<R> {
129135 self ,
130136 user_val : Usr ,
131137 mapper : impl FnOnce ( R , Usr ) -> OutR ,
132- otherwise : impl FnOnce ( Usr ) -> ( ) ,
138+ otherwise : impl FnOnce ( Usr ) ,
133139 ) -> CoroStateResumeResult < OutR > {
134140 PotentialCoroCallResult :: < R , ( ) > :: from ( self ) . map_result_or_else ( user_val, mapper, otherwise) . into ( )
135141 }
@@ -154,9 +160,3 @@ impl<SrcR> From<CoroStateResumeResult<SrcR>> for PotentialCoroCallResult<SrcR, (
154160 }
155161 }
156162}
157-
158- ///"coroutine statse", "coroutine instance", "resumable". Stores info to continue a function that was paused
159- pub trait CoroState < Ret , ResumeContext > : Debug {
160- /// resumes the execution of the coroutine
161- fn resume ( & mut self , ctx : ResumeContext , arg : ResumeArgument ) -> Result < CoroStateResumeResult < Ret > > ;
162- }
0 commit comments