alopex_core/
async_util.rs

1//! Async utilities shared across the Alopex workspace.
2//!
3//! This module provides a conditional `Send` bound that works on both native
4//! targets and `wasm32`, plus boxed `Future`/`Stream` type aliases that use it.
5
6use core::future::Future;
7use core::pin::Pin;
8
9use futures_core::Stream;
10
11/// A marker trait used to conditionally require `Send`.
12///
13/// - On native targets, `MaybeSend` implies `Send`.
14/// - On `wasm32`, `MaybeSend` has no additional requirements.
15#[cfg(not(target_arch = "wasm32"))]
16pub trait MaybeSend: Send {}
17
18#[cfg(not(target_arch = "wasm32"))]
19impl<T: Send> MaybeSend for T {}
20
21/// `wasm32` variant of [`MaybeSend`].
22#[cfg(target_arch = "wasm32")]
23pub trait MaybeSend {}
24
25#[cfg(target_arch = "wasm32")]
26impl<T> MaybeSend for T {}
27
28/// A boxed future that is `Send` on native targets and not `Send` on `wasm32`.
29#[cfg(not(target_arch = "wasm32"))]
30pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
31
32/// A boxed future that does not require `Send` (for `wasm32`).
33#[cfg(target_arch = "wasm32")]
34pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
35
36/// A boxed stream that is `Send` on native targets and not `Send` on `wasm32`.
37#[cfg(not(target_arch = "wasm32"))]
38pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + Send + 'a>>;
39
40/// A boxed stream that does not require `Send` (for `wasm32`).
41#[cfg(target_arch = "wasm32")]
42pub type BoxStream<'a, T> = Pin<Box<dyn Stream<Item = T> + 'a>>;