pub trait EventListener<T: Event>: Send + Sync {
// Required method
fn handle(&self, event: &T) -> Result<(), ListenerError>;
// Provided method
fn priority(&self) -> Priority { ... }
}Expand description
Trait for synchronous event listeners
Implement this trait to create reusable event listeners.
For simple cases, you can use closures with the dispatcher’s
subscribe or on methods instead.
§Example
use mod_events::{Event, EventListener, 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 EmailNotifier;
impl EventListener<UserRegistered> for EmailNotifier {
fn handle(&self, event: &UserRegistered) -> Result<(), ListenerError> {
// Send email logic here
println!("Sending email to {}", event.email);
Ok(())
}
fn priority(&self) -> Priority {
Priority::High
}
}Required Methods§
Sourcefn handle(&self, event: &T) -> Result<(), ListenerError>
fn handle(&self, event: &T) -> Result<(), ListenerError>
Handle the event.
This method is called when the event is dispatched.
Return Ok(()) on success, or wrap any error in ListenerError
on failure. &str and String convert directly via Into.