Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit b73ab18

Browse files
committed
codestyle, relax bounds on YieldedValue/ResumeArgument
1 parent ff933a2 commit b73ab18

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

crates/tinywasm/src/coro.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ pub trait CoroState<Ret, ResumeContext>: Debug {
1515
#[non_exhaustive] // some variants are feature-gated
1616
pub 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+
}

crates/tinywasm/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tinywasm_types::FuncType;
66
#[cfg(feature = "parser")]
77
pub use tinywasm_parser::ParseError;
88

9-
use crate::{coro::SuspendReason, interpreter};
9+
use crate::{coro::UnexpectedSuspendError, interpreter};
1010

1111
/// Errors that can occur for `TinyWasm` operations
1212
#[derive(Debug)]
@@ -45,7 +45,7 @@ pub enum Error {
4545

4646
/// Function unexpectedly yielded instead of returning
4747
/// (for backwards compatibility with old api)
48-
UnexpectedSuspend(SuspendReason),
48+
UnexpectedSuspend(UnexpectedSuspendError),
4949

5050
#[cfg(feature = "std")]
5151
/// An I/O error occurred

crates/types/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
extern crate alloc;
1212
use alloc::boxed::Box;
13-
use core::{fmt::Debug, ops::Range, any::Any};
13+
use core::{any::Any, fmt::Debug, ops::Range};
1414

1515
// Memory defaults
1616
const MEM_PAGE_SIZE: u64 = 65536;
@@ -409,9 +409,5 @@ pub enum ElementItem {
409409
Expr(ConstInstruction),
410410
}
411411

412-
// concider adding lifetime
413-
// yielded value might benefit from referencing something in
414-
// suspended host coro, and resume argument - from down the callstack
415-
// however, that would make executor structure more difficult
416-
pub type YieldedValue = Option<Box<dyn Any + Send + Sync>>;
417-
pub type ResumeArgument = Option<Box<dyn Any + Send + Sync>>;
412+
pub type YieldedValue = Option<Box<dyn Any>>;
413+
pub type ResumeArgument = Option<Box<dyn Any>>;

examples/host_coro.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ fn main() -> eyre::Result<()> {
5656
vals: &[WasmValue]|
5757
-> tinywasm::Result<PotentialCoroCallResult<Vec<WasmValue>, Box<dyn HostCoroState>>> {
5858
let base = if let WasmValue::I32(v) = vals.first().expect("wrong args") { v } else { panic!("wrong arg") };
59-
let val_to_yield = Box::new(MyUserData { magic: 42 });
6059
let coro = Box::new(MySuspendedState { base: *base });
61-
return Ok(PotentialCoroCallResult::Suspended(SuspendReason::Yield(Some(val_to_yield)), coro));
60+
return Ok(PotentialCoroCallResult::Suspended(
61+
SuspendReason::make_yield::<MyUserData>(MyUserData { magic: 42 }),
62+
coro,
63+
));
6264
};
6365
imports.define(
6466
"host",

0 commit comments

Comments
 (0)