pub trait Event:
Clone
+ Send
+ Sync
+ 'static {
// Required method
fn name(&self) -> &'static str;
// Provided methods
fn as_any(&self) -> &(dyn Any + 'static)
where Self: Sized { ... }
fn dispatch(self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>
where Self: Sized { ... }
fn dispatch_sync(self)
where Self: Sized { ... }
}Expand description
Marker trait for events that can be dispatched.
Events are simple data structures that represent something that happened in your application. They should be cheap to clone and contain all the data needed by listeners.
§Example
use ferro_events::Event;
#[derive(Clone)]
struct OrderPlaced {
order_id: i64,
user_id: i64,
total: f64,
}
impl Event for OrderPlaced {
fn name(&self) -> &'static str {
"OrderPlaced"
}
}
// Dispatch the event (Laravel-style API):
// OrderPlaced { order_id: 1, user_id: 2, total: 99.99 }.dispatch().await?;Required Methods§
Provided Methods§
Sourcefn as_any(&self) -> &(dyn Any + 'static)where
Self: Sized,
fn as_any(&self) -> &(dyn Any + 'static)where
Self: Sized,
Returns the event as Any for type erasure.
Sourcefn dispatch(self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>where
Self: Sized,
fn dispatch(self) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>where
Self: Sized,
Dispatch this event using the global dispatcher.
This is the ergonomic Laravel-style API for dispatching events.
§Example
ⓘ
use ferro_events::Event;
#[derive(Clone)]
struct UserRegistered { user_id: i64 }
impl Event for UserRegistered {
fn name(&self) -> &'static str { "UserRegistered" }
}
async fn register_user() -> Result<(), ferro_events::Error> {
// ... registration logic ...
UserRegistered { user_id: 123 }.dispatch().await?;
Ok(())
}Sourcefn dispatch_sync(self)where
Self: Sized,
fn dispatch_sync(self)where
Self: Sized,
Dispatch this event without waiting (fire and forget).
This spawns the event handling as a background task and returns immediately. Useful when you don’t need to wait for listeners to complete.
§Example
ⓘ
use ferro_events::Event;
#[derive(Clone)]
struct PageViewed { page: String }
impl Event for PageViewed {
fn name(&self) -> &'static str { "PageViewed" }
}
fn track_page_view(page: &str) {
PageViewed { page: page.to_string() }.dispatch_sync();
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.