wnfs_common/utils/
send_sync_poly.rs

1/// A conditionally compiled atomic or non-atomic reference counter.
2/// In this target it's backed by `std::sync::Arc<T>`.
3#[cfg(not(target_arch = "wasm32"))]
4pub type Arc<T> = std::sync::Arc<T>;
5
6/// A conditionally compiled atomic or non-atomic reference counter.
7/// In this target it's backed by `std::rc::Rc<T>`.
8#[cfg(target_arch = "wasm32")]
9pub type Arc<T> = std::rc::Rc<T>;
10
11/// A conditionally compiled `BoxFuture`.
12/// Resolves to either `BoxFuture` or `LocalBoxFuture`, depending on the target.
13#[cfg(not(target_arch = "wasm32"))]
14pub type BoxFuture<'a, T> = futures::future::BoxFuture<'a, T>;
15
16/// A conditionally compiled `BoxFuture`.
17/// Resolves to either `BoxFuture` or `LocalBoxFuture`, depending on the target.
18#[cfg(target_arch = "wasm32")]
19pub type BoxFuture<'a, T> = futures::future::LocalBoxFuture<'a, T>;
20
21/// A conditionally compiled `BoxStream`.
22/// Resolves to either `BoxStream` or `LocalBoxStream`, depending on the target.
23#[cfg(not(target_arch = "wasm32"))]
24pub type BoxStream<'a, T> = futures::stream::BoxStream<'a, T>;
25
26/// A conditionally compiled `BoxStream`.
27/// Resolves to either `BoxStream` or `LocalBoxStream`, depending on the target.
28#[cfg(target_arch = "wasm32")]
29pub type BoxStream<'a, T> = futures::stream::LocalBoxStream<'a, T>;
30
31/// A conditionally compiled trait indirection for `Send` bounds.
32/// This target makes it require `Send`.
33#[cfg(not(target_arch = "wasm32"))]
34pub trait CondSend: Send {}
35
36/// A conditionally compiled trait indirection for `Send` bounds.
37/// This target makes it not require any marker traits.
38#[cfg(target_arch = "wasm32")]
39pub trait CondSend {}
40
41#[cfg(not(target_arch = "wasm32"))]
42impl<S> CondSend for S where S: Send {}
43
44#[cfg(target_arch = "wasm32")]
45impl<S> CondSend for S {}
46
47/// A conditionally compiled trait indirection for `Send + Sync` bounds.
48/// This target makes it require `Send + Sync`.
49#[cfg(not(target_arch = "wasm32"))]
50pub trait CondSync: Send + Sync {}
51
52/// A conditionally compiled trait indirection for `Send + Sync` bounds.
53/// This target makes it not require any marker traits.
54#[cfg(target_arch = "wasm32")]
55pub trait CondSync {}
56
57#[cfg(not(target_arch = "wasm32"))]
58impl<S> CondSync for S where S: Send + Sync {}
59
60#[cfg(target_arch = "wasm32")]
61impl<S> CondSync for S {}
62
63#[cfg(not(target_arch = "wasm32"))]
64pub fn boxed_fut<'a, T>(
65    fut: impl futures::future::Future<Output = T> + Sized + CondSend + 'a,
66) -> BoxFuture<'a, T> {
67    futures::future::FutureExt::boxed(fut)
68}
69
70#[cfg(target_arch = "wasm32")]
71pub fn boxed_fut<'a, T>(
72    fut: impl futures::future::Future<Output = T> + Sized + CondSend + 'a,
73) -> BoxFuture<'a, T> {
74    futures::future::FutureExt::boxed_local(fut)
75}
76
77#[cfg(not(target_arch = "wasm32"))]
78pub fn boxed_stream<'a, T>(
79    stream: impl futures::stream::Stream<Item = T> + Sized + CondSend + 'a,
80) -> BoxStream<'a, T> {
81    futures::stream::StreamExt::boxed(stream)
82}
83
84#[cfg(target_arch = "wasm32")]
85pub fn boxed_stream<'a, T>(
86    stream: impl futures::stream::Stream<Item = T> + Sized + CondSend + 'a,
87) -> BoxStream<'a, T> {
88    futures::stream::StreamExt::boxed_local(stream)
89}