pub struct Topic { /* private fields */ }Expand description
A gossip pub/sub topic.
Topics are identified by a BLAKE3 hash of their name string. When subscribed, incoming messages are validated (§1.4) and delivered to an inbox. Messages from blocked senders (§1.5) are dropped silently.
§Examples
use ma_core::Topic;
let topic = Topic::new("/ma/broadcast/0.0.1");
assert_eq!(topic.name(), "/ma/broadcast/0.0.1");
assert!(!topic.is_subscribed());Implementations§
Source§impl Topic
impl Topic
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Create a new topic from a protocol-style name string.
The topic starts unsubscribed. Call subscribe to
begin receiving messages.
Sourcepub fn broadcast() -> Self
pub fn broadcast() -> Self
Create a topic for the well-known broadcast channel.
Equivalent to Topic::new("/ma/broadcast/0.0.1").
Sourcepub fn is_subscribed(&self) -> bool
pub fn is_subscribed(&self) -> bool
Whether this topic is currently subscribed (has an inbox).
Sourcepub fn subscribe(&mut self)
pub fn subscribe(&mut self)
Subscribe with a new internal inbox using the default capacity.
If already subscribed, this is a no-op.
Sourcepub fn subscribe_with(&mut self, inbox: Inbox<Message>)
pub fn subscribe_with(&mut self, inbox: Inbox<Message>)
Subscribe with an existing inbox, so messages from multiple sources converge into a single queue.
Replaces any previous inbox.
Sourcepub fn unsubscribe(&mut self)
pub fn unsubscribe(&mut self)
Unsubscribe — stop receiving messages and drop the inbox.
Sourcepub fn deliver(&mut self, message: Message) -> bool
pub fn deliver(&mut self, message: Message) -> bool
Deliver a message into this topic’s inbox after validation.
Returns true if the message was accepted, false if it was
rejected (wrong content type, has recipient, blocked sender,
expired, or not subscribed).
Sourcepub fn drain(&mut self) -> Vec<Message>
pub fn drain(&mut self) -> Vec<Message>
Drain all non-expired messages from the topic’s inbox.
Returns an empty Vec if not subscribed.
Sourcepub fn block(&mut self, sender_did: impl Into<String>)
pub fn block(&mut self, sender_did: impl Into<String>)
Block a sender DID. Messages from this sender will be dropped before any other validation.
Sourcepub fn is_blocked(&self, sender_did: &str) -> bool
pub fn is_blocked(&self, sender_did: &str) -> bool
Whether a sender DID is blocked.