Skip to main content

neuron_types/
wasm.rs

1//! WASM compatibility shims.
2//!
3//! On native targets, these are aliases for Send/Sync.
4//! On wasm32, the bounds are removed since wasm32 is single-threaded.
5
6use std::future::Future;
7use std::pin::Pin;
8
9#[cfg(not(target_arch = "wasm32"))]
10mod native {
11    use super::*;
12
13    /// Marker trait equivalent to `Send` on native, unconditional on WASM.
14    pub trait WasmCompatSend: Send {}
15    impl<T: Send> WasmCompatSend for T {}
16
17    /// Marker trait equivalent to `Sync` on native, unconditional on WASM.
18    pub trait WasmCompatSync: Sync {}
19    impl<T: Sync> WasmCompatSync for T {}
20
21    /// A boxed future that is `Send` on native and unbound on WASM.
22    pub type WasmBoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
23}
24
25#[cfg(target_arch = "wasm32")]
26mod wasm_impl {
27    use super::*;
28
29    /// Marker trait equivalent to `Send` on native, unconditional on WASM.
30    pub trait WasmCompatSend {}
31    impl<T> WasmCompatSend for T {}
32
33    /// Marker trait equivalent to `Sync` on native, unconditional on WASM.
34    pub trait WasmCompatSync {}
35    impl<T> WasmCompatSync for T {}
36
37    /// A boxed future that is `Send` on native and unbound on WASM.
38    pub type WasmBoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
39}
40
41#[cfg(not(target_arch = "wasm32"))]
42pub use native::*;
43
44#[cfg(target_arch = "wasm32")]
45pub use wasm_impl::*;
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    fn assert_wasm_compat_send<T: WasmCompatSend>() {}
52    fn assert_wasm_compat_sync<T: WasmCompatSync>() {}
53
54    #[test]
55    fn string_is_wasm_compat_send() {
56        assert_wasm_compat_send::<String>();
57    }
58
59    #[test]
60    fn string_is_wasm_compat_sync() {
61        assert_wasm_compat_sync::<String>();
62    }
63
64    #[test]
65    fn boxed_future_type_alias_compiles() {
66        let _fut: WasmBoxedFuture<'_, i32> = Box::pin(async { 42 });
67    }
68}