diff --git a/crates/matrix-sdk/src/event_handler/context.rs b/crates/matrix-sdk/src/event_handler/context.rs index 46213a0a9..2b921f61f 100644 --- a/crates/matrix-sdk/src/event_handler/context.rs +++ b/crates/matrix-sdk/src/event_handler/context.rs @@ -107,3 +107,38 @@ impl Deref for Ctx { &self.0 } } + +// `EventHandlerContext` for tuples. + +impl EventHandlerContext for () { + fn from_data(_data: &EventHandlerData<'_>) -> Option { + Some(()) + } +} + +macro_rules! impl_context_for_tuple { + ( $( $ty:ident ),* $(,)? ) => { + #[allow(non_snake_case)] + impl< $( $ty ),* > EventHandlerContext for ( $( $ty ),* , ) + where + $( $ty : EventHandlerContext, )* + { + fn from_data(data: &EventHandlerData<'_>) -> Option { + $( + let $ty = $ty ::from_data(data)?; + )* + + Some(( $( $ty ),* , )) + } + } + }; +} + +impl_context_for_tuple!(A); +impl_context_for_tuple!(A, B); +impl_context_for_tuple!(A, B, C); +impl_context_for_tuple!(A, B, C, D); +impl_context_for_tuple!(A, B, C, D, E); +impl_context_for_tuple!(A, B, C, D, E, F); +impl_context_for_tuple!(A, B, C, D, E, F, G); +impl_context_for_tuple!(A, B, C, D, E, F, G, H); diff --git a/crates/matrix-sdk/src/event_handler/mod.rs b/crates/matrix-sdk/src/event_handler/mod.rs index c37561981..0c63f33ee 100644 --- a/crates/matrix-sdk/src/event_handler/mod.rs +++ b/crates/matrix-sdk/src/event_handler/mod.rs @@ -753,6 +753,20 @@ mod tests { Ok(()) } + #[async_test] + #[allow(dependency_on_unit_never_type_fallback)] + async fn test_add_event_handler_with_tuples() -> crate::Result<()> { + let client = logged_in_client(None).await; + + client.add_event_handler( + |_ev: OriginalSyncRoomMemberEvent, (_room, _client): (Room, Client)| future::ready(()), + ); + + // If it compiles, it works. No need to assert anything. + + Ok(()) + } + #[async_test] #[allow(dependency_on_unit_never_type_fallback)] async fn test_remove_event_handler() -> crate::Result<()> {