Skip to main content

AsyncEventListener

Trait AsyncEventListener 

Source
pub trait AsyncEventListener<T: Event>: Send + Sync {
    // Required method
    fn handle<'a>(&'a self, event: &'a T) -> AsyncEventResult<'a>;

    // Provided method
    fn priority(&self) -> Priority { ... }
}
Expand description

Trait for asynchronous event listeners.

This trait is only available when the async feature is enabled.

§Example

use mod_events::{AsyncEventListener, AsyncEventResult, Event, ListenerError, Priority};

#[derive(Debug, Clone)]
struct UserRegistered {
    user_id: u64,
    email: String,
}

impl Event for UserRegistered {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

struct AsyncEmailNotifier;

impl AsyncEventListener<UserRegistered> for AsyncEmailNotifier {
    fn handle<'a>(&'a self, event: &'a UserRegistered) -> AsyncEventResult<'a> {
        Box::pin(async move {
            println!("Async email sent to {}", event.email);
            Ok(())
        })
    }
}

Required Methods§

Source

fn handle<'a>(&'a self, event: &'a T) -> AsyncEventResult<'a>

Handle the event asynchronously.

Provided Methods§

Source

fn priority(&self) -> Priority

Get the priority of this listener.

Higher priority listeners are executed first. Default is Priority::Normal.

Implementors§