Skip to main content

web_async/
maybe.rs

1use std::future::Future;
2use std::pin::Pin;
3
4#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
5/// A trait that is `Send` on native/WASI targets and empty in browser WASM.
6pub trait MaybeSend: Send {}
7
8#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
9impl<T: Send> MaybeSend for T {}
10
11#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
12/// A trait that is `Send` on native/WASI targets and empty in browser WASM.
13pub trait MaybeSend {}
14
15#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
16impl<T> MaybeSend for T {}
17
18#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
19/// A trait that is `Sync` on native/WASI targets and empty in browser WASM.
20pub trait MaybeSync: Sync {}
21
22#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
23impl<T: Sync> MaybeSync for T {}
24
25#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
26/// A trait that is `Sync` on native/WASI targets and empty in browser WASM.
27pub trait MaybeSync {}
28
29#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
30impl<T> MaybeSync for T {}
31
32#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
33/// A boxed future that is `Send` on native/WASI targets and local in browser WASM.
34pub type MaybeSendBoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
35
36#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
37/// A boxed future that is `Send` on native/WASI targets and local in browser WASM.
38pub type MaybeSendBoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
39
40/// Extension helpers for boxing futures with target-appropriate `Send` bounds.
41pub trait MaybeFutureExt: Future + Sized {
42	/// Box this future with target-appropriate `Send` bounds.
43	fn maybe_boxed<'a>(self) -> MaybeSendBoxFuture<'a, Self::Output>
44	where
45		Self: MaybeSend + 'a,
46	{
47		Box::pin(self)
48	}
49}
50
51impl<F: Future> MaybeFutureExt for F {}