Trait matrix_sdk::event_handler::EventHandler

source ·
pub trait EventHandler<Ev, Ctx>: Clone + SendOutsideWasm + SyncOutsideWasm + 'static { }
Expand description

Interface for event handlers.

This trait is an abstraction for a certain kind of functions / closures, specifically:

  • They must have at least one argument, which is the event itself, a type that implements SyncEvent. Any additional arguments need to implement the EventHandlerContext trait.
  • Their return type has to be one of: (), `Result<(), impl Display + Debug
    • ’static>(if you are usinganyhow::Resultoreyre::Resultyou can additionally enable theanyhow/eyrefeature to get the verboseDebug` output printed on error)

§How it works

This trait is basically a very constrained version of Fn: It requires at least one argument, which is represented as its own generic parameter Ev with the remaining parameter types being represented by the second generic parameter Ctx; they have to be stuffed into one generic parameter as a tuple because Rust doesn’t have variadic generics.

Ev and Ctx are generic parameters rather than associated types because the argument list is a generic parameter for the Fn traits too, so a single type could implement Fn multiple times with different argument lists¹. Luckily, when calling Client::add_event_handler with a closure argument the trait solver takes into account that only a single one of the implementations applies (even though this could theoretically change through a dependency upgrade) and uses that rather than raising an ambiguity error. This is the same trick used by web frameworks like actix-web and axum.

¹ the only thing stopping such types from existing in stable Rust is that all manual implementations of the Fn traits require a Nightly feature

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<Ev, Fun, Fut> EventHandler<Ev, ()> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture,

source§

impl<Ev, Fun, Fut, A> EventHandler<Ev, (A,)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext,

source§

impl<Ev, Fun, Fut, A, B> EventHandler<Ev, (A, B)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A, B) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext, B: EventHandlerContext,

source§

impl<Ev, Fun, Fut, A, B, C> EventHandler<Ev, (A, B, C)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A, B, C) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext, B: EventHandlerContext, C: EventHandlerContext,

source§

impl<Ev, Fun, Fut, A, B, C, D> EventHandler<Ev, (A, B, C, D)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A, B, C, D) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext, B: EventHandlerContext, C: EventHandlerContext, D: EventHandlerContext,

source§

impl<Ev, Fun, Fut, A, B, C, D, E> EventHandler<Ev, (A, B, C, D, E)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A, B, C, D, E) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext, B: EventHandlerContext, C: EventHandlerContext, D: EventHandlerContext, E: EventHandlerContext,

source§

impl<Ev, Fun, Fut, A, B, C, D, E, F> EventHandler<Ev, (A, B, C, D, E, F)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A, B, C, D, E, F) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext, B: EventHandlerContext, C: EventHandlerContext, D: EventHandlerContext, E: EventHandlerContext, F: EventHandlerContext,

source§

impl<Ev, Fun, Fut, A, B, C, D, E, F, G> EventHandler<Ev, (A, B, C, D, E, F, G)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A, B, C, D, E, F, G) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext, B: EventHandlerContext, C: EventHandlerContext, D: EventHandlerContext, E: EventHandlerContext, F: EventHandlerContext, G: EventHandlerContext,

source§

impl<Ev, Fun, Fut, A, B, C, D, E, F, G, H> EventHandler<Ev, (A, B, C, D, E, F, G, H)> for Fun
where Ev: SyncEvent, Fun: FnOnce(Ev, A, B, C, D, E, F, G, H) -> Fut + Clone + SendOutsideWasm + SyncOutsideWasm + 'static, Fut: EventHandlerFuture, A: EventHandlerContext, B: EventHandlerContext, C: EventHandlerContext, D: EventHandlerContext, E: EventHandlerContext, F: EventHandlerContext, G: EventHandlerContext, H: EventHandlerContext,