Skip to main content

TopicRouter

Struct TopicRouter 

Source
pub struct TopicRouter<T> { /* private fields */ }
Expand description

Routes MQTT topics to subscription payloads.

Each subscription pattern is associated with a payload T and a unique SubscriptionId. Delivered topics are matched against all stored patterns (including +/# wildcards); the router also bookkeeps which distinct broker subscriptions are active and at what QoS.

Implementations§

Source§

impl<T> TopicRouter<T>

Source

pub fn new() -> Self

Creates an empty router with no subscriptions.

Source

pub fn add_subscription( &mut self, topic: TopicPatternPath, qos: impl Into<QoS>, subscription: T, ) -> (bool, SubscriptionId)

Registers a subscription for topic with the given qos and payload.

Returns (needs_subscribe, id): needs_subscribe is true when this is the first subscription for the pattern or it raises the effective QoS, so the caller must (re)subscribe on the broker; id identifies the new subscription for later unsubscribe.

Source

pub fn unsubscribe( &mut self, id: &SubscriptionId, ) -> Result<(bool, TopicPatternPath), TopicRouterError>

Removes the subscription identified by id.

Returns (topic_now_empty, pattern): topic_now_empty is true when no other subscription remains for the pattern, so the caller should unsubscribe it on the broker. Fails with TopicRouterError::SubscriptionNotFound if id is unknown.

Source

pub fn get_subscribers<'a>( &'a self, topic: &TopicPath, ) -> Vec<(&'a SubscriptionId, &'a (TopicPatternPath, QoS), &'a T)>

Returns every subscription whose pattern matches topic.

Each entry is (id, (pattern, qos), payload) for a matching subscription (one delivered topic may match several patterns via +/# wildcards).

Source

pub fn get_active_subscriptions( &self, ) -> impl Iterator<Item = &(TopicPatternPath, QoS)>

Iterates over every active subscription as (pattern, qos).

Patterns are not deduplicated; multiple subscriptions may share one.

Source

pub fn get_topics_for_unsubscribe(&self) -> HashSet<ArcStr>

Get all unique active topic patterns

Source

pub fn get_topics_for_resubscribe(&self) -> HashMap<ArcStr, QoS>

Get all active topic patterns with their maximum QoS Returns unique topics (grouped by pattern) with the highest QoS among all subscribers

Source

pub fn cleanup(&mut self)

Cleanup all internal data structures and close subscriber channels This method is called during shutdown to ensure proper resource cleanup

Source

pub fn get_topic_by_id( &self, id: &SubscriptionId, ) -> Result<&(TopicPatternPath, QoS), TopicRouterError>

Looks up the (pattern, qos) registered for a subscription id.

Fails with TopicRouterError::SubscriptionNotFound if id is unknown.

Trait Implementations§

Source§

impl<T> Default for TopicRouter<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for TopicRouter<T>

§

impl<T> RefUnwindSafe for TopicRouter<T>
where T: RefUnwindSafe,

§

impl<T> Send for TopicRouter<T>
where T: Send,

§

impl<T> Sync for TopicRouter<T>
where T: Sync,

§

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

§

impl<T> UnsafeUnpin for TopicRouter<T>

§

impl<T> UnwindSafe for TopicRouter<T>
where T: UnwindSafe,

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

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

Source§

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.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

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

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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