send.rsannotatedsend.rssource39 lines · 1.4 kB · raw

Send where there are threads

A run's ports are asked from a server that may move a request between threads, so off the web their futures must be Send. In WebAssembly (a Cloudflare Worker, a browser) there is one thread, and what a host calls with (JavaScript promises, Rc) is never Send. MaybeSend and MaybeSync are Send and Sync everywhere else and ask nothing on wasm32, so one runner serves both without a wrapper that claims a future is Send when it is not.

11#[cfg(not(target_arch = "wasm32"))]
12mod bounds {

Send, except on wasm32.

14    pub trait MaybeSend: Send {}
15    impl<T: Send + ?Sized> MaybeSend for T {}

Sync, except on wasm32.

18    pub trait MaybeSync: Sync {}
19    impl<T: Sync + ?Sized> MaybeSync for T {}

A boxed future the runner recurses through.

22    pub type Boxed<'a, T> = std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>;
23}
25#[cfg(target_arch = "wasm32")]
26mod bounds {

Send, except on wasm32.

28    pub trait MaybeSend {}
29    impl<T: ?Sized> MaybeSend for T {}

Sync, except on wasm32.

32    pub trait MaybeSync {}
33    impl<T: ?Sized> MaybeSync for T {}

A boxed future the runner recurses through.

36    pub type Boxed<'a, T> = std::pin::Pin<Box<dyn Future<Output = T> + 'a>>;
37}
39pub use bounds::{Boxed, MaybeSend, MaybeSync};