ic_rig/wasm_compat.rs
1//! WASM compatibility shims.
2//!
3//! On native targets, async code requires `Send` bounds to work across threads.
4//! On WASM (single-threaded), those bounds are unnecessary and will prevent
5//! compilation. These traits alias to the appropriate bound per target.
6//!
7//! Always use [`WasmCompatSend`] and [`WasmCompatSync`] instead of raw `Send`/`Sync`
8//! in trait bounds throughout this crate.
9
10use std::{future::Future, pin::Pin};
11
12// ── WasmCompatSend ────────────────────────────────────────────────────────────
13
14#[cfg(not(target_family = "wasm"))]
15pub trait WasmCompatSend: Send {}
16#[cfg(not(target_family = "wasm"))]
17impl<T: Send> WasmCompatSend for T {}
18
19#[cfg(target_family = "wasm")]
20pub trait WasmCompatSend {}
21#[cfg(target_family = "wasm")]
22impl<T> WasmCompatSend for T {}
23
24// ── WasmCompatSync ────────────────────────────────────────────────────────────
25
26#[cfg(not(target_family = "wasm"))]
27pub trait WasmCompatSync: Sync {}
28#[cfg(not(target_family = "wasm"))]
29impl<T: Sync> WasmCompatSync for T {}
30
31#[cfg(target_family = "wasm")]
32pub trait WasmCompatSync {}
33#[cfg(target_family = "wasm")]
34impl<T> WasmCompatSync for T {}
35
36// ── BoxFuture ─────────────────────────────────────────────────────────────────
37
38/// A heap-allocated, type-erased future.
39///
40/// Intentionally non-`Send` on all targets. This keeps the library usable on
41/// single-threaded runtimes (ICP, WASM) without pulling in extra constraints.
42/// If you need a `Send` future on native, wrap at the call site.
43pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;