pub trait Event:
Clone
+ Send
+ Sync
+ 'static {
// Required method
fn event_name() -> &'static str
where Self: Sized;
// Provided methods
fn topic() -> &'static str
where Self: Sized { ... }
fn to_json(&self) -> Option<Value> { ... }
fn from_json(json: &str) -> Option<Self>
where Self: Sized { ... }
}Expand description
Core trait that all events must implement.
Events must be Clone + Send + Sync + 'static so they can be type-erased
via Arc<dyn Any + Send + Sync> and downcast back to the concrete type
in subscriber handlers.
§Example
ⓘ
#[derive(Clone, Debug)]
struct UserCreated {
user_id: u64,
name: String,
}
impl Event for UserCreated {
fn event_name() -> &'static str { "user.created" }
fn topic() -> &'static str { "user" }
}Required Methods§
Sourcefn event_name() -> &'static strwhere
Self: Sized,
fn event_name() -> &'static strwhere
Self: Sized,
Unique name for this event type, used for routing and topic matching.
Provided Methods§
Sourcefn topic() -> &'static strwhere
Self: Sized,
fn topic() -> &'static strwhere
Self: Sized,
Topic this event belongs to. Default is the event_name itself.
Override this when multiple event types share a common topic namespace,
e.g., "user" for both UserCreated and UserDeleted.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".