Pubsub

Struct Pubsub 

Source
pub struct Pubsub<T: PubsubTopic, P: Clone> { /* private fields */ }
Expand description

A publish-subscribe system that allows multiple subscribers to receive messages published to specific topics.

The Pubsub struct is the main interface for creating topics, publishing messages, and managing subscriptions. It uses an internal Arc reference to shared state, allowing multiple clones of the Pubsub instance to share the same underlying subscription data.

§Type Parameters

  • T - The topic type, must implement PubsubTopic (Clone + Hash + Eq)
  • P - The payload type, must implement Clone

§Examples

// let pubsub = Pubsub::new();
// let subscriber = pubsub.subscribe(vec!["topic1", "topic2"]).await;
// pubsub.publish("topic1", "Hello, world!".to_owned()).await;

Implementations§

Source§

impl<T: PubsubTopic, P: Clone> Pubsub<T, P>

Source

pub fn new() -> Self

Creates a new Pubsub instance with empty topic subscriptions.

This initializes the internal shared state and returns a new Pubsub instance that can be used to manage topics and subscriptions.

§Examples
// let pubsub = Pubsub::new();
Source

pub async fn subscribe(&self, topics: Vec<T>) -> Subscriber<T, P>

Subscribes to one or more topics and returns a new Subscriber instance.

§Arguments
  • topics - A vector of topics to subscribe to. The subscriber will receive messages published to any of these topics.
§Returns

A new Subscriber instance that can be used to receive messages for the subscribed topics.

§Example
// let pubsub = Pubsub::new();
// let subscriber = pubsub.subscribe(vec!["topic1", "topic2"]).await;
Source

pub async fn publish(&self, topic: T, payload: P)

Publishes a message to a specific topic.

§Arguments
  • topic - The topic to publish the message to. Subscribers subscribed to this topic will receive the message.
  • payload - The message payload to send to subscribers.
§Example
// let pubsub = Pubsub::new();
// pubsub.publish("topic1", "Hello, world!".to_owned()).await;

Trait Implementations§

Source§

impl<T: Clone + PubsubTopic, P: Clone + Clone> Clone for Pubsub<T, P>

Source§

fn clone(&self) -> Pubsub<T, P>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: PubsubTopic, P: Clone> Drop for Pubsub<T, P>

Implements the Drop trait for Pubsub to ensure proper cleanup when the last instance is dropped.

When the last strong reference to the Pubsub’s inner data is dropped, this implementation:

  1. Checks if this is the last strong reference (Arc::strong_count == 1)
  2. If so, iterates through all topics and their subscribers
  3. Closes each subscriber’s channel to prevent them from being stuck waiting for messages

This ensures that any remaining subscribers will receive an error on their next recv() call rather than blocking indefinitely, allowing them to clean up their resources properly.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, P> Freeze for Pubsub<T, P>

§

impl<T, P> !RefUnwindSafe for Pubsub<T, P>

§

impl<T, P> Send for Pubsub<T, P>
where T: Send + Sync, P: Send,

§

impl<T, P> Sync for Pubsub<T, P>
where T: Send + Sync, P: Send,

§

impl<T, P> Unpin for Pubsub<T, P>

§

impl<T, P> !UnwindSafe for Pubsub<T, P>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.