mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-14 11:05:32 -04:00
fix(sdk): Further relax event / notification handler bounds on WASM
This allows capturing non-`Send` / -`Sync` values in handler closures.
This commit is contained in:
committed by
Jonas Platte
parent
ad80839ffd
commit
4914c595e9
@@ -1,8 +1,6 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![warn(missing_debug_implementations)]
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
pub use instant;
|
||||
|
||||
pub mod deserialized_responses;
|
||||
@@ -38,13 +36,6 @@ pub trait SyncOutsideWasm {}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl<T> SyncOutsideWasm for T {}
|
||||
|
||||
/// Alias for `Future + SendOutsideWasm`.
|
||||
///
|
||||
/// Useful as a separate trait because the former trait bound can't be used
|
||||
/// with `dyn`.
|
||||
pub trait FutureSendOutsideWasm: Future + SendOutsideWasm {}
|
||||
impl<T: Future + SendOutsideWasm> FutureSendOutsideWasm for T {}
|
||||
|
||||
/// Super trait that is used for our store traits, this trait will differ if
|
||||
/// it's used on WASM. WASM targets will not require `Send` and `Sync` to have
|
||||
/// implemented, while other targets will.
|
||||
|
||||
@@ -32,7 +32,7 @@ use futures_signals::signal::Signal;
|
||||
use matrix_sdk_base::{
|
||||
deserialized_responses::SyncResponse,
|
||||
media::{MediaEventContent, MediaFormat, MediaRequest, MediaThumbnailSize},
|
||||
BaseClient, FutureSendOutsideWasm, Session, SessionMeta, SessionTokens, StateStore,
|
||||
BaseClient, SendOutsideWasm, Session, SessionMeta, SessionTokens, StateStore, SyncOutsideWasm,
|
||||
};
|
||||
use matrix_sdk_common::{
|
||||
instant::{Duration, Instant},
|
||||
@@ -110,9 +110,17 @@ const MIN_UPLOAD_REQUEST_TIMEOUT: Duration = Duration::from_secs(60 * 5);
|
||||
|
||||
type EventHandlerMap = BTreeMap<EventHandlerKey, Vec<EventHandlerWrapper>>;
|
||||
|
||||
type NotificationHandlerFut = Pin<Box<dyn FutureSendOutsideWasm<Output = ()>>>;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
type NotificationHandlerFut = Pin<Box<dyn Future<Output = ()> + Send>>;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
type NotificationHandlerFut = Pin<Box<dyn Future<Output = ()>>>;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
type NotificationHandlerFn =
|
||||
Box<dyn Fn(Notification, room::Room, Client) -> NotificationHandlerFut + Send + Sync>;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
type NotificationHandlerFn =
|
||||
Box<dyn Fn(Notification, room::Room, Client) -> NotificationHandlerFut>;
|
||||
|
||||
type AnyMap = anymap2::Map<dyn CloneAnySendSync + Send + Sync>;
|
||||
|
||||
@@ -798,8 +806,11 @@ impl Client {
|
||||
/// [`room::Room`], [`Client`] for now.
|
||||
pub async fn register_notification_handler<H, Fut>(&self, handler: H) -> &Self
|
||||
where
|
||||
H: Fn(Notification, room::Room, Client) -> Fut + Send + Sync + 'static,
|
||||
Fut: Future<Output = ()> + Send + 'static,
|
||||
H: Fn(Notification, room::Room, Client) -> Fut
|
||||
+ SendOutsideWasm
|
||||
+ SyncOutsideWasm
|
||||
+ 'static,
|
||||
Fut: Future<Output = ()> + SendOutsideWasm + 'static,
|
||||
{
|
||||
self.inner.notification_handlers.write().await.push(Box::new(
|
||||
move |notification, room, client| Box::pin((handler)(notification, room, client)),
|
||||
|
||||
@@ -45,7 +45,7 @@ use std::{
|
||||
|
||||
use matrix_sdk_base::{
|
||||
deserialized_responses::{EncryptionInfo, SyncRoomEvent},
|
||||
FutureSendOutsideWasm, SendOutsideWasm,
|
||||
SendOutsideWasm, SyncOutsideWasm,
|
||||
};
|
||||
use ruma::{events::AnySyncStateEvent, serde::Raw, OwnedRoomId};
|
||||
use serde::{de::DeserializeOwned, Deserialize};
|
||||
@@ -54,8 +54,15 @@ use tracing::{error, warn};
|
||||
|
||||
use crate::{room, Client};
|
||||
|
||||
type EventHandlerFut = Pin<Box<dyn FutureSendOutsideWasm<Output = ()>>>;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
type EventHandlerFut = Pin<Box<dyn Future<Output = ()> + Send>>;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
type EventHandlerFut = Pin<Box<dyn Future<Output = ()>>>;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
type EventHandlerFn = dyn Fn(EventHandlerData<'_>) -> EventHandlerFut + Send + Sync;
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
type EventHandlerFn = dyn Fn(EventHandlerData<'_>) -> EventHandlerFut;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
@@ -181,7 +188,7 @@ impl EventHandlerContext for EventHandlerHandle {
|
||||
///
|
||||
/// ¹ the only thing stopping such types from existing in stable Rust is that
|
||||
/// all manual implementations of the `Fn` traits require a Nightly feature
|
||||
pub trait EventHandler<Ev, Ctx>: Send + Sync + 'static {
|
||||
pub trait EventHandler<Ev, Ctx>: SendOutsideWasm + SyncOutsideWasm + 'static {
|
||||
/// The future returned by `handle_event`.
|
||||
#[doc(hidden)]
|
||||
type Future: Future + SendOutsideWasm + 'static;
|
||||
@@ -542,7 +549,7 @@ macro_rules! impl_event_handler {
|
||||
impl<Ev, Fun, Fut, $($ty),*> EventHandler<Ev, ($($ty,)*)> for Fun
|
||||
where
|
||||
Ev: SyncEvent,
|
||||
Fun: Fn(Ev, $($ty),*) -> Fut + Send + Sync + 'static,
|
||||
Fun: Fn(Ev, $($ty),*) -> Fut + SendOutsideWasm + SyncOutsideWasm + 'static,
|
||||
Fut: Future + SendOutsideWasm + 'static,
|
||||
Fut::Output: EventHandlerResult,
|
||||
$($ty: EventHandlerContext),*
|
||||
|
||||
Reference in New Issue
Block a user