ed_agent/maybe.rs
1//! Thread-safety bounds that relax on browser wasm.
2//!
3//! The host-facing seams ([`ToolExecutor`](crate::ToolExecutor),
4//! [`ApprovalHandler`](crate::ApprovalHandler), [`EventSink`](crate::EventSink),
5//! [`SessionStore`](crate::SessionStore)) must be `Send + Sync` on native
6//! targets, where a turn can hop between runtime threads. In a browser the
7//! agent lives on one JS thread and its implementations hold `JsValue`s, which
8//! are `!Send`. These aliases resolve to exactly `Send` / `Sync` everywhere
9//! except `wasm32-unknown-unknown`, so native signatures are unchanged. Other
10//! wasm targets (`wasm32-wasip1-threads`, Emscripten with pthreads) can run
11//! threads, so they keep the real bounds. It is the same pattern
12//! wgpu uses (`WasmNotSend`).
13//!
14//! Soundness rests on the single-thread guarantee: nothing holding a `JsValue`
15//! may be handed to another thread. Do not paper over a `!Send` type with
16//! `unsafe impl Send`.
17
18#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
19pub trait MaybeSend: Send {}
20#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
21impl<T: Send + ?Sized> MaybeSend for T {}
22
23#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
24pub trait MaybeSend {}
25#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
26impl<T: ?Sized> MaybeSend for T {}
27
28#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
29pub trait MaybeSync: Sync {}
30#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
31impl<T: Sync + ?Sized> MaybeSync for T {}
32
33#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
34pub trait MaybeSync {}
35#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
36impl<T: ?Sized> MaybeSync for T {}