Skip to main content

asimov_protocol/
topic_subscription.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::PublishError;
4use bytes::Bytes;
5pub use iroh_gossip::api::{GossipReceiver, GossipSender, GossipTopic};
6
7#[derive(Debug)]
8pub struct TopicSubscription(pub GossipTopic);
9
10impl TopicSubscription {
11    pub fn split(self) -> (GossipSender, GossipReceiver) {
12        self.0.split()
13    }
14
15    pub async fn publish(&mut self, payload: impl Into<Bytes>) -> Result<(), PublishError> {
16        Ok(self.0.broadcast(payload.into()).await?)
17    }
18}
19
20impl From<GossipTopic> for TopicSubscription {
21    fn from(input: GossipTopic) -> Self {
22        Self(input)
23    }
24}
25
26impl From<TopicSubscription> for GossipTopic {
27    fn from(input: TopicSubscription) -> Self {
28        input.0
29    }
30}