Skip to main content

cratestack_cose/
maybe_send.rs

1//! The target split behind [`CoseSigner`](crate::CoseSigner) and
2//! [`CoseVerifierResolver`](crate::CoseVerifierResolver) (cratestack#1007).
3//!
4//! Natively a signer is shared between threads, so it and its futures must be
5//! `Send + Sync`. On `wasm32` there is one thread and a browser future is
6//! never `Send`, so requiring it would make a WebCrypto or keystore-callback
7//! signer impossible to write. One definition with a target-dependent
8//! supertrait keeps the two halves from drifting, where two copies of each
9//! trait would not.
10
11use std::future::Future;
12use std::pin::Pin;
13
14/// `Send + Sync` natively, nothing on `wasm32`.
15#[cfg(not(target_arch = "wasm32"))]
16pub trait MaybeSendSync: Send + Sync {}
17#[cfg(not(target_arch = "wasm32"))]
18impl<T: Send + Sync + ?Sized> MaybeSendSync for T {}
19
20/// `Send + Sync` natively, nothing on `wasm32`.
21#[cfg(target_arch = "wasm32")]
22pub trait MaybeSendSync {}
23#[cfg(target_arch = "wasm32")]
24impl<T: ?Sized> MaybeSendSync for T {}
25
26/// `Send` natively, nothing on `wasm32`.
27#[cfg(not(target_arch = "wasm32"))]
28pub trait MaybeSend: Send {}
29#[cfg(not(target_arch = "wasm32"))]
30impl<T: Send + ?Sized> MaybeSend for T {}
31
32/// `Send` natively, nothing on `wasm32`.
33#[cfg(target_arch = "wasm32")]
34pub trait MaybeSend {}
35#[cfg(target_arch = "wasm32")]
36impl<T: ?Sized> MaybeSend for T {}
37
38/// A boxed future, `Send` natively and not on `wasm32`.
39#[cfg(not(target_arch = "wasm32"))]
40pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
41/// A boxed future, `Send` natively and not on `wasm32`.
42#[cfg(target_arch = "wasm32")]
43pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;