pub trait EventHandler: Send + Sync {
// Required methods
fn handles(&self) -> Vec<String>;
fn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
event: &'life1 Event,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided method
fn lane(&self) -> HandlerLane { ... }
}Expand description
Trait for event handlers that process published events.
Event handlers subscribe to specific event types and execute side effects when those events occur. Handlers should be idempotent where possible.
§Thread Safety
Implementations must be Send + Sync to work with async Rust.
§Example
use arc_core::event_bus::EventHandler;
use arc_core::event::Event;
use async_trait::async_trait;
struct AuditLogHandler;
#[async_trait]
impl EventHandler for AuditLogHandler {
fn handles(&self) -> Vec<String> {
// Handle all user-related events
vec![
"UserCreated".to_string(),
"UserUpdated".to_string(),
"UserDeleted".to_string(),
]
}
async fn handle(&self, event: &Event) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
println!("Audit: {} at {}", event.event_type, event.timestamp);
// Write to audit log...
Ok(())
}
}Required Methods§
Sourcefn handles(&self) -> Vec<String>
fn handles(&self) -> Vec<String>
Returns the list of event types this handler is interested in.
The handler’s handle() method will only be called for events
whose event_type is in this list.
§Returns
Vector of event type names (e.g., [“UserCreated”, “UserUpdated”])
Sourcefn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
event: &'life1 Event,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn handle<'life0, 'life1, 'async_trait>(
&'life0 self,
event: &'life1 Event,
) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Handle a published event.
This method is called when an event matching one of the types returned
by handles() is published to the event bus.
§Arguments
event: Reference to the event being handled
§Returns
Ok(())if the event was handled successfullyErr(...)if handling failed (error will be propagated to publisher)
§Error Handling
If an error is returned, it will stop event processing for subsequent handlers. Consider logging errors and returning Ok(()) if you want to allow other handlers to continue.
Provided Methods§
Sourcefn lane(&self) -> HandlerLane
fn lane(&self) -> HandlerLane
Selects the delivery lane for this handler.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".