Skip to main content

rig_core/
wasm_compat.rs

1use bytes::Bytes;
2use std::pin::Pin;
3
4use futures::Stream;
5
6#[cfg(not(all(feature = "wasm", target_arch = "wasm32")))]
7/// `Send` on native targets and a no-op marker on wasm32 with the `wasm` feature.
8pub trait WasmCompatSend: Send {}
9#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
10/// `Send` on native targets and a no-op marker on wasm32 with the `wasm` feature.
11pub trait WasmCompatSend {}
12
13#[cfg(not(all(feature = "wasm", target_arch = "wasm32")))]
14impl<T> WasmCompatSend for T where T: Send {}
15#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
16impl<T> WasmCompatSend for T {}
17
18#[cfg(not(all(feature = "wasm", target_arch = "wasm32")))]
19/// Streaming response bound that includes `Send` on native targets.
20pub trait WasmCompatSendStream:
21    Stream<Item = Result<Bytes, crate::http_client::Error>> + Send
22{
23    type InnerItem: Send;
24}
25
26#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
27/// Streaming response bound without `Send` on wasm32 with the `wasm` feature.
28pub trait WasmCompatSendStream: Stream<Item = Result<Bytes, crate::http_client::Error>> {
29    type InnerItem;
30}
31
32#[cfg(not(all(feature = "wasm", target_arch = "wasm32")))]
33impl<T> WasmCompatSendStream for T
34where
35    T: Stream<Item = Result<Bytes, crate::http_client::Error>> + Send,
36{
37    type InnerItem = Result<Bytes, crate::http_client::Error>;
38}
39
40#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
41impl<T> WasmCompatSendStream for T
42where
43    T: Stream<Item = Result<Bytes, crate::http_client::Error>>,
44{
45    type InnerItem = Result<Bytes, crate::http_client::Error>;
46}
47
48#[cfg(not(all(feature = "wasm", target_arch = "wasm32")))]
49/// `Sync` on native targets and a no-op marker on wasm32 with the `wasm` feature.
50pub trait WasmCompatSync: Sync {}
51#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
52/// `Sync` on native targets and a no-op marker on wasm32 with the `wasm` feature.
53pub trait WasmCompatSync {}
54
55#[cfg(not(all(feature = "wasm", target_arch = "wasm32")))]
56impl<T> WasmCompatSync for T where T: Sync {}
57#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
58impl<T> WasmCompatSync for T {}
59
60#[cfg(not(target_family = "wasm"))]
61/// Boxed future type that includes `Send` on non-wasm targets.
62pub type WasmBoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
63
64#[cfg(target_family = "wasm")]
65/// Boxed future type without `Send` on wasm targets.
66pub type WasmBoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
67
68#[macro_export]
69macro_rules! if_wasm {
70    ($($tokens:tt)*) => {
71        #[cfg(all(feature = "wasm", target_arch = "wasm32"))]
72        $($tokens)*
73
74    };
75}
76
77#[macro_export]
78macro_rules! if_not_wasm {
79    ($($tokens:tt)*) => {
80        #[cfg(not(all(feature = "wasm", target_arch = "wasm32")))]
81        $($tokens)*
82
83    };
84}