rings_runtime/bound.rs
1//! Thread-safety bounds that differ between the two runtimes.
2//!
3//! Native code runs on a multi-threaded Tokio executor, so anything it schedules or shares
4//! must cross threads; the browser executor is the single JavaScript thread, whose futures
5//! and handles (`Rc`, `JsValue`) are never `Send`. Each bound below is written once by its
6//! consumers and resolves per target:
7//!
8//! ```text
9//! MaybeSend ≅ Send (native) | ⊤ (browser)
10//! MaybeSendSync ≅ Send ∧ Sync (native) | ⊤ (browser)
11//! ```
12//!
13//! `MaybeSend` bounds what is *moved* onto the executor (a spawned future and its output);
14//! `MaybeSendSync` bounds what is *shared* between tasks (protocol state, connections).
15//!
16//! A trait object cannot name either trait (only auto traits may follow the principal trait
17//! of a `dyn`), so the same resolution is offered in type position by two macros:
18//!
19//! ```text
20//! maybe_send!(dyn T) ≅ dyn T + Send (native) | dyn T (browser)
21//! maybe_send_sync!(dyn T) ≅ dyn T + Send + Sync (native) | dyn T (browser)
22//! ```
23//!
24//! Each macro is selected by *this* crate's cfg when `rings-runtime` is compiled, so every
25//! consumer's trait objects agree with its trait bounds instead of re-deriving the target
26//! split under a predicate of its own.
27
28/// Bound on values moved onto the executor: `Send` on native, unconstrained in the browser.
29#[cfg(not(all(feature = "browser", target_family = "wasm")))]
30pub trait MaybeSend: Send {}
31#[cfg(not(all(feature = "browser", target_family = "wasm")))]
32impl<T: Send + ?Sized> MaybeSend for T {}
33
34/// Bound on values moved onto the executor: `Send` on native, unconstrained in the browser.
35#[cfg(all(feature = "browser", target_family = "wasm"))]
36pub trait MaybeSend {}
37#[cfg(all(feature = "browser", target_family = "wasm"))]
38impl<T: ?Sized> MaybeSend for T {}
39
40/// Bound on values shared between tasks: `Send + Sync` on native, unconstrained in the browser.
41#[cfg(not(all(feature = "browser", target_family = "wasm")))]
42pub trait MaybeSendSync: Send + Sync {}
43#[cfg(not(all(feature = "browser", target_family = "wasm")))]
44impl<T: Send + Sync + ?Sized> MaybeSendSync for T {}
45
46/// Bound on values shared between tasks: `Send + Sync` on native, unconstrained in the browser.
47#[cfg(all(feature = "browser", target_family = "wasm"))]
48pub trait MaybeSendSync {}
49#[cfg(all(feature = "browser", target_family = "wasm"))]
50impl<T: ?Sized> MaybeSendSync for T {}
51
52/// A trait object that is `Send` on native and unconstrained in the browser.
53///
54/// `maybe_send!(dyn Future<Output = ()> + 'static)` names the object type a native executor
55/// may move between threads; see the module documentation.
56#[cfg(not(all(feature = "browser", target_family = "wasm")))]
57#[macro_export]
58macro_rules! maybe_send {
59 (dyn $($object:tt)+) => { dyn $($object)+ + ::core::marker::Send };
60}
61
62/// A trait object that is `Send` on native and unconstrained in the browser.
63#[cfg(all(feature = "browser", target_family = "wasm"))]
64#[macro_export]
65macro_rules! maybe_send {
66 (dyn $($object:tt)+) => { dyn $($object)+ };
67}
68
69/// A trait object that is `Send + Sync` on native and unconstrained in the browser.
70///
71/// `Arc<maybe_send_sync!(dyn Handler)>` names a handler shared between native tasks; see the
72/// module documentation.
73#[cfg(not(all(feature = "browser", target_family = "wasm")))]
74#[macro_export]
75macro_rules! maybe_send_sync {
76 (dyn $($object:tt)+) => { dyn $($object)+ + ::core::marker::Send + ::core::marker::Sync };
77}
78
79/// A trait object that is `Send + Sync` on native and unconstrained in the browser.
80#[cfg(all(feature = "browser", target_family = "wasm"))]
81#[macro_export]
82macro_rules! maybe_send_sync {
83 (dyn $($object:tt)+) => { dyn $($object)+ };
84}
85
86#[cfg(all(test, not(target_family = "wasm")))]
87mod tests {
88 use std::future::Future;
89 use std::pin::Pin;
90
91 fn is_send<T: Send + ?Sized>() {}
92 fn is_send_sync<T: Send + Sync + ?Sized>() {}
93
94 /// Law: on native the macros resolve to the same bounds as the traits they mirror.
95 #[test]
96 fn test_native_trait_objects_carry_the_native_bounds() {
97 is_send::<maybe_send!(dyn Future<Output = ()> + 'static)>();
98 is_send_sync::<maybe_send_sync!(dyn std::error::Error)>();
99 is_send::<Pin<Box<maybe_send!(dyn Future<Output = u8>)>>>();
100 }
101}