1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/// A conditionally compiled atomic or non-atomic reference counter.
/// In this target it's backed by `std::sync::Arc<T>`.
#[cfg(not(target_arch = "wasm32"))]
pub type Arc<T> = std::sync::Arc<T>;

/// A conditionally compiled atomic or non-atomic reference counter.
/// In this target it's backed by `std::rc::Rc<T>`.
#[cfg(target_arch = "wasm32")]
pub type Arc<T> = std::rc::Rc<T>;

/// A conditionally compiled `BoxFuture`.
/// Resolves to either `BoxFuture` or `LocalBoxFuture`, depending on the target.
#[cfg(not(target_arch = "wasm32"))]
pub type BoxFuture<'a, T> = futures::future::BoxFuture<'a, T>;

/// A conditionally compiled `BoxFuture`.
/// Resolves to either `BoxFuture` or `LocalBoxFuture`, depending on the target.
#[cfg(target_arch = "wasm32")]
pub type BoxFuture<'a, T> = futures::future::LocalBoxFuture<'a, T>;

/// A conditionally compiled `BoxStream`.
/// Resolves to either `BoxStream` or `LocalBoxStream`, depending on the target.
#[cfg(not(target_arch = "wasm32"))]
pub type BoxStream<'a, T> = futures::stream::BoxStream<'a, T>;

/// A conditionally compiled `BoxStream`.
/// Resolves to either `BoxStream` or `LocalBoxStream`, depending on the target.
#[cfg(target_arch = "wasm32")]
pub type BoxStream<'a, T> = futures::stream::LocalBoxStream<'a, T>;

/// A conditionally compiled trait indirection for `Send` bounds.
/// This target makes it require `Send`.
#[cfg(not(target_arch = "wasm32"))]
pub trait CondSend: Send {}

/// A conditionally compiled trait indirection for `Send` bounds.
/// This target makes it not require any marker traits.
#[cfg(target_arch = "wasm32")]
pub trait CondSend {}

#[cfg(not(target_arch = "wasm32"))]
impl<S> CondSend for S where S: Send {}

#[cfg(target_arch = "wasm32")]
impl<S> CondSend for S {}

/// A conditionally compiled trait indirection for `Send + Sync` bounds.
/// This target makes it require `Send + Sync`.
#[cfg(not(target_arch = "wasm32"))]
pub trait CondSync: Send + Sync {}

/// A conditionally compiled trait indirection for `Send + Sync` bounds.
/// This target makes it not require any marker traits.
#[cfg(target_arch = "wasm32")]
pub trait CondSync {}

#[cfg(not(target_arch = "wasm32"))]
impl<S> CondSync for S where S: Send + Sync {}

#[cfg(target_arch = "wasm32")]
impl<S> CondSync for S {}

#[cfg(not(target_arch = "wasm32"))]
pub fn boxed_fut<'a, T>(
    fut: impl futures::future::Future<Output = T> + Sized + CondSend + 'a,
) -> BoxFuture<'a, T> {
    futures::future::FutureExt::boxed(fut)
}

#[cfg(target_arch = "wasm32")]
pub fn boxed_fut<'a, T>(
    fut: impl futures::future::Future<Output = T> + Sized + CondSend + 'a,
) -> BoxFuture<'a, T> {
    futures::future::FutureExt::boxed_local(fut)
}

#[cfg(not(target_arch = "wasm32"))]
pub fn boxed_stream<'a, T>(
    stream: impl futures::stream::Stream<Item = T> + Sized + CondSend + 'a,
) -> BoxStream<'a, T> {
    futures::stream::StreamExt::boxed(stream)
}

#[cfg(target_arch = "wasm32")]
pub fn boxed_stream<'a, T>(
    stream: impl futures::stream::Stream<Item = T> + Sized + CondSend + 'a,
) -> BoxStream<'a, T> {
    futures::stream::StreamExt::boxed_local(stream)
}