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>
impl<T: PubsubTopic, P: Clone> Subscriber<T, P>
Sourcepub async fn recv(&self) -> Result<(T, P), PubsubError>
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:
- The topic the message was published to
- The message payload
§Returns
Ok((T, P))- A tuple containing the topic and payload if a message is receivedErr(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>
impl<T: Clone + PubsubTopic, P: Clone + Clone> Clone for Subscriber<T, P>
Source§fn clone(&self) -> Subscriber<T, P>
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: PubsubTopic, P: Clone> Drop for Subscriber<T, P>
When Subscriber is dropped, remove itself from all the Pubsub subscriptions
impl<T: PubsubTopic, P: Clone> Drop for Subscriber<T, P>
When Subscriber is dropped, remove itself from all the Pubsub subscriptions
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>
impl<T, P> Sync for Subscriber<T, P>
impl<T, P> Unpin for Subscriber<T, P>
impl<T, P> !UnwindSafe for Subscriber<T, P>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more