Subscriber

Struct Subscriber 

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

A subscriber that receives messages for subscribed topics from a Pubsub system.

The Subscriber struct represents an active subscription to one or more topics. It contains an internal Arc reference to shared state that manages the message channel and subscription information.

§Type Parameters

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

§Examples

use pubsub_rs::Pubsub;
async fn some_fn() {
    let pubsub: Pubsub<&str, String> = Pubsub::new();
    let subscriber = pubsub.subscribe(vec!["topic1", "topic2"]).await;
    let (topic, message) = subscriber.recv().await.unwrap();
}

Implementations§

Source§

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

Source

pub async fn recv(&self) -> Result<(T, P), PubsubError>

Receives the next message for this subscriber.

This async function waits until a message is published to one of the subscriber’s subscribed topics, then returns a tuple containing:

  1. The topic the message was published to
  2. The message payload
§Returns
  • Ok((T, P)) - A tuple containing the topic and payload if a message is received
  • Err(PubsubError) - If the pubsub system has been closed and no more messages will be sent
§Examples
// let pubsub = Pubsub::new();
// let subscriber = pubsub.subscribe(vec!["topic1"]).await;
// pubsub.publish("topic1", "Hello".to_owned()).await;
// let (topic, message) = subscriber.recv().await.unwrap();
// assert_eq!(topic, "topic1");
// assert_eq!(message, "Hello");

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Subscriber<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 Subscriber<T, P>

When Subscriber is dropped, remove itself from all the Pubsub subscriptions

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, P> !UnwindSafe for Subscriber<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.