pub struct TopicRouter<T: 'static> { /* private fields */ }Expand description
Routes messages of type T to subscribed handlers based on topic patterns.
Supports wildcard patterns (* and ?) and priority-based ordering.
Caches topic-to-subscription mappings for efficient repeated lookups.
Implementations§
Source§impl<T: 'static> TopicRouter<T>
impl<T: 'static> TopicRouter<T>
Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Returns the number of active subscriptions.
Sourcepub fn handler_ids(&self) -> Vec<&str>
pub fn handler_ids(&self) -> Vec<&str>
Returns all subscription handler IDs.
Sourcepub fn subscribe(
&mut self,
pattern: MStr<Pattern>,
handler: TypedHandler<T>,
priority: u32,
)
pub fn subscribe( &mut self, pattern: MStr<Pattern>, handler: TypedHandler<T>, priority: u32, )
Subscribes a handler to a topic pattern.
§Warning
Assigning priority is an advanced feature. Higher priority handlers receive messages before lower priority handlers.
Sourcepub fn unsubscribe(&mut self, pattern: MStr<Pattern>, handler: &TypedHandler<T>)
pub fn unsubscribe(&mut self, pattern: MStr<Pattern>, handler: &TypedHandler<T>)
Unsubscribes a handler from a topic pattern.
Sourcepub fn remove_handler(&mut self, pattern: MStr<Pattern>, handler_id: Ustr)
pub fn remove_handler(&mut self, pattern: MStr<Pattern>, handler_id: Ustr)
Removes a specific handler from a pattern by handler ID.
Sourcepub fn is_subscribed(
&self,
pattern: MStr<Pattern>,
handler: &TypedHandler<T>,
) -> bool
pub fn is_subscribed( &self, pattern: MStr<Pattern>, handler: &TypedHandler<T>, ) -> bool
Checks if a handler is subscribed to a pattern.
Sourcepub fn has_subscribers(&self, topic: MStr<Topic>) -> bool
pub fn has_subscribers(&self, topic: MStr<Topic>) -> bool
Returns whether there are subscribers for the topic.
Sourcepub fn subscriber_count(&self, topic: MStr<Topic>) -> usize
pub fn subscriber_count(&self, topic: MStr<Topic>) -> usize
Returns the count of subscribers for a topic.
Sourcepub fn exact_subscriber_count(&self, topic: MStr<Topic>) -> usize
pub fn exact_subscriber_count(&self, topic: MStr<Topic>) -> usize
Returns the count of subscribers with an exact topic match, excluding wildcard pattern subscriptions.
Sourcepub fn publish(&mut self, topic: MStr<Topic>, message: &T)
pub fn publish(&mut self, topic: MStr<Topic>, message: &T)
Publishes a message to all handlers subscribed to matching patterns.
Sourcepub fn get_matching_handlers(
&mut self,
topic: MStr<Topic>,
) -> Vec<TypedHandler<T>>
pub fn get_matching_handlers( &mut self, topic: MStr<Topic>, ) -> Vec<TypedHandler<T>>
Returns cloned handlers matching a topic for safe out-of-borrow calling.
Use this when handlers may need to access the message bus during execution.
Note: Allocates a Vec on each call. For hot paths, prefer the thread-local
buffer pattern used by publish_* functions.