feat(sdk): Implement EventHandlerContext for tuples.

This patch implements `EventHandlerContext` for tuples where each part
implements `EventHandlerContext` itself.
This commit is contained in:
Ivan Enderlin
2024-11-12 15:39:00 +01:00
parent 8f8aad6f4d
commit e798a51709
2 changed files with 49 additions and 0 deletions

View File

@@ -107,3 +107,38 @@ impl<T> Deref for Ctx<T> {
&self.0
}
}
// `EventHandlerContext` for tuples.
impl EventHandlerContext for () {
fn from_data(_data: &EventHandlerData<'_>) -> Option<Self> {
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<Self> {
$(
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);

View File

@@ -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<()> {