Skip to main content

PubSub

Struct PubSub 

Source
pub struct PubSub<D> { /* private fields */ }
Expand description

An in-process hierarchical-topic pub/sub message bus.

D is the payload type passed to subscribers. The bus is single-threaded and uses interior mutability, so subscribers may call back into the bus during synchronous delivery.

Implementations§

Source§

impl<D> PubSub<D>

Source

pub fn new() -> Self

Create an empty bus.

Source

pub fn set_immediate_exceptions(&self, value: bool)

Set whether a panicking subscriber aborts delivery immediately.

Read fresh on each publish. Toggling it changes only later publishes.

Source

pub fn immediate_exceptions(&self) -> bool

Current value of the immediate-exceptions flag.

Source

pub fn subscribe<F>(&self, topic: impl Into<String>, func: F) -> Token
where F: Fn(&str, &D) + 'static,

Subscribe func to topic. Returns a Token for later removal.

Each call gets a fresh, unique token, even for the same callback on the same topic. The topic does not need to exist first.

Source

pub fn subscribe_all<F>(&self, func: F) -> Token
where F: Fn(&str, &D) + 'static,

Subscribe func to the wildcard topic *. It then runs for every published message. Returns the token, like PubSub::subscribe.

Source

pub fn subscribe_once<F>(&self, topic: impl Into<String>, func: F) -> Token
where F: Fn(&str, &D) + 'static,

Subscribe func to fire at most once.

Delivery removes the entry before it calls func. A republish of the same topic from inside func does not retrigger it.

Source

pub fn publish(&self, topic: impl Into<String>, data: D) -> bool

Publish data to topic for deferred delivery.

Queues delivery and returns at once. Subscribers run on the next PubSub::process_deferred call. Returns true if the topic had at least one matching subscriber when this was called.

Source

pub fn publish_sync(&self, topic: impl Into<String>, data: D) -> bool

Publish data to topic and deliver to all matching subscribers now.

Returns true if there was at least one matching subscriber, else false. The return value is computed before any subscriber runs.

Source

pub fn process_deferred(&self)

Run the deliveries queued by PubSub::publish so far, in call order.

This stands in for one event loop tick. It drains the jobs queued before the call. A subscriber that publishes during the drain queues its job for the next call, not this one. Call again to drain those.

Under delayed exceptions a panicking subscriber does not stop the other jobs in the batch. Every job runs, then the first panic is re-raised after the batch drains. Under immediate exceptions the first panic propagates at once and the rest of the batch does not run.

Source

pub fn pending(&self) -> usize

Number of queued deferred deliveries.

Source

pub fn unsubscribe(&self, token: &Token) -> Option<Token>

Remove the single subscription identified by token.

Returns the token if it matched a live subscription, else None. A second removal of the same token returns None.

Source

pub fn unsubscribe_topic(&self, topic: &str) -> bool

Remove a topic and every descendant topic, by string prefix.

Returns true if topic names an existing topic or a prefix of one, else false. Matching is a raw string prefix, not dot-boundary aware. unsubscribe_topic("a") therefore removes a, a.b, and ab alike.

Source

pub fn clear_all_subscriptions(&self)

Empty the whole registry. Does not reset the token counter.

Source

pub fn clear_subscriptions(&self, topic: &str)

Remove every topic whose name starts with topic (raw string prefix).

Source

pub fn count_subscriptions(&self, topic: &str) -> usize

Count subscribers in the first registered topic whose name starts with topic, then stop.

This counts one topic, not the sum across the hierarchy. It stops at the first prefix match and returns that topic’s subscriber count.

Source

pub fn get_subscriptions(&self, topic: &str) -> Vec<String>

List every registered topic name that starts with topic, in insertion order.

Trait Implementations§

Source§

impl<D> Debug for PubSub<D>

Source§

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

Print topic names with their subscriber counts. Payloads and callbacks are never formatted, so D need not be Debug.

Source§

impl<D> Default for PubSub<D>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<D> !Freeze for PubSub<D>

§

impl<D> !RefUnwindSafe for PubSub<D>

§

impl<D> !Send for PubSub<D>

§

impl<D> !Sync for PubSub<D>

§

impl<D> !UnwindSafe for PubSub<D>

§

impl<D> Unpin for PubSub<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for PubSub<D>

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.