Skip to main content

TopicRouter

Struct TopicRouter 

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

Source

pub fn new() -> Self

Creates a new empty topic router.

Source

pub fn subscription_count(&self) -> usize

Returns the number of active subscriptions.

Source

pub fn is_empty(&self) -> bool

Returns whether there are any subscriptions.

Source

pub fn patterns(&self) -> Vec<&str>

Returns all active subscription patterns.

Source

pub fn handler_ids(&self) -> Vec<&str>

Returns all subscription handler IDs.

Source

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.

Source

pub fn unsubscribe(&mut self, pattern: MStr<Pattern>, handler: &TypedHandler<T>)

Unsubscribes a handler from a topic pattern.

Source

pub fn remove_handler(&mut self, pattern: MStr<Pattern>, handler_id: Ustr)

Removes a specific handler from a pattern by handler ID.

Source

pub fn is_subscribed( &self, pattern: MStr<Pattern>, handler: &TypedHandler<T>, ) -> bool

Checks if a handler is subscribed to a pattern.

Source

pub fn has_subscribers(&self, topic: MStr<Topic>) -> bool

Returns whether there are subscribers for the topic.

Source

pub fn subscriber_count(&self, topic: MStr<Topic>) -> usize

Returns the count of subscribers for a topic.

Source

pub fn exact_subscriber_count(&self, topic: MStr<Topic>) -> usize

Returns the count of subscribers with an exact topic match, excluding wildcard pattern subscriptions.

Source

pub fn publish(&mut self, topic: MStr<Topic>, message: &T)

Publishes a message to all handlers subscribed to matching patterns.

Source

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.

Source

pub fn clear(&mut self)

Clears all subscriptions and cache.

Trait Implementations§

Source§

impl<T: Debug + 'static> Debug for TopicRouter<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: 'static> Default for TopicRouter<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for TopicRouter<T>

§

impl<T> !Send for TopicRouter<T>

§

impl<T> !Sync for TopicRouter<T>

§

impl<T> !UnwindSafe for TopicRouter<T>

§

impl<T> Freeze for TopicRouter<T>
where IndexSet<TypedSubscription<T>>: Freeze,

§

impl<T> Unpin for TopicRouter<T>
where IndexSet<TypedSubscription<T>>: Unpin,

§

impl<T> UnsafeUnpin for TopicRouter<T>
where IndexSet<TypedSubscription<T>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more