mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-06 15:04:11 -04:00
feat(sdk): Implement EventHandlerContext for tuples.
This patch implements `EventHandlerContext` for tuples where each part implements `EventHandlerContext` itself.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<()> {
|
||||
|
||||
Reference in New Issue
Block a user