Skip to main content

Event

Trait Event 

Source
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§

Source

fn event_name() -> &'static str
where Self: Sized,

Unique name for this event type, used for routing and topic matching.

Provided Methods§

Source

fn topic() -> &'static str
where 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.

Source

fn to_json(&self) -> Option<Value>

Serialize event to JSON for observers like TriggerRuleEngine. Returns None by default — events that implement Serialize can override to provide their JSON representation.

Source

fn from_json(json: &str) -> Option<Self>
where Self: Sized,

Deserialize event from a JSON string.

Default returns None — events that implement Deserialize can override to provide deserialization. The #[derive(Event)] macro auto-generates this using serde_json::from_str.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§