Read-only mirror of github.com/ondeinference/ed.
Mirrored from https://github.com/ondeinference/ed.git
Synced Nov 23, 01:00 UTC
| 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")))] |
| 19 | pub trait MaybeSend: Send {} |
| 20 | #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] |
| 21 | impl<T: Send + ?Sized> MaybeSend for T {} |
| 22 | |
| 23 | #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 24 | pub trait MaybeSend {} |
| 25 | #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 26 | impl<T: ?Sized> MaybeSend for T {} |
| 27 | |
| 28 | #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] |
| 29 | pub trait MaybeSync: Sync {} |
| 30 | #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] |
| 31 | impl<T: Sync + ?Sized> MaybeSync for T {} |
| 32 | |
| 33 | #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 34 | pub trait MaybeSync {} |
| 35 | #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 36 | impl<T: ?Sized> MaybeSync for T {} |