Skip to main content

EventRouter

Derive Macro EventRouter 

Source
#[derive(EventRouter)]
{
    // Attributes available to this derive:
    #[sioc]
}
Expand description

Derives TryFrom<DynEvent> for a dispatcher enum.

Each variant must be a newtype wrapping Event<E> for some E: EventType + DeserializePayload.

The macro generates an internal helper enum with a serde::Deserialize impl that dispatches on the event name string, plus a TryFrom<DynEvent> impl that routes each deserialized payload to the matching variant.

§Example

// ignore: sioc-macros cannot dev-depend on sioc (circular); see sioc crate for runnable examples.
use sioc::prelude::*;

#[derive(Debug, EventType, DeserializePayload)]
struct ChatMsg { text: String }

#[derive(Debug, EventType, DeserializePayload)]
struct UserJoined { name: String }

#[derive(Debug, EventRouter)]
enum AppEvent {
    ChatMsg(Event<ChatMsg>),
    UserJoined(Event<UserJoined>),
}