|
| 1 | +use core::fmt::Debug; |
| 2 | + |
| 3 | +use crate::Result; |
| 4 | +// use alloc::boxed::Box; |
| 5 | +pub(crate) use tinywasm_types::{ResumeArgument, YieldedValue}; |
| 6 | + |
| 7 | +#[derive(Debug)] |
| 8 | +pub enum SuspendReason { |
| 9 | + /// host function yielded |
| 10 | + /// potentially some host functions might expect resume argument when calling resume |
| 11 | + Yield(YieldedValue), |
| 12 | + // /// timer ran out (not implemented), |
| 13 | + // /// host shouldn't provide resume argument when calling resume |
| 14 | + // SuspendedEpoch, |
| 15 | + |
| 16 | + // /// async should_suspend flag was set (not implemented) |
| 17 | + // /// host shouldn't provide resume argument when calling resume |
| 18 | + // SuspendedFlag, |
| 19 | +} |
| 20 | + |
| 21 | +/// result of a function that might pause in the middle and yield |
| 22 | +/// to be resumed later |
| 23 | +#[derive(Debug)] |
| 24 | +pub enum PotentialCoroCallResult<R, State> |
| 25 | +//where for<Ctx> |
| 26 | +// State: CoroState<R, Ctx>, // can't in stable rust |
| 27 | +{ |
| 28 | + /// function returns normally |
| 29 | + Return(R), |
| 30 | + /// interpreter will be suspended and execution will return to host along with SuspendReason |
| 31 | + Suspended(SuspendReason, State), |
| 32 | +} |
| 33 | + |
| 34 | +/// result of resuming coroutine state. Unlike [`PotentialCoroCallResult`] |
| 35 | +/// doesn't need to have state, since it's contained in self |
| 36 | +#[derive(Debug)] |
| 37 | +pub enum CoroStateResumeResult<R> { |
| 38 | + /// after this CoroState::resume can't be called again on that CoroState |
| 39 | + Return(R), |
| 40 | + |
| 41 | + /// host function yielded |
| 42 | + /// execution returns to host along with yielded value |
| 43 | + Suspended(SuspendReason), |
| 44 | +} |
| 45 | + |
| 46 | +impl<R, State> PotentialCoroCallResult<R, State> { |
| 47 | + /// true if coro is finished |
| 48 | + pub fn finished(&self) -> bool { |
| 49 | + if let Self::Return(_) = self { |
| 50 | + true |
| 51 | + } else { |
| 52 | + false |
| 53 | + } |
| 54 | + } |
| 55 | + /// separates state from PotentialCoroCallResult, leaving CoroStateResumeResult (one without state) |
| 56 | + pub fn split_state(self) -> (CoroStateResumeResult<R>, Option<State>) { |
| 57 | + match self { |
| 58 | + Self::Return(val) => (CoroStateResumeResult::Return(val), None), |
| 59 | + Self::Suspended(suspend, state) => (CoroStateResumeResult::Suspended(suspend), Some(state)), |
| 60 | + } |
| 61 | + } |
| 62 | + /// separates result from PotentialCoroCallResult, leaving unit type in it's place |
| 63 | + pub fn split_result(self) -> (PotentialCoroCallResult<(), State>, Option<R>) { |
| 64 | + match self { |
| 65 | + Self::Return(result) => (PotentialCoroCallResult::Return(()), Some(result)), |
| 66 | + Self::Suspended(suspend, state) => (PotentialCoroCallResult::Suspended(suspend, state), None), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// transforms state |
| 71 | + pub fn map_state<OutS>(self, mapper: impl FnOnce(State) -> OutS) -> PotentialCoroCallResult<R, OutS> { |
| 72 | + match self { |
| 73 | + Self::Return(val) => PotentialCoroCallResult::Return(val), |
| 74 | + Self::Suspended(suspend, state) => PotentialCoroCallResult::Suspended(suspend, mapper(state)), |
| 75 | + } |
| 76 | + } |
| 77 | + /// transform result with mapper if there is none - calls "otherwise" arg. user_val |
| 78 | + pub fn map_result_or_else<OutR, Usr>( |
| 79 | + self, |
| 80 | + user_val: Usr, |
| 81 | + mapper: impl FnOnce(R, Usr) -> OutR, |
| 82 | + otherwise: impl FnOnce(Usr) -> (), |
| 83 | + ) -> PotentialCoroCallResult<OutR, State> { |
| 84 | + match self { |
| 85 | + Self::Return(res) => PotentialCoroCallResult::Return(mapper(res, user_val)), |
| 86 | + Self::Suspended(suspend, state) => { |
| 87 | + otherwise(user_val); |
| 88 | + PotentialCoroCallResult::Suspended(suspend, state) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + /// transforms result |
| 93 | + pub fn map_result<OutR>(self, mapper: impl FnOnce(R) -> OutR) -> PotentialCoroCallResult<OutR, State> { |
| 94 | + self.map_result_or_else((), |val, _| mapper(val), |_| {}) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<R> CoroStateResumeResult<R> { |
| 99 | + /// true if coro is finished |
| 100 | + pub fn finished(&self) -> bool { |
| 101 | + if let Self::Return(_) = self { |
| 102 | + true |
| 103 | + } else { |
| 104 | + false |
| 105 | + } |
| 106 | + } |
| 107 | + /// separates result from CoroStateResumeResult, leaving unit type in it's place |
| 108 | + pub fn split_result(self) -> (CoroStateResumeResult<()>, Option<R>) { |
| 109 | + let (a, r) = PotentialCoroCallResult::<R, ()>::from(self).split_result(); |
| 110 | + (a.into(), r) |
| 111 | + } |
| 112 | + /// transforms result |
| 113 | + pub fn map_result<OutR>(self, mapper: impl FnOnce(R) -> OutR) -> CoroStateResumeResult<OutR> { |
| 114 | + PotentialCoroCallResult::<R, ()>::from(self).map_result(mapper).into() |
| 115 | + } |
| 116 | + /// transform result with mapper if there is none - calls "otherwise" arg. user_val called |
| 117 | + pub fn map_result_or_else<OutR, Usr>( |
| 118 | + self, |
| 119 | + user_val: Usr, |
| 120 | + mapper: impl FnOnce(R, Usr) -> OutR, |
| 121 | + otherwise: impl FnOnce(Usr) -> (), |
| 122 | + ) -> CoroStateResumeResult<OutR> { |
| 123 | + PotentialCoroCallResult::<R, ()>::from(self).map_result_or_else(user_val, mapper, otherwise).into() |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<DstR, SrcR> From<PotentialCoroCallResult<SrcR, ()>> for CoroStateResumeResult<DstR> |
| 128 | +where |
| 129 | + DstR: From<SrcR>, |
| 130 | +{ |
| 131 | + fn from(value: PotentialCoroCallResult<SrcR, ()>) -> Self { |
| 132 | + match value { |
| 133 | + PotentialCoroCallResult::Return(val) => Self::Return(val.into()), |
| 134 | + PotentialCoroCallResult::Suspended(suspend, ()) => Self::Suspended(suspend), |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | +impl<SrcR> From<CoroStateResumeResult<SrcR>> for PotentialCoroCallResult<SrcR, ()> { |
| 139 | + fn from(value: CoroStateResumeResult<SrcR>) -> Self { |
| 140 | + match value { |
| 141 | + CoroStateResumeResult::Return(val) => PotentialCoroCallResult::Return(val), |
| 142 | + CoroStateResumeResult::Suspended(suspend) => PotentialCoroCallResult::Suspended(suspend, ()), |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +///"coroutine statse", "coroutine instance", "resumable". Stores info to continue a function that was paused |
| 148 | +pub trait CoroState<Ret, ResumeContext>: Debug { |
| 149 | + /// resumes the execution of the coroutine |
| 150 | + fn resume(&mut self, ctx: ResumeContext, arg: ResumeArgument) -> Result<CoroStateResumeResult<Ret>>; |
| 151 | +} |
0 commit comments