Skip to main content

fasm_storage/
maybe_send.rs

1//! Target-aware marker bounds for native and browser storage implementations.
2
3/// A portability bound that requires [`Send`] on native targets.
4///
5/// Native executors move storage sessions and their futures between tasks, so
6/// those values must be `Send`. Browser IndexedDB implementations may instead
7/// hold JavaScript values that cannot cross threads. Defining that difference
8/// here keeps conditional bounds out of the storage traits and preserves their
9/// native contract.
10///
11/// Native `async fn` trait implementations need no target-specific attribute;
12/// use this marker on explicit future bounds that must retain the native
13/// `Send` contract while permitting thread-local browser futures.
14#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
15pub trait MaybeSend: Send {}
16
17#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
18impl<T: Send> MaybeSend for T {}
19
20/// A portability bound that does not require [`Send`] in browsers.
21///
22/// IndexedDB handles can contain `JsValue`s, which are not `Send`. The browser
23/// form therefore accepts every type while native builds retain the `Send`
24/// supertrait. This applies only to `wasm32-unknown-unknown` browsers, not all
25/// WebAssembly targets: `wasm32-wasip1-threads`, for example, has real threads.
26/// Keeping the distinction in this marker leaves the native storage contract
27/// intact.
28///
29/// Native `async fn` trait implementations need no target-specific attribute;
30/// use this marker on explicit future bounds that may be `!Send` only in the
31/// browser.
32#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
33pub trait MaybeSend {}
34
35#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
36impl<T> MaybeSend for T {}
37
38/// A portability bound that requires [`Sync`] on native targets.
39///
40/// Native executors may share storage sessions between tasks, so session and
41/// error values at storage boundaries must be `Sync`. Browser IndexedDB
42/// handles may contain JavaScript values that are not `Sync`; centralizing the
43/// conditional requirement here keeps the native contract unchanged.
44///
45/// The browser exemption deliberately applies only to
46/// `wasm32-unknown-unknown`. It does not use `target_family = "wasm"`, which
47/// would incorrectly exempt threaded targets such as `wasm32-wasip1-threads`.
48#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
49pub trait MaybeSync: Sync {}
50
51#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
52impl<T: Sync> MaybeSync for T {}
53
54/// A portability bound that does not require [`Sync`] in browsers.
55///
56/// IndexedDB handles can contain `JsValue`s, which are not `Sync`, so browser
57/// storage implementations need this marker to accept every type. Native and
58/// threaded WebAssembly targets still receive the `Sync` supertrait; in
59/// particular, the predicate excludes `wasm32-wasip1-threads`. Defining that
60/// difference only here preserves the native storage contract.
61#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
62pub trait MaybeSync {}
63
64#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
65impl<T> MaybeSync for T {}