Skip to main content

Topic

Trait Topic 

Source
pub trait Topic<E: Event>:
    Hash
    + PartialEq
    + Eq
    + Clone
    + Send
    + Sync
    + 'static {
    // Required method
    fn from_event(event: &E) -> Self;

    // Provided method
    fn overflow_policy(&self) -> OverflowPolicy { ... }
}
Expand description

Maps events to routing topics.

Implement this for your own topic type (usually an enum) to classify events for the broker. Actors subscribe to one or more topics, and the broker delivers events to matching subscribers.

Topics must be Hash + Eq + Clone + Send + Sync + 'static because they are used as subscription keys in the broker, which runs in a spawned task.

Common patterns:

  • Enum topics for simple classification.
  • Struct topics when you need richer metadata (e.g., names or IDs).

Trait bounds: refer to the event trait as crate::Event in generic signatures to avoid confusion with the Event derive macro.

Required Methods§

Source

fn from_event(event: &E) -> Self

Classify an event into a topic.

Called by the broker for every incoming event to determine which subscribers should receive it. Each subscriber registered for the returned topic gets a copy of the event.

fn from_event(event: &MyEvent) -> Self {
    match event {
        MyEvent::Data(_) => MyTopic::Data,
        MyEvent::Control(_) => MyTopic::Control,
    }
}

Provided Methods§

Source

fn overflow_policy(&self) -> OverflowPolicy

Returns the overflow policy for this topic.

Controls what the broker does when a subscriber’s channel is full. See OverflowPolicy for details on each variant.

The default is OverflowPolicy::Fail, which closes the subscriber’s channel on overflow. This ensures problems are surfaced immediately rather than hidden by silent drops. Override this method to set per-topic policies:

fn overflow_policy(&self) -> OverflowPolicy {
    match self {
        MyTopic::Control => OverflowPolicy::Block,
        MyTopic::Metrics => OverflowPolicy::Drop,
    }
}

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.

Implementors§