Code patterns for working with async in cross-platform wasm & native context

⚓ rust    📅 2025-06-10    👤 surdeus    👁️ 2      

surdeus

Hi, i'm currently writing a library that's supposed to work both in a single-threaded wasm context and in native context (a library then bound by uniffi and wasm-bindgen). The main API of the library is exposing async functions.

Because wasmbindgen websys code often uses types that are not Send (like JsValue), this contaminates all my code with strictly different requirements : the native code bound by uniffi NEEDS Send trait for everything (especially future returned by async function) and the wasm code needs NOT Send.

i've started duplicating the main traits to have different constrained then use a #[cfg(wasm)] to compile a version with one constraint or the other, but that quickly becomes unmanageable.

What are the useful patterns for working in that kind of context and prevent code duplication ?

1 post - 1 participant

Read full topic

🏷️ rust_feed